Skip to content

Fix: Handle list token in LangChain callback on_llm_new_token to prevent AttributeError #2191

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

2429-mori-a
Copy link

Background

When using certain models or integrations with LangChain, the on_llm_new_token callback may receive a list of tokens instead of a string.
Chainlit currently expects token to be a string and directly calls string methods such as strip() on it.
If a list is received, this results in an AttributeError ('list' object has no attribute 'strip').

What does this PR do?

  • Adds type checking for the token argument in the on_llm_new_token callback.
  • If token is a list, its elements are joined into a single string.
  • If token is not a string, it is converted to a string.
  • This ensures compatibility with all LangChain model integrations and prevents runtime errors when a list is received.

How to test

  • Run the existing tests to ensure there are no regressions.
  • (Optional) Add a test where a list is passed as the token argument to confirm that no error occurs and the behavior is as expected.

Please let me know if any changes or improvements are needed!

Ensure on_llm_new_token properly handles the case where `token` is a list, preventing AttributeError when calling string methods.
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. backend Pertains to the Python backend. labels May 26, 2025
@hayescode hayescode enabled auto-merge July 1, 2025 00:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Pertains to the Python backend. size:S This PR changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants