fix(langchain): coerce LangGraph str-subclass node ids to str for OTel attributes (#4447) - #4448
Conversation
LangGraph permits str-subclass node ids (e.g. StrEnum members). extract_graph_structure appended them raw, and OpenTelemetry validates sequence attributes with an exact-type check, so a subclass instance caused the entire gen_ai.workflow.nodes attribute to be rejected (and opentelemetry.attributes logged a warning on every graph invocation). Coerce node ids to plain str when collecting them. Edges were already built via f-strings and thus unaffected. Closes traceloop#4447.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Team Run ID: 📒 Files selected for processing (2)
Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review. 📝 WalkthroughWalkthroughLangGraph graph extraction now converts node IDs to exact ChangesLangGraph node ID normalization
Estimated code review effort: 2 (Simple) | ~10 minutes Merge Risk: ⚪ Minimal · up to The change narrowly converts LangGraph node IDs to plain strings so OpenTelemetry accepts the workflow node attribute, with focused tests covering the affected behavior. No actionable merge-blocking risk remains after normal checks and review. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Linked Issues checkExplanation The change satisfies issue
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
Summary
Closes #4447.
LangGraph permits
StrEnum(and otherstr-subclass) node ids.extract_graph_structureappended them raw to the nodes list, which is then passed tospan.set_attribute(gen_ai.workflow.nodes, ...). OpenTelemetry validates sequence attributes with an exact-type check (type(element) in (bool, str, bytes, int, float)), so astrsubclass is rejected — the entiregen_ai.workflow.nodesattribute is dropped andopentelemetry.attributeslogs a warning on every graph invocation.Fix
Coerce node ids to a plain
strwhen collecting them inlanggraph_utils.extract_graph_structure. Edges are unaffected because they are already built via f-strings (which produce exact-strinstances).Verification
End-to-end with a real LangGraph graph using
StrEnumnode ids and a recordingTracerProvider:WARNING opentelemetry.attributes: Invalid type NodeName in attribute 'gen_ai.workflow.nodes' value sequence→ the attribute isNone(dropped).gen_ai.workflow.nodes == ("parse", "classify"), no warning.Added
tests/test_langgraph_graph_structure.py(3 self-contained tests, no API/VCR): str-subclass coercion, the OTel attribute being accepted end-to-end, and unchanged behavior for plain-string ids. The two str-subclass tests fail on the unpatched code (KeyError: 'gen_ai.workflow.nodes'/ non-strelement types) and pass with the fix.Summary by CodeRabbit
Bug Fixes
Tests