Skip to content

🐛 Bug Report: Langchain instrumentation does not end spans when output contains Date fields #2775

Closed

Description

@obs-gh-abhishekrao

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

  1. nirga commented on Mar 11, 2025

    @nirga
    Member

    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)

  2. obs-gh-abhishekrao commented on Mar 13, 2025

    @obs-gh-abhishekrao
    ContributorAuthor

    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.

  3. obs-gh-abhishekrao commented on Mar 13, 2025

    @obs-gh-abhishekrao
    ContributorAuthor

    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_span calls have corresponding _end_span calls as I would expect:

    CRITICAL:root:WITHIN _create_span trace_id: f3bfcfd49f1a9db45b2d2a18184b984a | span_id: 50e5ffc06f909133 | span_name: LangGraph.workflow
    CRITICAL:root:WITHIN _create_span trace_id: f3bfcfd49f1a9db45b2d2a18184b984a | span_id: 577828c0fd693b27 | span_name: __start__.task
    CRITICAL:root:WITHIN _create_span trace_id: f3bfcfd49f1a9db45b2d2a18184b984a | span_id: 65090ff78d4da776 | span_name: _write.task
    CRITICAL:root:WITHIN _end_span for trace_id: f3bfcfd49f1a9db45b2d2a18184b984a | span_id: 65090ff78d4da776
    CRITICAL:root:WITHIN _create_span trace_id: f3bfcfd49f1a9db45b2d2a18184b984a | span_id: 6b674d1adcc55ce4 | span_name: _write.task
    CRITICAL:root:WITHIN _end_span for trace_id: f3bfcfd49f1a9db45b2d2a18184b984a | span_id: 6b674d1adcc55ce4
    CRITICAL:root:WITHIN _end_span for trace_id: f3bfcfd49f1a9db45b2d2a18184b984a | span_id: 577828c0fd693b27
    CRITICAL:root:WITHIN _end_span CHILD SPAN ENDED for child_id: 2afdd01b-deb8-4d54-b6b0-d63d0688860e | child_span trace_id: f3bfcfd49f1a9db45b2d2a18184b984a | child_span span_id: 65090ff78d4da776
    CRITICAL:root:WITHIN _end_span CHILD SPAN ENDED for child_id: 2e22e706-b947-443f-a19a-90c8f5f97a9e | child_span trace_id: f3bfcfd49f1a9db45b2d2a18184b984a | child_span span_id: 6b674d1adcc55ce4
    CRITICAL:root:WITHIN _create_span trace_id: f3bfcfd49f1a9db45b2d2a18184b984a | span_id: a194be5749fdc4b3 | span_name: agent.task
    CRITICAL:root:WITHIN _create_span trace_id: f3bfcfd49f1a9db45b2d2a18184b984a | span_id: 5cc78e90ea667195 | span_name: ChatOpenAI.chat
    CRITICAL:root:_OpenAITracingWrapper for run_id: 9d5cd4b4-b24a-485f-8958-259aaa760e21
    CRITICAL:root:WITHIN _end_span for trace_id: f3bfcfd49f1a9db45b2d2a18184b984a | span_id: 5cc78e90ea667195
    CRITICAL:root:WITHIN _create_span trace_id: f3bfcfd49f1a9db45b2d2a18184b984a | span_id: 7dd18383c6db01ca | span_name: _write.task
    CRITICAL:root:WITHIN _end_span for trace_id: f3bfcfd49f1a9db45b2d2a18184b984a | span_id: 7dd18383c6db01ca
    CRITICAL:root:WITHIN _create_span trace_id: f3bfcfd49f1a9db45b2d2a18184b984a | span_id: 2e38eea3a47b9d11 | span_name: should_continue.task
    CRITICAL:root:WITHIN _end_span for trace_id: f3bfcfd49f1a9db45b2d2a18184b984a | span_id: 2e38eea3a47b9d11
    CRITICAL:root:WITHIN _end_span for trace_id: f3bfcfd49f1a9db45b2d2a18184b984a | span_id: a194be5749fdc4b3
    CRITICAL:root:WITHIN _end_span CHILD SPAN ENDED for child_id: 9d5cd4b4-b24a-485f-8958-259aaa760e21 | child_span trace_id: f3bfcfd49f1a9db45b2d2a18184b984a | child_span span_id: 5cc78e90ea667195
    CRITICAL:root:WITHIN _end_span CHILD SPAN ENDED for child_id: 5668f760-ea90-4c55-8d7b-0c9d4537daa1 | child_span trace_id: f3bfcfd49f1a9db45b2d2a18184b984a | child_span span_id: 7dd18383c6db01ca
    CRITICAL:root:WITHIN _end_span CHILD SPAN ENDED for child_id: c857faf6-40f8-4121-8e80-5eabf6dcd286 | child_span trace_id: f3bfcfd49f1a9db45b2d2a18184b984a | child_span span_id: 2e38eea3a47b9d11
    CRITICAL:root:WITHIN _end_span for trace_id: f3bfcfd49f1a9db45b2d2a18184b984a | span_id: 50e5ffc06f909133
    CRITICAL:root:WITHIN _end_span CHILD SPAN ENDED for child_id: f5e1470b-c7eb-4cd3-bead-edc201b51d3b | child_span trace_id: f3bfcfd49f1a9db45b2d2a18184b984a | child_span span_id: 577828c0fd693b27
    CRITICAL:root:WITHIN _end_span CHILD SPAN ENDED for child_id: ce79221c-0da6-4d95-b45e-080963b2a77d | child_span trace_id: f3bfcfd49f1a9db45b2d2a18184b984a | child_span span_id: a194be5749fdc4b3
    

    However, in our app, we don't see this behaviour. We only see the _end_span called on every decide_tool.task span. Something tells me ChannelWrite may have a role to play in this bug, but not sure.

    CRITICAL:root:WITHIN _create_span trace_id: 99ceadc2c97ae0fc4af68fd9ec816bdc | span_id: 2822f12f8adb2613 | span_name: LangGraph.workflow
    CRITICAL:root:WITHIN _create_span trace_id: 99ceadc2c97ae0fc4af68fd9ec816bdc | span_id: 9909154384f212d0 | span_name: __start__.task
    CRITICAL:root:WITHIN _create_span trace_id: 99ceadc2c97ae0fc4af68fd9ec816bdc | span_id: ae069913bfaaca00 | span_name: initial_plan.task
    CRITICAL:root:WITHIN _create_span trace_id: 99ceadc2c97ae0fc4af68fd9ec816bdc | span_id: aa8f01ae8e198c15 | 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
    CRITICAL:root:WITHIN _create_span trace_id: 99ceadc2c97ae0fc4af68fd9ec816bdc | span_id: 2014165abbdd34c9 | span_name: planner.task
    CRITICAL:root:WITHIN _create_span trace_id: 99ceadc2c97ae0fc4af68fd9ec816bdc | span_id: 3c1a1b74589bbb24 | span_name: ChannelWrite<planner,notebook,job_status,messages,customer_id,operation,instruction,task_id,runbook,last_tool_call,start_time,focused_block_id>.task
    CRITICAL:root:WITHIN _create_span trace_id: 99ceadc2c97ae0fc4af68fd9ec816bdc | span_id: be8b9bb4436e7356 | span_name: decide_tool.task
    CRITICAL:root:WITHIN _end_span for trace_id: 99ceadc2c97ae0fc4af68fd9ec816bdc | span_id: be8b9bb4436e7356
    CRITICAL:root:WITHIN _create_span trace_id: 99ceadc2c97ae0fc4af68fd9ec816bdc | span_id: 9601f1780e5de33c | span_name: ChannelWrite<branch:planner:decide_tool:observe_agent>.task
    CRITICAL:root:WITHIN _create_span trace_id: 99ceadc2c97ae0fc4af68fd9ec816bdc | span_id: fb5ac20b5c17d3e4 | span_name: observe_agent.task
    CRITICAL:root:WITHIN _create_span trace_id: 99ceadc2c97ae0fc4af68fd9ec816bdc | span_id: f0ea8400316a1734 | 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
    CRITICAL:root:WITHIN _create_span trace_id: 99ceadc2c97ae0fc4af68fd9ec816bdc | span_id: 3393d766a384179b | span_name: planner.task
    CRITICAL:root:WITHIN _create_span trace_id: 99ceadc2c97ae0fc4af68fd9ec816bdc | span_id: 94ea6ce92ebc2317 | span_name: ChannelWrite<planner,notebook,job_status,messages,customer_id,operation,instruction,task_id,runbook,last_tool_call,start_time,focused_block_id>.task
    CRITICAL:root:WITHIN _create_span trace_id: 99ceadc2c97ae0fc4af68fd9ec816bdc | span_id: e2ffd3a69c36f428 | span_name: decide_tool.task
    CRITICAL:root:WITHIN _end_span for trace_id: 99ceadc2c97ae0fc4af68fd9ec816bdc | span_id: e2ffd3a69c36f428
    CRITICAL:root:WITHIN _create_span trace_id: 99ceadc2c97ae0fc4af68fd9ec816bdc | span_id: 57264e686823d0ef | span_name: ChannelWrite<branch:planner:decide_tool:code_agent>.task
    CRITICAL:root:WITHIN _create_span trace_id: 99ceadc2c97ae0fc4af68fd9ec816bdc | span_id: 4eda926a6103c8cc | span_name: code_agent.task
    CRITICAL:root:WITHIN _create_span trace_id: 99ceadc2c97ae0fc4af68fd9ec816bdc | span_id: 53a74def2d8c86aa | 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
    CRITICAL:root:WITHIN _create_span trace_id: 99ceadc2c97ae0fc4af68fd9ec816bdc | span_id: 23774c355c055bdf | span_name: planner.task
    CRITICAL:root:WITHIN _create_span trace_id: 99ceadc2c97ae0fc4af68fd9ec816bdc | span_id: 903d76095c504117 | span_name: ChannelWrite<planner,notebook,job_status,messages,customer_id,operation,instruction,task_id,runbook,last_tool_call,start_time,focused_block_id>.task
    CRITICAL:root:WITHIN _create_span trace_id: 99ceadc2c97ae0fc4af68fd9ec816bdc | span_id: 962304b5041bd562 | span_name: decide_tool.task
    CRITICAL:root:WITHIN _end_span for trace_id: 99ceadc2c97ae0fc4af68fd9ec816bdc | span_id: 962304b5041bd562
    
  4. changed the title [-]🐛 Bug Report: Missing span exports in LangChain instrumentation[/-] [+]🐛 Bug Report: Langchain instrumentation does not end spans when output contains Date fields[/+] on Mar 20, 2025
  5. obs-gh-abhishekrao commented on Mar 20, 2025

    @obs-gh-abhishekrao
    ContributorAuthor

    Hi @nirga, I found the root cause of this issue.
    The problem happens here

    Our outputs contain a date field which is not handled by CallbackFilteredJSONEncoder. Through debugging, when we mutated outputs to drop that field before it's passed to the encoder, the spans ended correctly.

    So I think adding some logic for date types to CallbackFilteredJSONEncoder will be the solution here. I tried something like this and it worked, but it likely needs more care:

    if isinstance(o, datetime.datetime):
        return o.strftime("%Y%m%d_%H%M%S")
    

    Updated title to be more precise.

  6. nirga commented on Mar 22, 2025

    @nirga
    Member

    Thanks @obs-gh-abhishekrao! Creating a PR now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions