Skip to content

Python: Backfill chat span request model if it's unknown and response model is avaliable#6160

Merged
TaoChenOSU merged 1 commit into
mainfrom
feature/python-backfill-request-mode-in-chat-span
May 28, 2026
Merged

Python: Backfill chat span request model if it's unknown and response model is avaliable#6160
TaoChenOSU merged 1 commit into
mainfrom
feature/python-backfill-request-mode-in-chat-span

Conversation

@TaoChenOSU

@TaoChenOSU TaoChenOSU commented May 28, 2026

Copy link
Copy Markdown
Contributor

Motivation and Context

Addresses #5088

Description

Backfill the request model in the chat span if it's not set while the response contains the model.
image

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

@TaoChenOSU TaoChenOSU self-assigned this May 28, 2026
@TaoChenOSU TaoChenOSU added the python Usage: [Issues, PRs], Target: Python label May 28, 2026
Copilot AI review requested due to automatic review settings May 28, 2026 17:23
@TaoChenOSU TaoChenOSU added the observability Usage: [Issues, PRs], Target: observability related features label May 28, 2026
@github-actions github-actions Bot changed the title Backfill chat span request model if it's unknown and response model is avaliable Python: Backfill chat span request model if it's unknown and response model is avaliable May 28, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Improves Python OpenTelemetry chat span telemetry by backfilling gen_ai.request.model (and the span name) when the request model is initially unknown but the response includes a resolved model, addressing issue #5088’s “unknown request model” gap.

Changes:

  • Add ChatTelemetryLayer._backfill_request_model(...) to update REQUEST_MODEL and span name from RESPONSE_MODEL when the request model is "unknown".
  • Invoke the backfill step for both streaming and non-streaming chat responses before capturing response attributes.
  • Add unit tests covering the helper behavior and end-to-end chat client telemetry updates (streaming + non-streaming).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
python/packages/core/agent_framework/observability.py Adds request-model backfill helper and wires it into chat response capture paths.
python/packages/core/tests/core/test_observability.py Adds coverage for backfill helper and chat-client telemetry behavior.
python/packages/foundry/agent_framework_foundry/_agent.py Updates a Foundry-related doc example import/usage (documentation-only change).
Comments suppressed due to low confidence (1)

python/packages/foundry/agent_framework_foundry/_agent.py:520

  • The example in this docstring instantiates FoundryAgent and then passes it as client into Agent(...). FoundryAgent is itself an agent (it already has run(...) and owns an internal chat client), so this example is misleading/incorrect. Update the example to either (a) use FoundryAgent directly (agent = FoundryAgent(...); await agent.run(...)) or (b) show how to create a chat client (RawFoundryAgentChatClient / _FoundryAgentChatClient) and wrap it with Agent(client=...).
            from agent_framework import Agent
            from agent_framework.foundry import FoundryAgent
            from azure.identity import AzureCliCredential

            client = FoundryAgent(
                project_endpoint="https://your-project.services.ai.azure.com",
                agent_name="my-prompt-agent",
                agent_version="1",
                credential=AzureCliCredential(),
            )

            agent = Agent(client=client, tools=[my_function_tool])

@moonbox3

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework
   observability.py8266092%382, 384–385, 388, 391, 394–395, 400–401, 407–408, 414–415, 422, 424–426, 429–431, 436–437, 443–444, 450–451, 458, 615–616, 815, 819–821, 823, 827–828, 832, 870, 872, 883–885, 887–889, 893, 901, 1025–1026, 1261, 1521–1522, 1750, 1791–1792, 1935, 2071, 2268, 2486, 2488
packages/foundry/agent_framework_foundry
   _agent.py2395676%121, 124, 243–244, 248–250, 255–258, 348, 421–422, 434–435, 447–449, 451–452, 454–460, 462–463, 465, 467, 473–475, 478–487, 491–492, 682–683, 686, 712, 722, 738, 808, 813, 817
TOTAL36865431888% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
7393 34 💤 0 ❌ 0 🔥 1m 54s ⏱️

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Automated Code Review

Reviewers: 3 | Confidence: 91% | Result: All clear

Reviewed: Correctness, Test Coverage, Design Approach


Automated review by TaoChenOSU's agents

@TaoChenOSU TaoChenOSU marked this pull request as ready for review May 28, 2026 17:43

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Automated Code Review

Reviewers: 4 | Confidence: 92% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Design Approach


Automated review by TaoChenOSU's agents

@TaoChenOSU TaoChenOSU added this pull request to the merge queue May 28, 2026
Merged via the queue into main with commit 0578f4c May 28, 2026
43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

observability Usage: [Issues, PRs], Target: observability related features python Usage: [Issues, PRs], Target: Python

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants