Skip to content

fix(langchain): record dict-form assistant messages on workflow and agent spans - #881

Merged
lmolkova merged 4 commits into
open-telemetry:mainfrom
Thanatos9404:fix/langchain-dict-output-messages
Oct 15, 2026
Merged

lmolkova merged 4 commits into
open-telemetry:mainfrom
Thanatos9404:fix/langchain-dict-output-messages

Conversation

@Thanatos9404

Copy link
Copy Markdown
Contributor

Description

Fixes the output half of #388. When a workflow's or agent's messages contain role/content dicts or (role, content) tuples, gen_ai.output.messages was dropped, or an earlier AIMessage was reported as the final output, because to_output_messages only kept AIMessage instances.

Inputs and outputs now share a _normalize_messages helper that converts entries one at a time, so one unconvertible entry no longer drops the other convertible ones. This picks up the review feedback on #417 (closed as stale); thanks @alexliluz.

Repro: a StateGraph with a plain messages: list channel (no add_messages reducer) whose node returns {"role": "assistant", "content": "42"}. On main the invoke_workflow span has no gen_ai.output.messages; with this change it records the assistant message.

Fixes #388

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How has this been tested?

  • tox -e py310-test-instrumentation-genai-langchain-oldest (langchain-core 0.3.86): 448 passed, 11 skipped
  • tox -e py310-test-instrumentation-genai-langchain-latest: 494 passed, 4 skipped
  • tox -e py314-test-instrumentation-genai-langchain-latest: 498 passed
  • tox -e precommit and tox -e typecheck (Python 3.14): pass
  • The 7 new tests fail on main and pass with this change.

Checklist

  • Followed the style guidelines of this project
  • Changelog updated if the change requires an entry
  • Unit tests added

AI-assisted (Claude Opus 5.5), disclosed with an Assisted-by: commit trailer. I have reviewed the change and can answer questions about it.

…gent spans

to_output_messages kept only AIMessage instances, so role/content dicts
and (role, content) tuples returned in a workflow or agent's `messages`
were dropped from gen_ai.output.messages, or an earlier AIMessage was
reported as the final output instead.

Convert entries with convert_to_messages one at a time in a shared
helper used for both inputs and outputs, so a single unconvertible entry
no longer drops the other convertible ones.

Fixes open-telemetry#388

Assisted-by: Claude Opus 5.5
@Thanatos9404
Thanatos9404 requested a review from a team as a code owner October 12, 2026 04:01
Copilot AI balanced review requested due to automatic review settings October 12, 2026 04:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@linux-foundation-easycla

linux-foundation-easycla Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

Assisted-by: Claude Opus 5.5
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Merged · refreshed 2026-10-15 22:32 UTC

Status above doesn't look right?
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

@Thanatos9404

Copy link
Copy Markdown
Contributor Author

/easycla

… entry

Try convert_to_messages on the whole list first and only convert entries
one at a time when that fails. In the fallback, BaseMessage instances are
kept as they are instead of going through convert_to_messages again.

Assisted-by: Claude Opus 5.5
@lmolkova
lmolkova enabled auto-merge October 13, 2026 23:55
@lmolkova
lmolkova disabled auto-merge October 13, 2026 23:55
@lmolkova
lmolkova enabled auto-merge October 13, 2026 23:56
Resolve the conflict in test_callback_handler.py by taking main's removal
of the hand-driven callback tests (open-telemetry#884) and moving the dict-form workflow
and agent output check into test_chain_input_and_output_capture as a
dict-assistant case.

Assisted-by: Claude Opus 5.5
auto-merge was automatically disabled October 14, 2026 02:27

Head branch was pushed to by a user without write access

@lmolkova
lmolkova enabled auto-merge October 15, 2026 22:14
@lmolkova
lmolkova added this pull request to the merge queue Oct 15, 2026
Merged via the queue into open-telemetry:main with commit 885c8b8 Oct 15, 2026
82 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

langchain: dict-form messages raise in on_chain_start and silently drop the span

4 participants