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())
Description
When a streaming run through
Agent->FoundryChatClientis truncated because it hitmax_output_tokens(max_tokensinChatOptions), the assembledAgentResponse/ChatResponsecomes back withusage_details is Noneandfinish_reason is None.Expected:
usage_detailspopulated from the terminal event'sresponse.usage,finish_reason == "length"(or an equivalent truncation signal).Environment
agent-framework==1.11.0,openai==2.45.0FoundryChatClient(Azure AI Foundry project endpoint),stream=TrueChatOptions(max_tokens=1000)(translated tomax_output_tokenson 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.0Python Version
Python 3.11.0
Additional Context
No response