Skip to content

langchain: gen_ai.conversation.id is set only on invoke_agent spans #475

Description

@sfc-gh-zeningchen

Describe your environment

OS: Ubuntu 22.04
Python version: Python 3.12
Package version: opentelemetry-instrumentation-genai-langchain 1.1b1, opentelemetry-util-genai 1.1b0 (also reproduces on main)
GenAI library (e.g. anthropic, openai) and version: langchain 1.3.16, langchain-openai 1.4.3

What happened?

The LangChain instrumentation reads a conversation identifier from the
thread_id, session_id, or conversation_id metadata keys, but only applies
it to invoke_agent spans. The chat and invoke_workflow spans in the same
trace have no gen_ai.conversation.id, even though semconv defines the
attribute as conditionally required on all three.
WorkflowInvocation in opentelemetry-util-genai has no conversation_id
field at all, so an invoke_workflow span cannot carry one even when the
application supplies it.
Separately, AgentInvocation applies the attribute from
_get_request_attributes, which runs when the span finishes. The attribute is
absent for the whole life of the span, so a SpanProcessor.on_start cannot see
it and a child span cannot read it from its parent.

Steps to Reproduce

examples/agent/main.py in the langchain package already reproduces this — it
sets session_id in the agent's metadata:

agent = create_react_agent(llm, tools=[multiply, add]).with_config(
    {
        "metadata": {
            "agent_name": "coordinator",
            "session_id": session_id,
        },
    }
)

Run it against any exporter and inspect the resulting spans, or swap the OTLP
exporters for an InMemorySpanExporter and print gen_ai.conversation.id for
each span.

Expected Result

Every span in the trace that semconv defines the attribute for carries it — the
invoke_agent span, the chat spans for the model calls it makes, and the
invoke_workflow span if the run is classified as a workflow.

Actual Result

Only the invoke_agent span carries gen_ai.conversation.id. The chat spans
and any invoke_workflow span have no such attribute.

Additional context

No response

Would you like to implement a fix?

Yes

Tip

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status
    New issues

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions