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
Original file line number Diff line number Diff line change
Expand Up @@ -1580,8 +1580,13 @@ def make_openai_tool(tool_name: str, tool: ToolDef) -> ChatCompletionToolParam:

for input_tool in tools:
if input_tool.type == "function":
func = input_tool.model_dump(exclude_none=True)
if not func.get("parameters"):
func["parameters"] = {"type": "object"}
elif "type" not in func["parameters"]:
func["parameters"]["type"] = "object"
self.ctx.chat_tools.append(
ChatCompletionToolParam(type="function", function=input_tool.model_dump(exclude_none=True)) # type: ignore[typeddict-item,arg-type] # Dict compatible with FunctionDefinition
ChatCompletionToolParam(type="function", function=func) # type: ignore[typeddict-item,arg-type] # Dict compatible with FunctionDefinition
)
elif input_tool.type in WebSearchToolTypes:
tool_name = "web_search"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from unittest.mock import patch

import pytest

from openai.types.chat.chat_completion_chunk import (
ChatCompletionChunk,
Choice,
Expand Down Expand Up @@ -844,6 +846,39 @@ async def fake_stream_no_args():
assert parsed == {}


@pytest.mark.parametrize(
"input_params,expected_params",
[
({}, {"type": "object"}),
({"properties": {"x": {"type": "string"}}}, {"type": "object", "properties": {"x": {"type": "string"}}}),
({"type": "object", "properties": {}}, {"type": "object", "properties": {}}),
],
ids=["empty", "missing-type", "already-valid"],
)
async def test_function_tool_empty_parameters_normalized(
openai_responses_impl, mock_inference_api, input_params, expected_params
):
"""Test that empty or type-less function parameters are normalized to valid JSON Schema."""
model = "meta-llama/Llama-3.1-8B-Instruct"
mock_inference_api.openai_chat_completion.return_value = fake_stream()

await openai_responses_impl.create_openai_response(
input="test",
model=model,
stream=False,
tools=[
OpenAIResponseInputToolFunction(
name="my_func",
description="A function",
parameters=input_params,
)
],
)

params = mock_inference_api.openai_chat_completion.call_args[0][0]
assert params.tools[0]["function"]["parameters"] == expected_params


async def test_function_tool_strict_field_excluded_when_none(openai_responses_impl, mock_inference_api):
"""Test that function tool 'strict' field is excluded when None (fix for #4617)."""
input_text = "What is the weather?"
Expand Down
Loading