Skip to content

Commit 4aa586d

Browse files
committed
Fix: Ensure that tool calls with no arguments get handled correctly #3560
When a model decides to use an MCP tool call that requires no arguments, it sets the arguments field to None. This causes validation errors because this field gets removed when being parsed by an openai compatible inference provider like vLLM This PR ensures that, as soon as the tool call args are accumulated while streaming, we check to ensure no tool call function arguments are set to None - if they are we replace them with "{}" Closes #3456 Added new unit test to verify that any tool calls with function arguments set to None get handled correctly Signed-off-by: Jaideep Rao <[email protected]>
1 parent 6cce553 commit 4aa586d

File tree

3 files changed

+163
-1
lines changed

3 files changed

+163
-1
lines changed

llama_stack/providers/inline/agents/meta_reference/responses/streaming.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,11 @@ async def _process_streaming_chunks(
328328

329329
# Emit arguments.done events for completed tool calls (differentiate between MCP and function calls)
330330
for tool_call_index in sorted(chat_response_tool_calls.keys()):
331+
tool_call = chat_response_tool_calls[tool_call_index]
332+
# Ensure that arguments, if sent back to the inference provider, are not None
333+
tool_call.function.arguments = tool_call.function.arguments or "{}"
331334
tool_call_item_id = tool_call_item_ids[tool_call_index]
332-
final_arguments = chat_response_tool_calls[tool_call_index].function.arguments or ""
335+
final_arguments = tool_call.function.arguments
333336
tool_call_name = chat_response_tool_calls[tool_call_index].function.name
334337

335338
# Check if this is an MCP tool call

tests/integration/agents/test_openai_responses.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,36 @@ def test_function_call_output_response(openai_client, client_with_models, text_m
264264
assert (
265265
"sunny" in response2.output[0].content[0].text.lower() or "warm" in response2.output[0].content[0].text.lower()
266266
)
267+
268+
269+
def test_function_call_output_response_with_none_arguments(openai_client, client_with_models, text_model_id):
270+
"""Test handling of function call outputs in responses when function does not accept arguments."""
271+
if isinstance(client_with_models, LlamaStackAsLibraryClient):
272+
pytest.skip("OpenAI responses are not supported when testing with library client yet.")
273+
274+
client = openai_client
275+
276+
# First create a response that triggers a function call
277+
response = client.responses.create(
278+
model=text_model_id,
279+
input=[
280+
{
281+
"role": "user",
282+
"content": "what's the current time? You MUST call the `get_current_time` function to find out.",
283+
}
284+
],
285+
tools=[
286+
{
287+
"type": "function",
288+
"name": "get_current_time",
289+
"description": "Get the current time",
290+
"parameters": {},
291+
}
292+
],
293+
stream=False,
294+
)
295+
296+
# Verify we got a function call
297+
assert response.output[0].type == "function_call"
298+
assert response.output[0].arguments == "{}"
299+
_ = response.output[0].call_id

tests/unit/providers/agents/meta_reference/test_openai_responses.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,132 @@ async def fake_stream_toolcall():
328328
assert chunks[5].response.output[0].name == "get_weather"
329329

330330

331+
async def test_create_openai_response_with_tool_call_function_arguments_none(openai_responses_impl, mock_inference_api):
332+
"""Test creating an OpenAI response with a tool call response that has a function that does not accept arguments, or arguments set to None when they are not mandatory."""
333+
# Setup
334+
input_text = "What is the time right now?"
335+
model = "meta-llama/Llama-3.1-8B-Instruct"
336+
337+
async def fake_stream_toolcall():
338+
yield ChatCompletionChunk(
339+
id="123",
340+
choices=[
341+
Choice(
342+
index=0,
343+
delta=ChoiceDelta(
344+
tool_calls=[
345+
ChoiceDeltaToolCall(
346+
index=0,
347+
id="tc_123",
348+
function=ChoiceDeltaToolCallFunction(name="get_current_time", arguments=None),
349+
type=None,
350+
)
351+
]
352+
),
353+
),
354+
],
355+
created=1,
356+
model=model,
357+
object="chat.completion.chunk",
358+
)
359+
360+
mock_inference_api.openai_chat_completion.return_value = fake_stream_toolcall()
361+
362+
# Function does not accept arguments
363+
result = await openai_responses_impl.create_openai_response(
364+
input=input_text,
365+
model=model,
366+
stream=True,
367+
temperature=0.1,
368+
tools=[
369+
OpenAIResponseInputToolFunction(
370+
name="get_current_time",
371+
description="Get current time for system's timezone",
372+
parameters={},
373+
)
374+
],
375+
)
376+
377+
# Check that we got the content from our mocked tool execution result
378+
chunks = [chunk async for chunk in result]
379+
380+
# Verify event types
381+
# Should have: response.created, output_item.added, function_call_arguments.delta,
382+
# function_call_arguments.done, output_item.done, response.completed
383+
assert len(chunks) == 5
384+
385+
# Verify inference API was called correctly (after iterating over result)
386+
first_call = mock_inference_api.openai_chat_completion.call_args_list[0]
387+
assert first_call.kwargs["messages"][0].content == input_text
388+
assert first_call.kwargs["tools"] is not None
389+
assert first_call.kwargs["temperature"] == 0.1
390+
391+
# Check response.created event (should have empty output)
392+
assert chunks[0].type == "response.created"
393+
assert len(chunks[0].response.output) == 0
394+
395+
# Check streaming events
396+
assert chunks[1].type == "response.output_item.added"
397+
assert chunks[2].type == "response.function_call_arguments.done"
398+
assert chunks[3].type == "response.output_item.done"
399+
400+
# Check response.completed event (should have the tool call with arguments set to "{}")
401+
assert chunks[4].type == "response.completed"
402+
assert len(chunks[4].response.output) == 1
403+
assert chunks[4].response.output[0].type == "function_call"
404+
assert chunks[4].response.output[0].name == "get_current_time"
405+
assert chunks[4].response.output[0].arguments == "{}"
406+
407+
mock_inference_api.openai_chat_completion.return_value = fake_stream_toolcall()
408+
409+
# Function accepts optional arguments
410+
result = await openai_responses_impl.create_openai_response(
411+
input=input_text,
412+
model=model,
413+
stream=True,
414+
temperature=0.1,
415+
tools=[
416+
OpenAIResponseInputToolFunction(
417+
name="get_current_time",
418+
description="Get current time for system's timezone",
419+
parameters={
420+
"timezone": "string",
421+
},
422+
)
423+
],
424+
)
425+
426+
# Check that we got the content from our mocked tool execution result
427+
chunks = [chunk async for chunk in result]
428+
429+
# Verify event types
430+
# Should have: response.created, output_item.added, function_call_arguments.delta,
431+
# function_call_arguments.done, output_item.done, response.completed
432+
assert len(chunks) == 5
433+
434+
# Verify inference API was called correctly (after iterating over result)
435+
first_call = mock_inference_api.openai_chat_completion.call_args_list[0]
436+
assert first_call.kwargs["messages"][0].content == input_text
437+
assert first_call.kwargs["tools"] is not None
438+
assert first_call.kwargs["temperature"] == 0.1
439+
440+
# Check response.created event (should have empty output)
441+
assert chunks[0].type == "response.created"
442+
assert len(chunks[0].response.output) == 0
443+
444+
# Check streaming events
445+
assert chunks[1].type == "response.output_item.added"
446+
assert chunks[2].type == "response.function_call_arguments.done"
447+
assert chunks[3].type == "response.output_item.done"
448+
449+
# Check response.completed event (should have the tool call with arguments set to "{}")
450+
assert chunks[4].type == "response.completed"
451+
assert len(chunks[4].response.output) == 1
452+
assert chunks[4].response.output[0].type == "function_call"
453+
assert chunks[4].response.output[0].name == "get_current_time"
454+
assert chunks[4].response.output[0].arguments == "{}"
455+
456+
331457
async def test_create_openai_response_with_multiple_messages(openai_responses_impl, mock_inference_api):
332458
"""Test creating an OpenAI response with multiple messages."""
333459
# Setup

0 commit comments

Comments
 (0)