|
34 | 34 | ResponseReasoningItem, |
35 | 35 | ) |
36 | 36 |
|
37 | | -from agents import Agent, Runner, function_tool |
| 37 | +from agents import Agent, Runner, function_tool, trace |
38 | 38 | from agents.exceptions import ModelBehaviorError, UserError |
39 | 39 | from agents.model_settings import ModelSettings |
40 | 40 | from agents.models.chatcmpl_converter import Converter |
|
50 | 50 | from agents.models.interface import ModelTracing |
51 | 51 | from agents.models.openai_chatcompletions import OpenAIChatCompletionsModel |
52 | 52 | from agents.models.openai_provider import OpenAIProvider |
| 53 | +from tests.testing_processor import fetch_ordered_spans |
53 | 54 | from tests.utils.simple_session import SimpleListSession |
54 | 55 |
|
55 | 56 |
|
@@ -3995,3 +3996,40 @@ async def test_streamed_run_does_not_double_count_when_usage_is_present(monkeypa |
3995 | 3996 |
|
3996 | 3997 | assert result.context_wrapper.usage.requests == 1 |
3997 | 3998 | assert result.context_wrapper.usage.total_tokens == 12 |
| 3999 | + |
| 4000 | + |
| 4001 | +@pytest.mark.allow_call_model_methods |
| 4002 | +@pytest.mark.asyncio |
| 4003 | +async def test_streamed_span_records_the_request_when_provider_omits_usage(monkeypatch) -> None: |
| 4004 | + """Streamed tracing must record the request the same way the non-streaming path does. |
| 4005 | +
|
| 4006 | + Non-streaming writes a span usage object with `requests: 1` when the provider reports no |
| 4007 | + usage. Streaming used to omit span usage entirely, so the run reported one request while |
| 4008 | + the model span showed none. |
| 4009 | + """ |
| 4010 | + monkeypatch.setattr( |
| 4011 | + OpenAIChatCompletionsModel, "_fetch_response", _usageless_stream_patch(usage=None) |
| 4012 | + ) |
| 4013 | + model = OpenAIProvider(use_responses=False).get_model("gpt-4") |
| 4014 | + |
| 4015 | + with trace(workflow_name="test"): |
| 4016 | + async for _ in model.stream_response( |
| 4017 | + system_instructions=None, |
| 4018 | + input="", |
| 4019 | + model_settings=ModelSettings(), |
| 4020 | + tools=[], |
| 4021 | + output_schema=None, |
| 4022 | + handoffs=[], |
| 4023 | + tracing=ModelTracing.ENABLED, |
| 4024 | + previous_response_id=None, |
| 4025 | + conversation_id=None, |
| 4026 | + prompt=None, |
| 4027 | + ): |
| 4028 | + pass |
| 4029 | + |
| 4030 | + spans = fetch_ordered_spans() |
| 4031 | + generation = next(s for s in spans if s.span_data.type == "generation") |
| 4032 | + assert generation.span_data.usage is not None |
| 4033 | + assert generation.span_data.usage["requests"] == 1 |
| 4034 | + # The provider reported no tokens, so every total stays at zero. |
| 4035 | + assert generation.span_data.usage["total_tokens"] == 0 |
0 commit comments