Skip to content

Python: [Bug]: Empty usage and finish_reason when a response is truncated at max_output_tokens #7051

Description

@CristinaStn

Description

When a streaming run through Agent -> FoundryChatClient is truncated because it hit max_output_tokens (max_tokens in ChatOptions), the assembled AgentResponse / ChatResponse comes back with usage_details is None and finish_reason is None.

Expected:

  • usage_details populated from the terminal event's response.usage,
  • finish_reason == "length" (or an equivalent truncation signal).

Environment

  • agent-framework == 1.11.0, openai==2.45.0
  • Client: FoundryChatClient (Azure AI Foundry project endpoint), stream=True
  • Options: ChatOptions(max_tokens=1000) (translated to max_output_tokens on the Responses API)

Code Sample

import asyncio

from agent_framework import Agent, ChatOptions, Content, Message
from agent_framework_foundry import FoundryChatClient


async def main() -> None:
    client = FoundryChatClient(...)  # Azure AI Foundry project endpoint
    agent = Agent(client=client, name="TEST", instructions="You are helpful.")

    message = [
        Message(
            role="user",
            contents=[
                Content.from_text(
                    "Write an extremely long, detailed essay about the history of pizza."
                )
            ],
        )
    ]

    # Cap the output low so the model is truncated mid-answer.
    stream = agent.run(message, stream=True, options=ChatOptions(max_tokens=1000))
    async for _update in stream:
        pass

    final = await stream.get_final_response()

    print("text tail :", repr(final.text[-60:]))   # truncated mid-sentence
    print("usage     :", final.usage_details)       # -> None  (BUG: usage lost)
    print("finish    :", final.finish_reason)        # -> None  (BUG: no "length")


asyncio.run(main())

Error Messages / Stack Traces

Package Versions

agent-framework-core = "1.11.0"; agent-framework-foundry = "1.10.0"; agent-framework == 1.11.0; openai==2.45.0

Python Version

Python 3.11.0

Additional Context

No response

Activity

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

Metadata

Metadata

Labels

agentsUsage: [Issues, PRs], Target: Single agentpythonUsage: [Issues, PRs], Target: PythonreproducedUsage: [Issues], Target: all issues that can be reproduced by the triage workflow

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions