Skip to content

Commit e961ed6

Browse files
[MCP] Handle Ollama's deviation from the OpenAI tool streaming spec (#3140)
* fix ollama * style
1 parent 5c5fbd1 commit e961ed6

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/huggingface_hub/inference/_mcp/mcp_client.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -286,16 +286,19 @@ async def process_single_turn_with_tools(
286286
# Process tool calls
287287
if delta.tool_calls:
288288
for tool_call in delta.tool_calls:
289-
# Aggregate chunks into tool calls
290-
if tool_call.index not in final_tool_calls:
291-
if (
292-
tool_call.function.arguments is None or tool_call.function.arguments == "{}"
293-
): # Corner case (depends on provider)
294-
tool_call.function.arguments = ""
295-
final_tool_calls[tool_call.index] = tool_call
296-
297-
elif tool_call.function.arguments:
298-
final_tool_calls[tool_call.index].function.arguments += tool_call.function.arguments
289+
idx = tool_call.index
290+
# first chunk for this tool call
291+
if idx not in final_tool_calls:
292+
final_tool_calls[idx] = tool_call
293+
if final_tool_calls[idx].function.arguments is None:
294+
final_tool_calls[idx].function.arguments = ""
295+
continue
296+
# safety before concatenating text to .function.arguments
297+
if final_tool_calls[idx].function.arguments is None:
298+
final_tool_calls[idx].function.arguments = ""
299+
300+
if tool_call.function.arguments:
301+
final_tool_calls[idx].function.arguments += tool_call.function.arguments
299302

300303
# Optionally exit early if no tools in first chunks
301304
if exit_if_first_chunk_no_tool and num_of_chunks <= 2 and len(final_tool_calls) == 0:

0 commit comments

Comments
 (0)