Skip to content

Commit 3337639

Browse files
committed
fix(extensions): record litellm truncation refusal before span output
1 parent 57152a4 commit 3337639

2 files changed

Lines changed: 57 additions & 14 deletions

File tree

‎src/agents/extensions/models/litellm_model.py‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -302,19 +302,6 @@ async def get_response(
302302
usage = Usage(requests=1)
303303
logger.warning("No usage information returned from Litellm")
304304

305-
if tracing.include_data():
306-
span_generation.span_data.output = (
307-
[message.model_dump()] if message is not None else []
308-
)
309-
span_generation.span_data.usage = {
310-
"requests": usage.requests,
311-
"input_tokens": usage.input_tokens,
312-
"output_tokens": usage.output_tokens,
313-
"total_tokens": usage.total_tokens,
314-
"input_tokens_details": usage.input_tokens_details.model_dump(),
315-
"output_tokens_details": usage.output_tokens_details.model_dump(),
316-
}
317-
318305
# Surface content-filter refusals explicitly. Some providers (e.g.
319306
# Anthropic on Amazon Bedrock) signal a safety block only via
320307
# ``finish_reason == "content_filter"`` with an empty message and no
@@ -324,7 +311,9 @@ async def get_response(
324311
# refusal so downstream handling (ResponseOutputRefusal) fires.
325312
# A completion truncated before any visible token (``finish_reason ==
326313
# "length"``) has the same shape and must not collapse into an empty
327-
# turn either.
314+
# turn either. This runs before the span output is recorded so the
315+
# synthesized refusal is reflected in tracing; a provider-supplied
316+
# refusal (carried in ``provider_specific_fields``) is left as-is.
328317
if (
329318
message is not None
330319
and first_choice is not None
@@ -343,6 +332,19 @@ async def get_response(
343332
provider_specific_fields["refusal"] = refusal_message
344333
message.provider_specific_fields = provider_specific_fields
345334

335+
if tracing.include_data():
336+
span_generation.span_data.output = (
337+
[message.model_dump()] if message is not None else []
338+
)
339+
span_generation.span_data.usage = {
340+
"requests": usage.requests,
341+
"input_tokens": usage.input_tokens,
342+
"output_tokens": usage.output_tokens,
343+
"total_tokens": usage.total_tokens,
344+
"input_tokens_details": usage.input_tokens_details.model_dump(),
345+
"output_tokens_details": usage.output_tokens_details.model_dump(),
346+
}
347+
346348
# Build provider_data for provider specific fields
347349
provider_data: dict[str, Any] = {"model": self.model}
348350
if message is not None and hasattr(response, "id"):

‎tests/models/test_litellm_content_filter.py‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
from litellm.types.utils import Choices, Message, ModelResponse, Usage
44
from openai.types.responses import ResponseOutputMessage, ResponseOutputRefusal
55

6+
from agents import trace
67
from agents.extensions.models.litellm_model import LitellmModel
78
from agents.model_settings import ModelSettings
89
from agents.models.interface import ModelTracing
10+
from tests.testing_processor import fetch_ordered_spans
911

1012

1113
async def _get_response(monkeypatch, *, finish_reason, content):
@@ -111,6 +113,45 @@ async def test_length_does_not_clobber_real_content(monkeypatch):
111113
assert not refusals, "should not synthesize a refusal when content is present"
112114

113115

116+
@pytest.mark.allow_call_model_methods
117+
@pytest.mark.asyncio
118+
async def test_length_finish_reason_refusal_recorded_in_trace(monkeypatch) -> None:
119+
"""The synthesized truncation refusal must be recorded in the generation span
120+
output (the synthesis happens before span_data.output is captured), matching
121+
the non-streaming openai_chatcompletions tracing behavior."""
122+
async def fake_acompletion(model, messages=None, **kwargs):
123+
msg = Message(role="assistant", content=None)
124+
choice = Choices(index=0, finish_reason="length", message=msg)
125+
return ModelResponse(choices=[choice], usage=Usage(0, 0, 0))
126+
127+
monkeypatch.setattr(litellm, "acompletion", fake_acompletion)
128+
model = LitellmModel(model="test-model")
129+
with trace(workflow_name="litellm-length-truncation"):
130+
await model.get_response(
131+
system_instructions=None,
132+
input=[],
133+
model_settings=ModelSettings(),
134+
tools=[],
135+
output_schema=None,
136+
handoffs=[],
137+
tracing=ModelTracing.ENABLED,
138+
previous_response_id=None,
139+
)
140+
141+
generation_spans = [
142+
span for span in fetch_ordered_spans() if span.span_data.type == "generation"
143+
]
144+
assert len(generation_spans) == 1
145+
exported_span = generation_spans[0].export()
146+
assert exported_span is not None
147+
output = exported_span["span_data"]["output"]
148+
assert output
149+
provider_fields = output[0].get("provider_specific_fields") or {}
150+
assert provider_fields.get("refusal") == (
151+
"Response truncated because the provider's maximum token limit was reached."
152+
)
153+
154+
114155
@pytest.mark.allow_call_model_methods
115156
@pytest.mark.asyncio
116157
async def test_normal_stop_is_unaffected(monkeypatch):

0 commit comments

Comments
 (0)