Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions chatmock/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ def _merge_from(src):
],
}
yield f"data: {json.dumps(finish_chunk)}\n\n".encode("utf-8")
sent_stop_chunk = True
except Exception:
pass

Expand Down Expand Up @@ -682,6 +683,7 @@ def _merge_from(src):
"choices": [{"index": 0, "delta": {}, "finish_reason": "tool_calls"}],
}
yield f"data: {json.dumps(finish_chunk)}\n\n".encode("utf-8")
sent_stop_chunk = True
elif kind == "response.reasoning_summary_part.added":
if compat in ("think-tags", "o3"):
if saw_any_summary:
Expand Down
61 changes: 61 additions & 0 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,67 @@ def test_chat_completions(self, mock_start) -> None:
self.assertEqual(body["choices"][0]["message"]["content"], "hello")
self.assertEqual(body["model"], "gpt5.4-mini")

@patch("chatmock.routes_openai.start_upstream_request")
def test_chat_completions_stream_single_finish_reason_with_tool_call(self, mock_start) -> None:
"""A streamed response that mixes assistant text with a function call must
emit exactly one finish_reason ("tool_calls"), not a trailing "stop" chunk.

Clients that honor the last finish_reason otherwise treat the response as a
plain text turn and drop the accumulated tool calls entirely.
"""
mock_start.return_value = (
FakeUpstream(
[
{"type": "response.output_text.delta", "delta": "Let me look that up."},
{
"type": "response.output_item.done",
"item": {
"type": "function_call",
"call_id": "call_1",
"name": "get_weather",
"arguments": "{\"city\": \"Seoul\"}",
},
},
{"type": "response.completed", "response": {"id": "resp-tool-stream"}},
]
),
None,
)
response = self.client.post(
"/v1/chat/completions",
json={
"model": "gpt-5.4",
"messages": [{"role": "user", "content": "weather in seoul?"}],
"stream": True,
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
},
},
}
],
},
)
self.assertEqual(response.status_code, 200)
finish_reasons: list[str] = []
saw_tool_call_delta = False
for line in response.get_data(as_text=True).splitlines():
if not line.startswith("data: ") or line == "data: [DONE]":
continue
chunk = json.loads(line[len("data: ") :])
for choice in chunk.get("choices", []):
if choice.get("finish_reason"):
finish_reasons.append(choice["finish_reason"])
if (choice.get("delta") or {}).get("tool_calls"):
saw_tool_call_delta = True
self.assertTrue(saw_tool_call_delta)
self.assertEqual(finish_reasons, ["tool_calls"])

@patch("chatmock.routes_openai.start_upstream_request")
def test_chat_completions_honors_debug_model_override(self, mock_start) -> None:
app = create_app(debug_model="gpt-5.4")
Expand Down