Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: OpenAIChatGenerator - do not pass tools to the OpenAI client when none are provided #8702

Merged
merged 3 commits into from
Jan 10, 2025
Merged
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
7 changes: 4 additions & 3 deletions haystack/components/generators/chat/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,13 @@ def _prepare_api_call( # noqa: PLR0913
tools_strict = tools_strict if tools_strict is not None else self.tools_strict
_check_duplicate_tool_names(tools)

openai_tools = None
openai_tools = {}
if tools:
openai_tools = [
tool_definitions = [
{"type": "function", "function": {**t.tool_spec, **({"strict": tools_strict} if tools_strict else {})}}
for t in tools
]
openai_tools = {"tools": tool_definitions}

is_streaming = streaming_callback is not None
num_responses = generation_kwargs.pop("n", 1)
Expand All @@ -302,8 +303,8 @@ def _prepare_api_call( # noqa: PLR0913
"model": self.model,
"messages": openai_formatted_messages, # type: ignore[arg-type] # openai expects list of specific message types
"stream": streaming_callback is not None,
"tools": openai_tools, # type: ignore[arg-type]
"n": num_responses,
**openai_tools,
**generation_kwargs,
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
OpenAIChatGenerator no longer passes tools to the OpenAI client if none are provided.
Previously, a null value was passed.
This change improves compatibility with OpenAI-compatible APIs that do not support tools.
10 changes: 9 additions & 1 deletion test/components/generators/chat/test_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ def test_run_with_params(self, chat_messages, openai_mock_chat_completion):
assert kwargs["max_tokens"] == 10
assert kwargs["temperature"] == 0.5

# check that the tools are not passed to the OpenAI API (the generator is initialized without tools)
assert "tools" not in kwargs

# check that the component returns the correct response
assert isinstance(response, dict)
assert "replies" in response
Expand Down Expand Up @@ -400,9 +403,14 @@ def test_run_with_tools(self, tools):

mock_chat_completion_create.return_value = completion

component = OpenAIChatGenerator(api_key=Secret.from_token("test-api-key"), tools=tools)
component = OpenAIChatGenerator(api_key=Secret.from_token("test-api-key"), tools=tools, tools_strict=True)
response = component.run([ChatMessage.from_user("What's the weather like in Paris?")])

# ensure that the tools are passed to the OpenAI API
assert mock_chat_completion_create.call_args[1]["tools"] == [
{"type": "function", "function": {**tools[0].tool_spec, "strict": True}}
]

assert len(response["replies"]) == 1
message = response["replies"][0]

Expand Down
Loading