Skip to content

pydantic_ai integration: auto-populate LLMObs session_id from conversation_id #19237

Description

@mlorthiois

Package Name

pydantic_ai

Package Version(s)

No response

Describe the goal of the feature

The pydantic_ai LLMObs integration (ddtrace/contrib/internal/pydantic_ai/patch.py) traces Agent.iter and
Agent.run_stream, both of which receive an optional conversation_id: str | None keyword argument (see
pydantic_ai/agent/__init__.py, Agent.iter signature). This conversation_id is exactly the semantic equivalent of LLMObs' session_id: it groups multiple agent runs that belong to the same end-user conversation/thread.

Today, traced_agent_iter and traced_agent_run_stream never read this argument, so it's silently dropped.

The only way to get session correlation in LLM Observability is to wrap the call in an explicit
LLMObs.workflow(session_id=...) / LLMObs.agent(session_id=...) context manager, duplicating a value the
integration already has in kwargs at interception time:

  def traced_agent_run_stream(func, instance, args, kwargs):
      integration = pydantic_ai._datadog_integration
      integration._run_stream_active = True
      span = integration.trace(
          _agent_resource(instance),
          span_name="pydantic_ai.agent",
          submit_to_llmobs=True,
          model=getattr(instance, "model", None),
          kind="agent",
      )
      ...

A solution: When conversation_id is present in kwargs for Agent.iter / Agent.run_stream, pass it through to _annotate_llmobs_span_data as session_id, e.g. in PydanticAIIntegration._llmobs_set_tags_agent :

  conversation_id = kwargs.get("conversation_id")
  _annotate_llmobs_span_data(
      span,
      name=agent_name or "PydanticAI Agent",
      input_value=user_prompt,
      output_value=result,
      session_id=conversation_id,
  )

Is your feature request related to a problem?

No response

Describe alternatives you've considered

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions