Skip to content

fix(usage): error usage format in streaming #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/agents/models/openai_chatcompletions.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,20 +405,30 @@ async def stream_response(
for function_call in state.function_calls.values():
outputs.append(function_call)

final_response = response.model_copy(update={"output": outputs, "usage": usage})
final_usage = (
Usage(
requests=1,
input_tokens=usage.prompt_tokens,
output_tokens=usage.completion_tokens,
total_tokens=usage.total_tokens,
)
if usage
else Usage()
)
final_response = response.model_copy(update={"output": outputs, "usage": final_usage})

yield ResponseCompletedEvent(
response=final_response,
type="response.completed",
)

if tracing.include_data():
span_generation.span_data.output = [final_response.model_dump()]
span_generation.span_data.output = [output.model_dump() for output in outputs]

if usage:
span_generation.span_data.usage = {
"input_tokens": usage.prompt_tokens,
"output_tokens": usage.completion_tokens,
}
span_generation.span_data.usage = {
"input_tokens": final_usage.input_tokens,
"output_tokens": final_usage.output_tokens,
}

@overload
async def _fetch_response(
Expand Down