fix(langchain): use message.text as content#3702
fix(langchain): use message.text as content#3702ianchi wants to merge 1 commit intotraceloop:mainfrom
Conversation
There was a problem hiding this comment.
Important
Looks good to me! 👍
Reviewed everything up to 1e581ca in 7 seconds. Click for details.
- Reviewed
24lines of code in1files - Skipped
0files when reviewing. - Skipped posting
0draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
Workflow ID: wflow_ubkzvS8Uypp94adQ
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
|
No actionable comments were generated in the recent review. 🎉 📝 WalkthroughWalkthroughModified content extraction in set_chat_response to prefer Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/span_utils.py (1)
229-238:generation.textpriority may shadowmessage.textfor some providers.The fix is correct for the common case: when
message.contentis a complex array,ChatGeneration.textis set to""by LangChain's validator, so line 229 is falsy and execution reaches themessage.textbranch. However, some providers may explicitly populategeneration.textwith a partial/truncated string representation even whenmessage.contentis a list, which would silently bypass the newmessage.textpath entirely.Consider inverting the priority for chat-type generations so
message.textis preferred when available:♻️ Proposed refinement
- if hasattr(generation, "text") and generation.text: - content = generation.text - elif hasattr(generation, "message") and generation.message: - if hasattr(generation.message, "text") and generation.message.text: - content = generation.message.text - elif generation.message.content: - if isinstance(generation.message.content, str): - content = generation.message.content - else: - content = json.dumps(generation.message.content, cls=CallbackFilteredJSONEncoder) + if hasattr(generation, "message") and generation.message: + if hasattr(generation.message, "text") and generation.message.text: + content = generation.message.text + elif generation.message.content: + if isinstance(generation.message.content, str): + content = generation.message.content + else: + content = json.dumps(generation.message.content, cls=CallbackFilteredJSONEncoder) + elif hasattr(generation, "text") and generation.text: + # Non-chat completions (plain Generation objects) + content = generation.text🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/span_utils.py` around lines 229 - 238, Change the extraction order so chat-style generations prefer message content over the flattened text: when a generation has a .message and that message has usable text/content, use generation.message.text or generation.message.content (serializing non-string content with CallbackFilteredJSONEncoder) before falling back to generation.text; update the logic around the generation variable in span_utils.py (the branches referencing generation.text, generation.message, generation.message.text, and generation.message.content) so .message is checked first for chat-type generations.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In
`@packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/span_utils.py`:
- Around line 229-238: Change the extraction order so chat-style generations
prefer message content over the flattened text: when a generation has a .message
and that message has usable text/content, use generation.message.text or
generation.message.content (serializing non-string content with
CallbackFilteredJSONEncoder) before falling back to generation.text; update the
logic around the generation variable in span_utils.py (the branches referencing
generation.text, generation.message, generation.message.text, and
generation.message.content) so .message is checked first for chat-type
generations.
|
Inverted priority of generation.text/message.text as suggested. |
messages have a text property that handles complex cases of text data when content block is an array.
Use this property when available to handle correctly those cases, especially needed when the array has mixed types.
feat(instrumentation): ...orfix(instrumentation): ....Important
Use
message.textas content inset_chat_responseinspan_utils.pyto handle complex text data cases.set_chat_responseinspan_utils.py, usemessage.textascontentwhen available, handling complex text data cases.message.contentifmessage.textis unavailable, ensuring proper handling of mixed type arrays.This description was created by
for 1e581ca. You can customize this summary. It will automatically update as commits are pushed.
Summary by CodeRabbit