Skip to content

Commit 12504f6

Browse files
committed
test: update empty output test for finish_reason-aware handling
The existing test used finish_reason="stop" which now returns gracefully. Change it to finish_reason="length" to keep testing the error path, and add a new test verifying that finish_reason="stop" with empty content returns a valid response (GPT-5 behavior).
1 parent ff713c4 commit 12504f6

1 file changed

Lines changed: 40 additions & 1 deletion

File tree

tests/test_openai_source.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,7 @@ async def fake_create(**kwargs):
11421142

11431143
@pytest.mark.asyncio
11441144
async def test_parse_openai_completion_raises_empty_model_output_error():
1145+
"""Empty output with non-stop finish_reason should raise."""
11451146
provider = _make_provider()
11461147
try:
11471148
completion = ChatCompletion.model_validate(
@@ -1159,7 +1160,7 @@ async def test_parse_openai_completion_raises_empty_model_output_error():
11591160
"refusal": None,
11601161
"tool_calls": None,
11611162
},
1162-
"finish_reason": "stop",
1163+
"finish_reason": "length",
11631164
}
11641165
],
11651166
"usage": {
@@ -1176,6 +1177,44 @@ async def test_parse_openai_completion_raises_empty_model_output_error():
11761177
await provider.terminate()
11771178

11781179

1180+
@pytest.mark.asyncio
1181+
async def test_parse_openai_completion_empty_content_finish_stop_no_error():
1182+
"""Empty output with finish_reason=stop should return gracefully (GPT-5 etc.)."""
1183+
provider = _make_provider()
1184+
try:
1185+
completion = ChatCompletion.model_validate(
1186+
{
1187+
"id": "chatcmpl-empty-stop",
1188+
"object": "chat.completion",
1189+
"created": 0,
1190+
"model": "gpt-5.4",
1191+
"choices": [
1192+
{
1193+
"index": 0,
1194+
"message": {
1195+
"role": "assistant",
1196+
"content": None,
1197+
"refusal": None,
1198+
"tool_calls": None,
1199+
},
1200+
"finish_reason": "stop",
1201+
}
1202+
],
1203+
"usage": {
1204+
"prompt_tokens": 13,
1205+
"completion_tokens": 18,
1206+
"total_tokens": 31,
1207+
},
1208+
}
1209+
)
1210+
1211+
resp = await provider._parse_openai_completion(completion, tools=None)
1212+
assert resp.role == "assistant"
1213+
assert resp.result_chain is not None
1214+
finally:
1215+
await provider.terminate()
1216+
1217+
11791218
@pytest.mark.asyncio
11801219
async def test_query_stream_extracts_usage_from_empty_choices_chunk(monkeypatch):
11811220
provider = _make_provider()

0 commit comments

Comments
 (0)