Fix: Handle list token in LangChain callback on_llm_new_token to prevent AttributeError #2191
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 asstrip()
on it.If a list is received, this results in an
AttributeError
('list' object has no attribute 'strip'
).What does this PR do?
token
argument in theon_llm_new_token
callback.token
is a list, its elements are joined into a single string.token
is not a string, it is converted to a string.How to test
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!