Repository navigation
Description
Which component is this bug for?
Langchain Instrumentation
📜 Description
Our langchain calls involving graph.add_conditional_edges are not being traced properly. We see the task spans exported, but that's about it. The parent spans, the workflow and other spans are not being exported.
I say "exported" because, when I locally added logging messages to opentelemetry-instrumentation-langchain and use that, I see these logs, which clearly indicate spans being created and the hierarchical structure is intact within the process. However, only a subset of these spans (task spans with IDs 30c85a38d1317f09, 05017876f019db00, 07b43ecb80a36357) are ever exported.
WITHIN _create_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: ac8113fdcb10386e | span_name: LangGraph.workflow
WITHIN _create_task_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: ac8113fdcb10386e | span_name: LangGraph.workflow
WITHIN _create_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: 09be633b9b552d90 | span_name: __start__.task
WITHIN _create_task_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: 09be633b9b552d90 | span_name: __start__.task
WITHIN _create_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: 416dbbd587fad2ca | span_name: initial_plan.task
WITHIN _create_task_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: 416dbbd587fad2ca | span_name: initial_plan.task
WITHIN _create_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: 6437f89658e0d14d | span_name: ChannelWrite<initial_plan,notebook,job_status,messages,customer_id,operation,instruction,task_id,runbook,last_tool_call,start_time,focused_block_id>.task
WITHIN _create_task_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: 6437f89658e0d14d | span_name: ChannelWrite<initial_plan,notebook,job_status,messages,customer_id,operation,instruction,task_id,runbook,last_tool_call,start_time,focused_block_id>.task
WITHIN _create_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: ebb1277f2ceae490 | span_name: planner.task
WITHIN _create_task_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: ebb1277f2ceae490 | span_name: planner.task
WITHIN _create_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: 0b1e3310d3aa143d | span_name: ChannelWrite<planner,notebook,job_status,messages,customer_id,operation,instruction,task_id,runbook,last_tool_call,start_time,focused_block_id>.task
WITHIN _create_task_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: 0b1e3310d3aa143d | span_name: ChannelWrite<planner,notebook,job_status,messages,customer_id,operation,instruction,task_id,runbook,last_tool_call,start_time,focused_block_id>.task
WITHIN _create_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: 07b43ecb80a36357 | span_name: decide_tool.task
WITHIN _create_task_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: 07b43ecb80a36357 | span_name: decide_tool.task
WITHIN _create_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: 617c6b4629d9f51b | span_name: ChannelWrite<branch:planner:decide_tool:observe_agent>.task
WITHIN _create_task_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: 617c6b4629d9f51b | span_name: ChannelWrite<branch:planner:decide_tool:observe_agent>.task
WITHIN _create_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: 77693f769bc8f79c | span_name: observe_agent.task
WITHIN _create_task_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: 77693f769bc8f79c | span_name: observe_agent.task
WITHIN _create_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: cad0c8cd4065309e | span_name: ChannelWrite<observe_agent,notebook,job_status,messages,customer_id,operation,instruction,task_id,runbook,last_tool_call,start_time,focused_block_id>.task
WITHIN _create_task_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: cad0c8cd4065309e | span_name: ChannelWrite<observe_agent,notebook,job_status,messages,customer_id,operation,instruction,task_id,runbook,last_tool_call,start_time,focused_block_id>.task
WITHIN _create_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: e96d8161f3ddc979 | span_name: planner.task
WITHIN _create_task_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: e96d8161f3ddc979 | span_name: planner.task
WITHIN _create_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: 7ec96633edee4666 | span_name: ChannelWrite<planner,notebook,job_status,messages,customer_id,operation,instruction,task_id,runbook,last_tool_call,start_time,focused_block_id>.task
WITHIN _create_task_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: 7ec96633edee4666 | span_name: ChannelWrite<planner,notebook,job_status,messages,customer_id,operation,instruction,task_id,runbook,last_tool_call,start_time,focused_block_id>.task
WITHIN _create_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: 05017876f019db00 | span_name: decide_tool.task
WITHIN _create_task_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: 05017876f019db00 | span_name: decide_tool.task
WITHIN _create_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: d5584cfe6b7af38a | span_name: ChannelWrite<branch:planner:decide_tool:code_agent>.task
WITHIN _create_task_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: d5584cfe6b7af38a | span_name: ChannelWrite<branch:planner:decide_tool:code_agent>.task
WITHIN _create_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: b652bef72ac28696 | span_name: code_agent.task
WITHIN _create_task_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: b652bef72ac28696 | span_name: code_agent.task
WITHIN _create_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: fe8297e18d9c8d2f | span_name: ChannelWrite<code_agent,notebook,job_status,messages,customer_id,operation,instruction,task_id,runbook,last_tool_call,start_time,focused_block_id>.task
WITHIN _create_task_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: fe8297e18d9c8d2f | span_name: ChannelWrite<code_agent,notebook,job_status,messages,customer_id,operation,instruction,task_id,runbook,last_tool_call,start_time,focused_block_id>.task
WITHIN _create_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: 35ecf636abd59901 | span_name: planner.task
WITHIN _create_task_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: 35ecf636abd59901 | span_name: planner.task
WITHIN _create_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: d519483b88fb4831 | span_name: ChannelWrite<planner,notebook,job_status,messages,customer_id,operation,instruction,task_id,runbook,last_tool_call,start_time,focused_block_id>.task
WITHIN _create_task_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: d519483b88fb4831 | span_name: ChannelWrite<planner,notebook,job_status,messages,customer_id,operation,instruction,task_id,runbook,last_tool_call,start_time,focused_block_id>.task
WITHIN _create_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: 30c85a38d1317f09 | span_name: decide_tool.task
WITHIN _create_task_span trace_id: fbd0346509605b292fafdb92e4ed2e53 | span_id: 30c85a38d1317f09 | span_name: decide_tool.task
👟 Reproduction steps
Reproducible using latest version of openllmetry.
Instrument LangChain agentic app using openllmetry. Invoke add_conditional_edges similar to the following:
graph.add_conditional_edges(
"planner",
self.decide_tool,
[
"node1",
"node2",
"node3",
"node4",
"node5",
"node6",
"node7",
"end",
],
)
graph.add_edge("node1", "planner")
graph.add_edge("node2", "planner")
graph.add_edge("node6", "planner")
graph.add_edge("node7", "planner")
graph.add_edge("node3", "planner")
graph.add_edge("node5", "planner")
graph.add_edge("node4", "planner")
Where decide_tool is an async function:
async def decide_tool(state: PlannerState) -> str:
...
return "Something"
👍 Expected behavior
All created spans must be exported
👎 Actual Behavior with Screenshots
Only a few spans get exported
🤖 Python Version
3.11.8
📃 Provide any additional context for the Bug.
No response
👀 Have you spent some time to check if this bug has been raised before?
- I checked and didn't find similar issue
Are you willing to submit PR?
None
Activity
nirga commentedon Mar 11, 2025
Thanks @obs-gh-abhishekrao! We'll take a look. If you can provide a full working app that would be tremendously helpful :)
(no worries if not)
obs-gh-abhishekrao commentedon Mar 13, 2025
Hi @nirga, thank you for looking into this! I'm trying to get a reproducible app for this bug but so far haven't been able to nail down the exact conditions leading up to it.
obs-gh-abhishekrao commentedon Mar 13, 2025
Some progress that I hope could be helpful. I used the sample app https://github.com/traceloop/openllmetry/blob/main/packages/sample-app/sample_app/langgraph_example.py for comparison as it looked similar to our app and along with the custom logging I added, I'm able to see some new differences
In the sample app version, I could see the following logs, wherein all
_create_spancalls have corresponding_end_spancalls as I would expect:However, in our app, we don't see this behaviour. We only see the
_end_spancalled on everydecide_tool.taskspan. Something tells meChannelWritemay have a role to play in this bug, but not sure.[-]🐛 Bug Report: Missing span exports in LangChain instrumentation[/-][+]🐛 Bug Report: Langchain instrumentation does not end spans when output contains Date fields[/+]obs-gh-abhishekrao commentedon Mar 20, 2025
Hi @nirga, I found the root cause of this issue.
The problem happens here
openllmetry/packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/utils.py
Line 11 in a76870c
Our outputs contain a date field which is not handled by
CallbackFilteredJSONEncoder. Through debugging, when we mutatedoutputsto drop that field before it's passed to the encoder, the spans ended correctly.So I think adding some logic for date types to
CallbackFilteredJSONEncoderwill be the solution here. I tried something like this and it worked, but it likely needs more care:Updated title to be more precise.
nirga commentedon Mar 22, 2025
Thanks @obs-gh-abhishekrao! Creating a PR now