From 773bdff8a87405cf680e08e9672e555cfabe8e58 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 12 Jun 2026 06:00:47 +0000 Subject: [PATCH] refactor(translation): extract _tool_event_step from the stream fold The merged step() carries both branches' new event arms (wire_chunk + chunk), tipping C901 to 11. Pull the two tool arms into one helper via an or-pattern case; the helper narrows on the two-literal tag so no placeholder arm is needed. --- .../translation/inbound/openai_chat/stream.py | 78 ++++++++++--------- 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/litellm/translation/inbound/openai_chat/stream.py b/litellm/translation/inbound/openai_chat/stream.py index 62cb12335e..6ed0425354 100644 --- a/litellm/translation/inbound/openai_chat/stream.py +++ b/litellm/translation/inbound/openai_chat/stream.py @@ -73,43 +73,8 @@ def step(state: StreamState, event: StreamEvent) -> _StepResult: return replace(state, model=state.model or event.start.model), () case "text_delta": return _emit(state, {"content": event.text_delta.text}) - case "tool_use_start": - started = replace(state, tool_index=state.tool_index + 1) - return _emit( - started, - { - "content": "", - "tool_calls": [ - { - "id": event.tool_use_start.id, - "type": "function", - "function": { - "name": event.tool_use_start.name, - "arguments": "", - }, - "index": started.tool_index, - } - ], - }, - ) - case "tool_args_delta": - return _emit( - state, - { - "content": "", - "tool_calls": [ - { - "id": None, - "type": "function", - "function": { - "name": None, - "arguments": event.tool_args_delta.partial_json, - }, - "index": max(state.tool_index, 0), - } - ], - }, - ) + case "tool_use_start" | "tool_args_delta": + return _tool_event_step(state, event) case "thinking_delta": return _emit( state, @@ -135,6 +100,45 @@ def step(state: StreamState, event: StreamEvent) -> _StepResult: assert_never(event.tag) +def _tool_event_step(state: StreamState, event: StreamEvent) -> _StepResult: + if event.tag == "tool_use_start": + started = replace(state, tool_index=state.tool_index + 1) + return _emit( + started, + { + "content": "", + "tool_calls": [ + { + "id": event.tool_use_start.id, + "type": "function", + "function": { + "name": event.tool_use_start.name, + "arguments": "", + }, + "index": started.tool_index, + } + ], + }, + ) + return _emit( + state, + { + "content": "", + "tool_calls": [ + { + "id": None, + "type": "function", + "function": { + "name": None, + "arguments": event.tool_args_delta.partial_json, + }, + "index": max(state.tool_index, 0), + } + ], + }, + ) + + def _finish_chunk(state: StreamState, finish: str) -> Body: return { "model": state.model,