Fix attribute error for raw tuples in langchain#261
Conversation
a0b7a53 to
d6bbdb3
Compare
There was a problem hiding this comment.
Pull request overview
Fixes a crash in the LangChain instrumentation when agent/workflow inputs provide raw ("role", "content") tuples in the messages list, which previously could raise AttributeError and prevent correct agent invocation/span tracking.
Changes:
- Add a type guard in
to_input_messagesto avoid accessing.contenton non-BaseMessageinputs. - Add a unit test covering
messageslists containing non-BaseMessageentries. - Add a towncrier changelog fragment describing the behavior change.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| instrumentation/opentelemetry-instrumentation-genai-langchain/src/opentelemetry/instrumentation/genai/langchain/utils.py | Skips non-BaseMessage entries during input message conversion to avoid runtime errors. |
| instrumentation/opentelemetry-instrumentation-genai-langchain/tests/test_callback_handler.py | Adds a regression test around mixed tuple/BaseMessage inputs to make_input_message. |
| instrumentation/opentelemetry-instrumentation-genai-langchain/.changelog/261.changed | Adds a changelog fragment for the behavior change. |
|
This PR has review comments. Review suggestions, whether from maintainers or automated reviewers, aren't always correct or required. Please evaluate each comment on its merits, then make sure each thread has a clear outcome. For example, link to the commit if you applied a suggestion, explain why it wasn't applied, or ask a follow-up question. Automation flags a PR for human review once every review thread has a reply or is marked as resolved. Status across open PRs is visible on the pull request dashboard. |
d6bbdb3 to
c3d11cb
Compare
f6ced75 to
c9f08d6
Compare
c9f08d6 to
ac93a87
Compare
Description
to_input_messagesassumed every entry in an input messages list was a LangChain BaseMessage. LangGraph/create_agent inputs commonly use raw ("role", text) tuples (e.g. {"messages": [("human", user_input)]}). Accessing .content on such a tuple raised AttributeError("'tuple' object has no attribute 'content'"), which aborted the callback before the invocation could be registered.This exception fired inside the root agent's on_chain_start, before the agent invocation was registered in the invocation manager. As a result, find_nearest_agent could never locate the root agent, so each LangGraph super-step node (model, tools, ...) created its own invoke_agent span, producing duplicate invoke_agent spans instead of one agent span with nested tool/LLM children.
After Change -
Type of change
Please delete options that are not relevant.
How has this been tested?
Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. List any relevant details for your test
configuration.
Checklist
See CONTRIBUTING.md
for the style guide, changelog guidance, and more.