Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trim_messages causes an IndexError when invoked with strategy="last", include_system=True, and empty messages #26895

Closed
5 tasks done
sudosays opened this issue Sep 26, 2024 · 0 comments · Fixed by #26896
Closed
5 tasks done
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature Ɑ: core Related to langchain-core investigate

Comments

@sudosays
Copy link
Contributor

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

from langchain_core.messages import trim_messages

trim_messages(max_tokens=10, token_counter=lambda x: 0, include_system=True).invoke([])

Error Message and Stack Trace (if applicable)

Error type: IndexError

Stack trace:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/julius/harbour/documents/clinigma/casa/.venv/lib/python3.12/site-packages/langchain_core/runnables/base.py", line 4706, in invoke
    return self._call_with_config(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/julius/harbour/documents/clinigma/casa/.venv/lib/python3.12/site-packages/langchain_core/runnables/base.py", line 1923, in _call_with_config
    context.run(
  File "/home/julius/harbour/documents/clinigma/casa/.venv/lib/python3.12/site-packages/langchain_core/runnables/config.py", line 396, in call_func_with_variable_args
    return func(input, **kwargs)  # type: ignore[call-arg]
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/julius/harbour/documents/clinigma/casa/.venv/lib/python3.12/site-packages/langchain_core/runnables/base.py", line 4562, in _invoke
    output = call_func_with_variable_args(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/julius/harbour/documents/clinigma/casa/.venv/lib/python3.12/site-packages/langchain_core/runnables/config.py", line 396, in call_func_with_variable_args
    return func(input, **kwargs)  # type: ignore[call-arg]
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/julius/harbour/documents/clinigma/casa/.venv/lib/python3.12/site-packages/langchain_core/messages/utils.py", line 853, in trim_messages
    return _last_max_tokens(
           ^^^^^^^^^^^^^^^^^
  File "/home/julius/harbour/documents/clinigma/casa/.venv/lib/python3.12/site-packages/langchain_core/messages/utils.py", line 962, in _last_max_tokens
    swapped_system = include_system and isinstance(messages[0], SystemMessage)
                                                   ~~~~~~~~^^^
IndexError: list index out of range

Description

What is the issue?

  • Trying to use trim_messages with include_system=True in a RunnableWithMessageHistory without a message already in a MessageHistory

Expected behaviour:

  • an empty list is returned

Current behaviour:

  • Raises an IndexError and crashes

This was found when setting up a RunnableWithMessageHistory where there were no initial messages in the store to pull from.

The documentation currently provides examples where prior messages are available, which could be a pitfall to new users.

Testing

I have tested all other combinations of argument values for:

  • strategy
  • allow_partials
  • include_system
  • starts_on (partially with only "human" and "system")
  • ends_on (partially with only "human" and "system")

The error is only triggered when strategy="last" and include_system=True

System Info

System Information

OS: Linux
OS Version: #1 SMP PREEMPT_DYNAMIC Thu, 12 Sep 2024 17:21:02 +0000
Python Version: 3.12.6 (main, Sep 8 2024, 13:18:56) [GCC 14.2.1 20240805]

Package Information

langchain_core: 0.3.6
langchain: 0.3.1
langchain_community: 0.3.1
langsmith: 0.1.128
langchain_openai: 0.2.0
langchain_text_splitters: 0.3.0

Optional packages not installed

langgraph
langserve

Other Dependencies

aiohttp: 3.10.6
async-timeout: Installed. No version info available.
dataclasses-json: 0.6.7
httpx: 0.27.2
jsonpatch: 1.33
numpy: 1.26.4
openai: 1.48.0
orjson: 3.10.7
packaging: 24.1
pydantic: 2.9.2
pydantic-settings: 2.5.2
PyYAML: 6.0.2
requests: 2.32.3
SQLAlchemy: 2.0.35
tenacity: 8.5.0
tiktoken: 0.7.0
typing-extensions: 4.12.2

@langcarl langcarl bot added the investigate label Sep 26, 2024
@dosubot dosubot bot added Ɑ: core Related to langchain-core 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature labels Sep 26, 2024
Sheepsta300 pushed a commit to Sheepsta300/langchain that referenced this issue Oct 1, 2024
…angchain-ai#26896)

This prevents `trim_messages` from raising an `IndexError` when invoked
with `include_system=True`, `strategy="last"`, and an empty message
list.

Fixes langchain-ai#26895

Dependencies: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature Ɑ: core Related to langchain-core investigate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant