Skip to content

Genai utils | Add AgentInvocation type#4274

Open
etserend wants to merge 6 commits intoopen-telemetry:mainfrom
etserend:genai-utils/agent-invocation
Open

Genai utils | Add AgentInvocation type#4274
etserend wants to merge 6 commits intoopen-telemetry:mainfrom
etserend:genai-utils/agent-invocation

Conversation

@etserend
Copy link

Description

This PR adds the AgentInvocation type extending _BaseAgent for agent invocation (invoke_agent) spans. It provides TelemetryHandler lifecycle methods and an agent() context manager — aligned with the GenAI agent span semantic conventions.

Builds on the _BaseAgent base class and AgentCreation type added in #4217. Agent metrics and events to come in follow-up PRs.

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

  • Basic unit tests for starting and stopping agent invocation spans
  • Unit tests for agent invocation with all attributes (request + response)
  • Unit tests for context manager happy path and error path
  • Unit tests for AgentInvocation dataclass defaults and messages

Does This PR Require a Core Repo Change?

  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@etserend etserend force-pushed the genai-utils/agent-invocation branch from 1b5cd9f to 08e2b07 Compare February 27, 2026 17:45
@etserend etserend changed the title Genai utils/agent invocation Genai utils/Add AgentInvocation type Mar 2, 2026
@etserend etserend changed the title Genai utils/Add AgentInvocation type Genai utils | Add AgentInvocation type Mar 2, 2026
@etserend
Copy link
Author

etserend commented Mar 2, 2026

Sample trace from [demo app](Trace: b6db19ce1c2846667d1b237c4e264a0a

Trace: b6db19ce1c2846667d1b237c4e264a0a

invoke_agent Poet Agent (1.970s, Client)
├── gen_ai.operation.name: invoke_agent
├── gen_ai.agent.name: Poet Agent
├── gen_ai.agent.description: Writes short poems
├── gen_ai.provider.name: openai
├── gen_ai.request.model: gpt-4o-mini
├── gen_ai.response.model: gpt-4o-mini-2024-07-18
├── gen_ai.response.id: chatcmpl-DF5wUAWLGG75qrEFwE5QKlB40QA85
├── gen_ai.usage.input_tokens: 15
├── gen_ai.usage.output_tokens: 19
├── server.address: api.openai.com
├── server.port: 443
│
└── chat gpt-4o-mini (1.950s, Client)
    ├── gen_ai.operation.name: chat
    ├── gen_ai.request.model: gpt-4o-mini
    ├── gen_ai.response.model: gpt-4o-mini-2024-07-18
    ├── gen_ai.response.id: chatcmpl-DF5wUAWLGG75qrEFwE5QKlB40QA85
    ├── gen_ai.system: openai
    ├── gen_ai.usage.input_tokens: 15
    ├── gen_ai.usage.output_tokens: 19
    └── server.address: api.openai.com

@etserend etserend marked this pull request as ready for review March 2, 2026 22:23
@etserend etserend requested a review from a team as a code owner March 2, 2026 22:23
@tammy-baylis-swi tammy-baylis-swi moved this to Ready for review in Python PR digest Mar 3, 2026
response_id: str | None = None
finish_reasons: list[str] | None = None
input_tokens: int | None = None
output_tokens: int | None = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm starting to see a lot of duplicated properties on types. Wondering if we can consolidate some of these into reusable bits.

Comment on lines +432 to +444
# System instructions (Opt-In)
if (
is_experimental_mode()
and get_content_capturing_mode()
in (
ContentCapturingMode.SPAN_ONLY,
ContentCapturingMode.SPAN_AND_EVENT,
)
and creation.system_instruction
):
attributes[GenAI.GEN_AI_SYSTEM_INSTRUCTIONS] = gen_ai_json_dumps(
[asdict(p) for p in creation.system_instruction]
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can probably be a helper to be used with agent/llm/tool invocations

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

Labels

None yet

Projects

Status: Ready for review

Development

Successfully merging this pull request may close these issues.

4 participants