fix(langchain): parse tool_calls from AIMessage #1316
Merged
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.
Important
Enhance
_convert_message_to_dictinCallbackHandler.pyto capturetool_callsfromAIMessagefor improved traceability in LangChain workflows._convert_message_to_dictinCallbackHandler.pyto includetool_callsfromAIMessageif present.tool_callsare captured in the message dictionary for better traceability.client.pyandprompt_cache.pyfor consistency.client.pyforclear_prompt_cache()function.This description was created by
for cba05ab. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Summary
This PR adds support for capturing
tool_callsfrom AIMessage objects in the Langchain callback handler. The change modifies the_convert_message_to_dictmethod inlangfuse/langchain/CallbackHandler.pyto check if an AIMessage hastool_callsand includes them in the message dictionary when present.The implementation adds a conditional block that checks for the existence of the
tool_callsattribute, ensures it's not None, and verifies it has content before adding it to the message dictionary. This follows the established pattern used throughout the method for other optional message attributes.This enhancement integrates with Langfuse's observability capabilities by ensuring that tool/function calling information from LangChain agents and chat models is properly captured and tracked. Modern LLM workflows increasingly rely on tool calling for complex tasks, and this data is essential for understanding agent behavior, debugging workflows, and maintaining comprehensive trace records. The change fits naturally into the existing message conversion logic and maintains backward compatibility.
Confidence score: 5/5