Skip to content

fix(langchain): preserve error description on span status#4072

Open
glogiotatidis wants to merge 1 commit intotraceloop:mainfrom
glogiotatidis:fix-langchain-set-status-description
Open

fix(langchain): preserve error description on span status#4072
glogiotatidis wants to merge 1 commit intotraceloop:mainfrom
glogiotatidis:fix-langchain-set-status-description

Conversation

@glogiotatidis
Copy link
Copy Markdown

@glogiotatidis glogiotatidis commented May 4, 2026

Span.set_status(...) per the OTel SDK contract silently drops the description argument and emits a warning when the first argument is already a Status object:

Description ... ignored. Use either `Status` or `(StatusCode, Description)`

TraceloopCallbackHandler._handle_error was calling

span.set_status(Status(StatusCode.ERROR), str(error))

so the human-readable error message was never set on the span's status, and every callback error caused a warning to be logged from opentelemetry.sdk.trace.

Pass the description inside Status(...) instead — matching the canonical pattern used elsewhere in the openllmetry monorepo (see PR #3970 for anthropic/groq/mistralai).

Adds tests/test_error_status.py as a regression test that pins the behaviour: the span's status description equals the exception message, and no Description ... ignored warning is emitted.

  • I have added tests that cover my changes.
  • If adding a new instrumentation or changing an existing one, I've added screenshots from some observability platform showing the change.
  • PR name follows conventional commits format: feat(instrumentation): ... or fix(instrumentation): ....
  • (If applicable) I have updated the documentation accordingly.

Summary by CodeRabbit

  • Bug Fixes

    • Error spans now include the exception message in their status description, ensuring tracing captures full error context and avoids lost descriptions.
  • Tests

    • Added a regression test that verifies run-time errors produce spans marked ERROR with the exact exception message in the status description and that no SDK warnings about dropped descriptions are emitted.

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 4, 2026

CLA assistant check
All committers have signed the CLA.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 4, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5ea0e8f0-9e9c-4774-a879-0a7c7e5f50ff

📥 Commits

Reviewing files that changed from the base of the PR and between ad68e02 and aab61fd.

📒 Files selected for processing (2)
  • packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/callback_handler.py
  • packages/opentelemetry-instrumentation-langchain/tests/test_error_status.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/opentelemetry-instrumentation-langchain/tests/test_error_status.py

📝 Walkthrough

Walkthrough

Span status setting in the LangChain OpenTelemetry callback was corrected to embed the exception message in the Status object passed to span.set_status, and a regression test was added to assert exported spans carry the exception message in status.description.

Changes

Error Status Handling Fix

Layer / File(s) Summary
Core Bug Fix
packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/callback_handler.py
_handle_error now calls span.set_status(Status(StatusCode.ERROR, str(error))), placing the exception message inside the Status object rather than passing it as a separate set_status argument.
Tests / Regression
packages/opentelemetry-instrumentation-langchain/tests/test_error_status.py
Adds _explode(_) helper and test_chain_error_sets_status_description that runs a failing RunnableLambda, collects finished spans, asserts at least one span has status.status_code == StatusCode.ERROR, checks each ERROR span's status.description == "boom: something went wrong", and ensures no SDK warning about ignored descriptions was emitted.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A rabbit hops through error traces,
and tucks descriptions in the right places.
No warnings left to make a fuss,
the spans now whisper what went wrong to us. 🐇

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the main fix: preserving error description on span status in the langchain instrumentation, which is the primary change in the changeset.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share
Review rate limit: 6/8 reviews remaining, refill in 11 minutes and 21 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

@glogiotatidis glogiotatidis force-pushed the fix-langchain-set-status-description branch from 47447e0 to ad68e02 Compare May 4, 2026 09:50
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/opentelemetry-instrumentation-langchain/tests/test_error_status.py`:
- Around line 38-52: The test is flakey because the session-scoped
InMemorySpanExporter (span_exporter) accumulates spans across tests; to isolate
this test call span_exporter.clear() at the start of the test_error_status
function (before the chain runs and before calling
span_exporter.get_finished_spans()) so get_finished_spans() only returns spans
produced by this test and the subsequent assertions on span.status.description
are reliable.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1d260691-e20a-4e39-80ba-55aea839ba52

📥 Commits

Reviewing files that changed from the base of the PR and between abbcc11 and 47447e0.

📒 Files selected for processing (2)
  • packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/callback_handler.py
  • packages/opentelemetry-instrumentation-langchain/tests/test_error_status.py

`Span.set_status(...)` per the OTel SDK contract silently drops the
`description` argument and emits a warning when the first argument is
already a `Status` object:

    Description ... ignored. Use either `Status` or `(StatusCode, Description)`

`TraceloopCallbackHandler._handle_error` was calling

    span.set_status(Status(StatusCode.ERROR), str(error))

so the human-readable error message was never set on the span's status,
and every callback error caused a warning to be logged from
`opentelemetry.sdk.trace`.

Pass the description inside `Status(...)` instead — matching the
canonical pattern used elsewhere in the openllmetry monorepo (see
PR traceloop#3970 for anthropic/groq/mistralai).

Adds `tests/test_error_status.py` as a regression test that pins the
behaviour: the span's status description equals the exception message,
and no `Description ... ignored` warning is emitted.
@glogiotatidis glogiotatidis force-pushed the fix-langchain-set-status-description branch from ad68e02 to aab61fd Compare May 4, 2026 10:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants