fix(openai): only fold tool-call finish reasons to stop in json_mode

The previous change folded every json_mode tool call to finish_reason
"stop", which rewrote a provider "length" reason to "stop" and hid
truncated output from callers. Map only "tool_calls" and
"function_call" to "stop" and pass every other reason through, and
parametrize the regression test over tool_calls, function_call, length
and stop.

Signed-off-by: Saylee <saylee21@users.noreply.github.com>
This commit is contained in:
Saylee 2026-10-12 22:40:05 +05:30
parent 92fbe7698b
commit 5e65f314eb
2 changed files with 17 additions and 6 deletions

View file

@ -793,7 +793,7 @@ class OpenAIGPTConfig(BaseLLMModelInfo, BaseConfig):
json_mode_content_str: str | None = str(new_tool_calls[0]["function"].get("arguments", "")) or None
if json_mode_content_str is not None:
translated_message = Message(content=json_mode_content_str)
finish_reason = "stop"
finish_reason = "stop" if choice["finish_reason"] in ("tool_calls", "function_call") else choice["finish_reason"]
if translated_message is None:
## get the reasoning content

View file

@ -2217,12 +2217,23 @@ async def test_chat_sends_a_breakpoint_by_the_deployments_own_flag_not_a_sibling
}
def test_transform_choices_json_mode_reports_stop_when_tool_call_becomes_content():
"""json_mode turns a tool call into JSON content; the choice must then report
finish_reason 'stop' instead of 'tool_calls' with no tool_calls attached."""
@pytest.mark.parametrize(
"provider_finish_reason, expected_finish_reason",
[
("tool_calls", "stop"),
("function_call", "stop"),
("length", "length"),
("stop", "stop"),
],
)
def test_transform_choices_json_mode_reports_stop_only_for_tool_call_reasons(
provider_finish_reason, expected_finish_reason
):
"""json_mode folds a tool call into JSON content. Only tool-call reasons become
'stop'; a truncated ('length') or already-final reason must survive untouched."""
choice = [
{
"finish_reason": "tool_calls",
"finish_reason": provider_finish_reason,
"index": 0,
"logprobs": None,
"message": {
@ -2243,4 +2254,4 @@ def test_transform_choices_json_mode_reports_stop_when_tool_call_becomes_content
assert out[0].message.content == '{"a": 1}'
assert out[0].message.tool_calls is None
assert out[0].finish_reason == "stop"
assert out[0].finish_reason == expected_finish_reason