aimlapi-haystack 3.0.0__tar.gz → 3.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (19) hide show
  1. {aimlapi_haystack-3.0.0 → aimlapi_haystack-3.1.0}/CHANGELOG.md +21 -0
  2. {aimlapi_haystack-3.0.0 → aimlapi_haystack-3.1.0}/PKG-INFO +2 -2
  3. {aimlapi_haystack-3.0.0 → aimlapi_haystack-3.1.0}/pyproject.toml +2 -3
  4. {aimlapi_haystack-3.0.0 → aimlapi_haystack-3.1.0}/src/haystack_integrations/components/generators/aimlapi/chat/chat_generator.py +7 -60
  5. aimlapi_haystack-3.1.0/tests/test_aimlapi_chat_generator.py +309 -0
  6. {aimlapi_haystack-3.0.0 → aimlapi_haystack-3.1.0}/tests/test_aimlapi_chat_generator_async.py +16 -118
  7. aimlapi_haystack-3.0.0/tests/test_aimlapi_chat_generator.py +0 -890
  8. aimlapi_haystack-3.0.0/tests/test_aimlapi_stub_async.py +0 -15
  9. {aimlapi_haystack-3.0.0 → aimlapi_haystack-3.1.0}/.gitignore +0 -0
  10. {aimlapi_haystack-3.0.0 → aimlapi_haystack-3.1.0}/LICENSE.txt +0 -0
  11. {aimlapi_haystack-3.0.0 → aimlapi_haystack-3.1.0}/README.md +0 -0
  12. {aimlapi_haystack-3.0.0 → aimlapi_haystack-3.1.0}/examples/aimlapi_basic_example.py +0 -0
  13. {aimlapi_haystack-3.0.0 → aimlapi_haystack-3.1.0}/examples/aimlapi_with_tools_example.py +0 -0
  14. {aimlapi_haystack-3.0.0 → aimlapi_haystack-3.1.0}/pydoc/config_docusaurus.yml +0 -0
  15. {aimlapi_haystack-3.0.0 → aimlapi_haystack-3.1.0}/src/haystack_integrations/components/generators/aimlapi/__init__.py +0 -0
  16. {aimlapi_haystack-3.0.0 → aimlapi_haystack-3.1.0}/src/haystack_integrations/components/generators/aimlapi/chat/__init__.py +0 -0
  17. {aimlapi_haystack-3.0.0 → aimlapi_haystack-3.1.0}/src/haystack_integrations/components/generators/py.typed +0 -0
  18. {aimlapi_haystack-3.0.0 → aimlapi_haystack-3.1.0}/tests/__init__.py +0 -0
  19. {aimlapi_haystack-3.0.0 → aimlapi_haystack-3.1.0}/tests/conftest.py +0 -0
@@ -1,5 +1,26 @@
1
1
  # Changelog
2
2
 
3
+ ## [integrations/aimlapi-v3.0.0] - 2026-10-16
4
+
5
+ ### 🐛 Bug Fixes
6
+
7
+ - Fix new issues raised by ruff 0.16.0 (#3670)
8
+
9
+ ### 🧪 Testing
10
+
11
+ - Fix tests for openai>=3.0.0 (#3801)
12
+ - Test compatible integrations with Python 3.15; update pyproject (#4104)
13
+
14
+ ### ⚙️ CI
15
+
16
+ - Improve changelog generation; fix existing changelogs (#3883)
17
+
18
+ ### 🧹 Chores
19
+
20
+ - Replace ToolInvoker with Agent in tests, examples and docstrings (#4108)
21
+ - [**breaking**] Drop Python 3.10 support (#4105)
22
+
23
+
3
24
  ## [integrations/aimlapi-v2.1.0] - 2026-07-13
4
25
 
5
26
  ### 🚀 Features
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: aimlapi-haystack
3
- Version: 3.0.0
3
+ Version: 3.1.0
4
4
  Project-URL: Documentation, https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/aimlapi#readme
5
5
  Project-URL: Issues, https://github.com/deepset-ai/haystack-core-integrations/issues
6
6
  Project-URL: Source, https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/aimlapi
@@ -18,7 +18,7 @@ Classifier: Programming Language :: Python :: 3.15
18
18
  Classifier: Programming Language :: Python :: Implementation :: CPython
19
19
  Classifier: Programming Language :: Python :: Implementation :: PyPy
20
20
  Requires-Python: >=3.11
21
- Requires-Dist: haystack-ai>=2.22.0
21
+ Requires-Dist: haystack-ai>=3.5.0
22
22
  Description-Content-Type: text/markdown
23
23
 
24
24
  # aimlapi-haystack
@@ -23,7 +23,7 @@ classifiers = [
23
23
  "Programming Language :: Python :: Implementation :: CPython",
24
24
  "Programming Language :: Python :: Implementation :: PyPy",
25
25
  ]
26
- dependencies = ["haystack-ai>=2.22.0"]
26
+ dependencies = ["haystack-ai>=3.5.0"]
27
27
 
28
28
  [project.urls]
29
29
  Documentation = "https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/aimlapi#readme"
@@ -56,8 +56,7 @@ dependencies = [
56
56
  "pytest-cov",
57
57
  "pytest-rerunfailures",
58
58
  "mypy",
59
- "pip",
60
- "pytz"
59
+ "pip"
61
60
  ]
62
61
 
63
62
  [tool.hatch.envs.test.scripts]
@@ -4,15 +4,13 @@
4
4
 
5
5
  from typing import Any
6
6
 
7
- from haystack import component, default_to_dict, logging
7
+ from haystack import component, default_to_dict
8
8
  from haystack.components.generators.chat import OpenAIChatGenerator
9
- from haystack.dataclasses import ChatMessage, StreamingCallbackT
10
- from haystack.tools import ToolsType, _check_duplicate_tool_names, flatten_tools_or_toolsets, serialize_tools_or_toolset
9
+ from haystack.dataclasses import StreamingCallbackT
10
+ from haystack.tools import ToolsType, serialize_tools_or_toolset
11
11
  from haystack.utils import serialize_callable
12
12
  from haystack.utils.auth import Secret
13
13
 
14
- logger = logging.getLogger(__name__)
15
-
16
14
  AIMLAPI_HEADERS = {"HTTP-Referer": "https://github.com/deepset-ai/haystack-core-integrations", "X-Title": "Haystack"}
17
15
 
18
16
 
@@ -153,59 +151,8 @@ class AIMLAPIChatGenerator(OpenAIChatGenerator):
153
151
  http_client_kwargs=self.http_client_kwargs,
154
152
  )
155
153
 
156
- def _prepare_api_call(
157
- self,
158
- *,
159
- messages: list[ChatMessage],
160
- streaming_callback: StreamingCallbackT | None = None,
161
- generation_kwargs: dict[str, Any] | None = None,
162
- tools: ToolsType | None = None,
163
- tools_strict: bool | None = None,
164
- ) -> dict[str, Any]:
165
- # update generation kwargs by merging with the generation kwargs passed to the run method
166
- generation_kwargs = {**self.generation_kwargs, **(generation_kwargs or {})}
167
- extra_headers = {**AIMLAPI_HEADERS, **(self.extra_headers or {})}
168
-
169
- # adapt ChatMessage(s) to the format expected by the OpenAI API (AIMLAPI uses the same format)
170
- aimlapi_formatted_messages: list[dict[str, Any]] = [message.to_openai_dict_format() for message in messages]
171
-
172
- tools_strict = tools_strict if tools_strict is not None else self.tools_strict
173
- flattened_tools = flatten_tools_or_toolsets(tools or self.tools)
174
- _check_duplicate_tool_names(flattened_tools)
175
-
176
- openai_tools = {}
177
- if flattened_tools:
178
- tool_definitions = []
179
- for tool in flattened_tools:
180
- function_spec = {**tool.tool_spec}
181
- if tools_strict:
182
- function_spec["strict"] = True
183
- parameters = function_spec.get("parameters")
184
- if isinstance(parameters, dict):
185
- parameters["additionalProperties"] = False
186
- tool_definitions.append({"type": "function", "function": function_spec})
187
- openai_tools = {"tools": tool_definitions}
188
-
189
- is_streaming = streaming_callback is not None
190
- num_responses = generation_kwargs.pop("n", 1)
191
-
192
- if is_streaming and num_responses > 1:
193
- msg = "Cannot stream multiple responses, please set n=1."
194
- raise ValueError(msg)
195
- response_format = generation_kwargs.pop("response_format", None)
196
-
197
- base_args = {
198
- "model": self.model,
199
- "messages": aimlapi_formatted_messages,
200
- "n": num_responses,
201
- **openai_tools,
202
- "extra_body": {**generation_kwargs},
203
- "extra_headers": {**extra_headers},
154
+ def _adapt_generation_kwargs(self, generation_kwargs: dict[str, Any]) -> dict[str, Any]:
155
+ return {
156
+ "extra_body": dict(generation_kwargs),
157
+ "extra_headers": {**AIMLAPI_HEADERS, **(self.extra_headers or {})},
204
158
  }
205
- if response_format and not is_streaming:
206
- return {**base_args, "response_format": response_format, "openai_endpoint": "parse"}
207
-
208
- final_args = {**base_args, "stream": is_streaming, "openai_endpoint": "create"}
209
- if response_format:
210
- final_args["response_format"] = response_format
211
- return final_args
@@ -0,0 +1,309 @@
1
+ # SPDX-FileCopyrightText: 2023-present deepset GmbH <info@deepset.ai>
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ import os
6
+
7
+ import pytest
8
+ from haystack.components.agents import Agent
9
+ from haystack.components.generators.utils import print_streaming_chunk
10
+ from haystack.dataclasses import ChatMessage, ChatRole, StreamingChunk, ToolCall
11
+ from haystack.tools import Tool
12
+ from haystack.utils.auth import Secret
13
+
14
+ from haystack_integrations.components.generators.aimlapi.chat.chat_generator import AIMLAPIChatGenerator
15
+
16
+
17
+ def weather(city: str):
18
+ """Get weather for a given city."""
19
+ return f"The weather in {city} is sunny and 32°C"
20
+
21
+
22
+ @pytest.fixture
23
+ def tools():
24
+ tool_parameters = {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]}
25
+ tool = Tool(
26
+ name="weather",
27
+ description="useful to determine the weather in a given location",
28
+ parameters=tool_parameters,
29
+ function=weather,
30
+ )
31
+
32
+ return [tool]
33
+
34
+
35
+ class TestAIMLAPIChatGenerator:
36
+ def test_init_default(self, monkeypatch):
37
+ monkeypatch.setenv("AIMLAPI_API_KEY", "test-api-key")
38
+ component = AIMLAPIChatGenerator()
39
+ assert component.api_key.resolve_value() == "test-api-key"
40
+ assert component.model == "openai/gpt-5-chat-latest"
41
+ assert component.api_base_url == "https://api.aimlapi.com/v1"
42
+ assert component.streaming_callback is None
43
+ assert not component.generation_kwargs
44
+
45
+ def test_init_with_parameters(self):
46
+ component = AIMLAPIChatGenerator(
47
+ api_key=Secret.from_token("test-api-key"),
48
+ model="openai/gpt-5-chat-latest",
49
+ streaming_callback=print_streaming_chunk,
50
+ api_base_url="test-base-url",
51
+ generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"},
52
+ )
53
+ assert component.api_key.resolve_value() == "test-api-key"
54
+ assert component.model == "openai/gpt-5-chat-latest"
55
+ assert component.streaming_callback is print_streaming_chunk
56
+ assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"}
57
+
58
+ def test_to_dict_default(self, monkeypatch):
59
+ monkeypatch.setenv("AIMLAPI_API_KEY", "test-api-key")
60
+ component = AIMLAPIChatGenerator()
61
+ data = component.to_dict()
62
+
63
+ assert (
64
+ data["type"]
65
+ == "haystack_integrations.components.generators.aimlapi.chat.chat_generator.AIMLAPIChatGenerator"
66
+ )
67
+
68
+ expected_params = {
69
+ "api_key": {"env_vars": ["AIMLAPI_API_KEY"], "strict": True, "type": "env_var"},
70
+ "model": "openai/gpt-5-chat-latest",
71
+ "streaming_callback": None,
72
+ "api_base_url": "https://api.aimlapi.com/v1",
73
+ "generation_kwargs": {},
74
+ "extra_headers": None,
75
+ "timeout": None,
76
+ "max_retries": None,
77
+ "tools": None,
78
+ "http_client_kwargs": None,
79
+ }
80
+
81
+ for key, value in expected_params.items():
82
+ assert data["init_parameters"][key] == value
83
+
84
+ def test_to_dict_with_parameters(self, monkeypatch):
85
+ monkeypatch.setenv("ENV_VAR", "test-api-key")
86
+ component = AIMLAPIChatGenerator(
87
+ api_key=Secret.from_env_var("ENV_VAR"),
88
+ model="openai/gpt-5-chat-latest",
89
+ streaming_callback=print_streaming_chunk,
90
+ api_base_url="test-base-url",
91
+ generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"},
92
+ extra_headers={"test-header": "test-value"},
93
+ timeout=10,
94
+ max_retries=10,
95
+ tools=None,
96
+ http_client_kwargs={"proxy": "http://localhost:8080"},
97
+ )
98
+ data = component.to_dict()
99
+
100
+ assert (
101
+ data["type"]
102
+ == "haystack_integrations.components.generators.aimlapi.chat.chat_generator.AIMLAPIChatGenerator"
103
+ )
104
+
105
+ expected_params = {
106
+ "api_key": {"env_vars": ["ENV_VAR"], "strict": True, "type": "env_var"},
107
+ "model": "openai/gpt-5-chat-latest",
108
+ "api_base_url": "test-base-url",
109
+ "streaming_callback": "haystack.components.generators.utils.print_streaming_chunk",
110
+ "generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"},
111
+ "extra_headers": {"test-header": "test-value"},
112
+ "timeout": 10,
113
+ "max_retries": 10,
114
+ "tools": None,
115
+ "http_client_kwargs": {"proxy": "http://localhost:8080"},
116
+ }
117
+
118
+ for key, value in expected_params.items():
119
+ assert data["init_parameters"][key] == value
120
+
121
+ def test_from_dict(self, monkeypatch):
122
+ monkeypatch.setenv("AIMLAPI_API_KEY", "fake-api-key")
123
+ data = {
124
+ "type": ("haystack_integrations.components.generators.aimlapi.chat.chat_generator.AIMLAPIChatGenerator"),
125
+ "init_parameters": {
126
+ "api_key": {"env_vars": ["AIMLAPI_API_KEY"], "strict": True, "type": "env_var"},
127
+ "model": "openai/gpt-5-chat-latest",
128
+ "api_base_url": "test-base-url",
129
+ "streaming_callback": "haystack.components.generators.utils.print_streaming_chunk",
130
+ "generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"},
131
+ "extra_headers": {"test-header": "test-value"},
132
+ "timeout": 10,
133
+ "max_retries": 10,
134
+ "tools": None,
135
+ "http_client_kwargs": {"proxy": "http://localhost:8080"},
136
+ },
137
+ }
138
+ component = AIMLAPIChatGenerator.from_dict(data)
139
+ assert component.model == "openai/gpt-5-chat-latest"
140
+ assert component.streaming_callback is print_streaming_chunk
141
+ assert component.api_base_url == "test-base-url"
142
+ assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"}
143
+ assert component.api_key == Secret.from_env_var("AIMLAPI_API_KEY")
144
+ assert component.http_client_kwargs == {"proxy": "http://localhost:8080"}
145
+ assert component.tools is None
146
+ assert component.extra_headers == {"test-header": "test-value"}
147
+ assert component.timeout == 10
148
+ assert component.max_retries == 10
149
+
150
+ @pytest.mark.parametrize("extra_headers", [None, {"X-Title": "Custom", "X-Custom": "value"}])
151
+ def test_prepare_api_call_with_params(self, extra_headers):
152
+ component = AIMLAPIChatGenerator(
153
+ generation_kwargs={"max_tokens": 10, "temperature": 0.5}, extra_headers=extra_headers
154
+ )
155
+ args = component._prepare_api_call(
156
+ messages=[ChatMessage.from_user("hi")], generation_kwargs={"temperature": 0.7}
157
+ )
158
+
159
+ assert args["extra_body"] == {"max_tokens": 10, "temperature": 0.7}
160
+ assert args["extra_headers"] == {
161
+ "HTTP-Referer": "https://github.com/deepset-ai/haystack-core-integrations",
162
+ "X-Title": "Haystack",
163
+ **(extra_headers or {}),
164
+ }
165
+ assert component.generation_kwargs == {"max_tokens": 10, "temperature": 0.5}
166
+
167
+ @pytest.mark.skipif(
168
+ not os.environ.get("AIMLAPI_API_KEY", None),
169
+ reason="Export an env var called AIMLAPI_API_KEY containing the AIMLAPI API key to run this test.",
170
+ )
171
+ @pytest.mark.integration
172
+ def test_live_run(self):
173
+ chat_messages = [ChatMessage.from_user("What's the capital of France")]
174
+ component = AIMLAPIChatGenerator(model="openai/gpt-5-nano-2025-08-07")
175
+ results = component.run(chat_messages)
176
+ assert len(results["replies"]) == 1
177
+ message: ChatMessage = results["replies"][0]
178
+ assert message.text
179
+ assert "Paris" in message.text
180
+ assert "gpt-5-nano" in message.meta["model"]
181
+ assert message.meta["finish_reason"] == "stop"
182
+
183
+ @pytest.mark.skipif(
184
+ not os.environ.get("AIMLAPI_API_KEY", None),
185
+ reason="Export an env var called AIMLAPI_API_KEY containing the AIMLAPI API key to run this test.",
186
+ )
187
+ @pytest.mark.integration
188
+ def test_live_run_streaming(self):
189
+ class Callback:
190
+ def __init__(self):
191
+ self.responses = ""
192
+ self.counter = 0
193
+
194
+ def __call__(self, chunk: StreamingChunk) -> None:
195
+ self.counter += 1
196
+ self.responses += chunk.content if chunk.content else ""
197
+
198
+ callback = Callback()
199
+ component = AIMLAPIChatGenerator(streaming_callback=callback, model="openai/gpt-5-nano-2025-08-07")
200
+ results = component.run([ChatMessage.from_user("What's the capital of France?")])
201
+
202
+ assert len(results["replies"]) == 1
203
+ message: ChatMessage = results["replies"][0]
204
+ assert message.text
205
+ assert "Paris" in message.text
206
+
207
+ assert "gpt-5-nano" in message.meta["model"]
208
+ assert message.meta["finish_reason"] == "stop"
209
+
210
+ assert callback.counter > 1
211
+ assert "Paris" in callback.responses
212
+
213
+ @pytest.mark.skipif(
214
+ not os.environ.get("AIMLAPI_API_KEY", None),
215
+ reason="Export an env var called AIMLAPI_API_KEY containing the AIMLAPI API key to run this test.",
216
+ )
217
+ @pytest.mark.integration
218
+ def test_live_run_with_tools_and_response(self, tools):
219
+ """
220
+ Integration test that the AIMLAPIChatGenerator component can run with tools and get a response.
221
+ """
222
+ initial_messages = [ChatMessage.from_user("What's the weather like in Paris and Berlin?")]
223
+ component = AIMLAPIChatGenerator(tools=tools, model="openai/gpt-5-nano-2025-08-07")
224
+ results = component.run(messages=initial_messages, generation_kwargs={"tool_choice": "auto"})
225
+
226
+ assert len(results["replies"]) == 1
227
+
228
+ # Find the message with tool calls
229
+ tool_message = results["replies"][0]
230
+
231
+ assert isinstance(tool_message, ChatMessage)
232
+ tool_calls = tool_message.tool_calls
233
+ assert len(tool_calls) == 2
234
+ assert ChatMessage.is_from(tool_message, ChatRole.ASSISTANT)
235
+
236
+ for tool_call in tool_calls:
237
+ assert tool_call.id is not None
238
+ assert isinstance(tool_call, ToolCall)
239
+ assert tool_call.tool_name == "weather"
240
+
241
+ arguments = [tool_call.arguments for tool_call in tool_calls]
242
+ assert sorted(arguments, key=lambda x: x["city"]) == [{"city": "Berlin"}, {"city": "Paris"}]
243
+ assert tool_message.meta["finish_reason"] == "tool_calls"
244
+
245
+ new_messages = [
246
+ initial_messages[0],
247
+ tool_message,
248
+ ChatMessage.from_tool(tool_result="22° C and sunny", origin=tool_calls[0]),
249
+ ChatMessage.from_tool(tool_result="16° C and windy", origin=tool_calls[1]),
250
+ ]
251
+ # Pass the tool result to the model to get the final response
252
+ results = component.run(new_messages)
253
+
254
+ assert len(results["replies"]) == 1
255
+ final_message = results["replies"][0]
256
+ assert final_message.is_from(ChatRole.ASSISTANT)
257
+ assert len(final_message.text) > 0
258
+ assert "paris" in final_message.text.lower()
259
+
260
+ @pytest.mark.skipif(
261
+ not os.environ.get("AIMLAPI_API_KEY", None),
262
+ reason="Export an env var called AIMLAPI_API_KEY containing the AIMLAPI API key to run this test.",
263
+ )
264
+ @pytest.mark.integration
265
+ def test_live_run_with_tools_streaming(self, tools):
266
+ """
267
+ Integration test that the AIMLAPIChatGenerator component can run with tools and streaming.
268
+ """
269
+ component = AIMLAPIChatGenerator(
270
+ tools=tools, streaming_callback=print_streaming_chunk, model="openai/gpt-5-nano-2025-08-07"
271
+ )
272
+ results = component.run(
273
+ [ChatMessage.from_user("What's the weather like in Paris and Berlin?")],
274
+ generation_kwargs={"tool_choice": "auto"},
275
+ )
276
+
277
+ assert len(results["replies"]) == 1
278
+
279
+ # Find the message with tool calls
280
+ tool_message = results["replies"][0]
281
+
282
+ assert isinstance(tool_message, ChatMessage)
283
+ tool_calls = tool_message.tool_calls
284
+ assert len(tool_calls) == 2
285
+ assert ChatMessage.is_from(tool_message, ChatRole.ASSISTANT)
286
+
287
+ for tool_call in tool_calls:
288
+ assert tool_call.id is not None
289
+ assert isinstance(tool_call, ToolCall)
290
+ assert tool_call.tool_name == "weather"
291
+
292
+ arguments = [tool_call.arguments for tool_call in tool_calls]
293
+ assert sorted(arguments, key=lambda x: x["city"]) == [{"city": "Berlin"}, {"city": "Paris"}]
294
+ assert tool_message.meta["finish_reason"] == "tool_calls"
295
+
296
+ @pytest.mark.skipif(
297
+ not os.environ.get("AIMLAPI_API_KEY", None),
298
+ reason="Export an env var called AIMLAPI_API_KEY containing the AIMLAPI API key to run this test.",
299
+ )
300
+ @pytest.mark.integration
301
+ def test_agent_with_aimlapi_chat_generator(self, tools):
302
+ """
303
+ Test that the AIMLAPIChatGenerator component can drive tool calls in an Agent
304
+ """
305
+ agent = Agent(chat_generator=AIMLAPIChatGenerator(model="openai/gpt-5-mini-2025-08-07"), tools=tools)
306
+ result = agent.run(messages=[ChatMessage.from_user("What's the weather like in Paris?")])
307
+
308
+ tool_results = [msg.tool_call_result.result for msg in result["messages"] if msg.tool_call_result]
309
+ assert "The weather in Paris is sunny and 32°C" in tool_results
@@ -1,32 +1,22 @@
1
+ # SPDX-FileCopyrightText: 2023-present deepset GmbH <info@deepset.ai>
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
1
5
  import os
2
- from datetime import datetime
3
- from unittest.mock import AsyncMock, patch
4
6
 
5
7
  import pytest
6
- import pytz
7
8
  from haystack.dataclasses import (
8
9
  ChatMessage,
9
10
  ChatRole,
10
11
  StreamingChunk,
11
12
  )
12
13
  from haystack.tools import Tool
13
- from openai import AsyncOpenAI
14
- from openai.types.chat import ChatCompletion, ChatCompletionMessage
15
- from openai.types.chat.chat_completion import Choice
16
14
 
17
15
  from haystack_integrations.components.generators.aimlapi.chat.chat_generator import (
18
16
  AIMLAPIChatGenerator,
19
17
  )
20
18
 
21
19
 
22
- @pytest.fixture
23
- def chat_messages():
24
- return [
25
- ChatMessage.from_system("You are a helpful assistant"),
26
- ChatMessage.from_user("What's the capital of France"),
27
- ]
28
-
29
-
30
20
  def weather(city: str):
31
21
  """Get weather for a given city."""
32
22
  return f"The weather in {city} is sunny and 32°C"
@@ -49,91 +39,13 @@ def tools():
49
39
  return [tool]
50
40
 
51
41
 
52
- @pytest.fixture
53
- def mock_async_chat_completion():
54
- """
55
- Mock the Async OpenAI API completion response and reuse it for async tests
56
- """
57
- with patch(
58
- "openai.resources.chat.completions.AsyncCompletions.create",
59
- new_callable=AsyncMock,
60
- ) as mock_chat_completion_create:
61
- completion = ChatCompletion(
62
- id="foo",
63
- model="openai/gpt-5-chat-latest",
64
- object="chat.completion",
65
- choices=[
66
- Choice(
67
- finish_reason="stop",
68
- logprobs=None,
69
- index=0,
70
- message=ChatCompletionMessage(content="Hello world!", role="assistant"),
71
- )
72
- ],
73
- created=int(datetime.now(tz=pytz.timezone("UTC")).timestamp()),
74
- usage={
75
- "prompt_tokens": 57,
76
- "completion_tokens": 40,
77
- "total_tokens": 97,
78
- },
79
- )
80
- # For async mocks, the return value should be awaitable
81
- mock_chat_completion_create.return_value = completion
82
- yield mock_chat_completion_create
83
-
84
-
42
+ @pytest.mark.skipif(
43
+ not os.environ.get("AIMLAPI_API_KEY", None),
44
+ reason="Export an env var called AIMLAPI_API_KEY containing the AIMLAPI API key to run this test.",
45
+ )
46
+ @pytest.mark.integration
47
+ @pytest.mark.asyncio
85
48
  class TestAIMLAPIChatGeneratorAsync:
86
- @pytest.mark.asyncio
87
- async def test_warm_up_async(self, monkeypatch):
88
- monkeypatch.setenv("AIMLAPI_API_KEY", "test-api-key")
89
- component = AIMLAPIChatGenerator()
90
- if hasattr(component, "warm_up_async"):
91
- # haystack-ai >= 3.0 creates the async client during async warm-up
92
- await component.warm_up_async()
93
-
94
- assert isinstance(component.async_client, AsyncOpenAI)
95
- assert component.async_client.api_key == "test-api-key"
96
- assert component.async_client.base_url == "https://api.aimlapi.com/v1/"
97
-
98
- @pytest.mark.asyncio
99
- async def test_run_async(self, chat_messages, mock_async_chat_completion, monkeypatch): # noqa: ARG002
100
- monkeypatch.setenv("AIMLAPI_API_KEY", "fake-api-key")
101
- component = AIMLAPIChatGenerator(model="openai/gpt-5-nano-2025-08-07")
102
- response = await component.run_async(chat_messages)
103
-
104
- # check that the component returns the correct ChatMessage response
105
- assert isinstance(response, dict)
106
- assert "replies" in response
107
- assert isinstance(response["replies"], list)
108
- assert len(response["replies"]) == 1
109
- assert [isinstance(reply, ChatMessage) for reply in response["replies"]]
110
-
111
- @pytest.mark.asyncio
112
- async def test_run_async_with_params(self, chat_messages, mock_async_chat_completion, monkeypatch):
113
- monkeypatch.setenv("AIMLAPI_API_KEY", "fake-api-key")
114
- component = AIMLAPIChatGenerator(
115
- model="openai/gpt-5-nano-2025-08-07", generation_kwargs={"max_tokens": 10, "temperature": 0.5}
116
- )
117
- response = await component.run_async(chat_messages)
118
-
119
- # check that the component calls the OpenAI API with the correct parameters
120
- _, kwargs = mock_async_chat_completion.call_args
121
- assert kwargs["extra_body"]["max_tokens"] == 10
122
- assert kwargs["extra_body"]["temperature"] == 0.5
123
-
124
- # check that the component returns the correct response
125
- assert isinstance(response, dict)
126
- assert "replies" in response
127
- assert isinstance(response["replies"], list)
128
- assert len(response["replies"]) == 1
129
- assert [isinstance(reply, ChatMessage) for reply in response["replies"]]
130
-
131
- @pytest.mark.skipif(
132
- not os.environ.get("AIMLAPI_API_KEY", None),
133
- reason="Export an env var called AIMLAPI_API_KEY containing the AIMLAPI API key to run this test.",
134
- )
135
- @pytest.mark.integration
136
- @pytest.mark.asyncio
137
49
  async def test_live_run_async(self):
138
50
  chat_messages = [ChatMessage.from_user("What's the capital of France")]
139
51
  component = AIMLAPIChatGenerator(model="openai/gpt-5-nano-2025-08-07")
@@ -143,13 +55,8 @@ class TestAIMLAPIChatGeneratorAsync:
143
55
  assert "Paris" in message.text
144
56
  assert "gpt-5-nano" in message.meta["model"]
145
57
  assert message.meta["finish_reason"] == "stop"
58
+ await component.close_async()
146
59
 
147
- @pytest.mark.skipif(
148
- not os.environ.get("AIMLAPI_API_KEY", None),
149
- reason="Export an env var called AIMLAPI_API_KEY containing the AIMLAPI API key to run this test.",
150
- )
151
- @pytest.mark.integration
152
- @pytest.mark.asyncio
153
60
  async def test_live_run_streaming_async(self):
154
61
  counter = 0
155
62
  responses = ""
@@ -172,13 +79,8 @@ class TestAIMLAPIChatGeneratorAsync:
172
79
 
173
80
  assert counter > 1
174
81
  assert "Paris" in responses
82
+ await component.close_async()
175
83
 
176
- @pytest.mark.skipif(
177
- not os.environ.get("AIMLAPI_API_KEY", None),
178
- reason="Export an env var called AIMLAPI_API_KEY containing the AIMLAPI API key to run this test.",
179
- )
180
- @pytest.mark.integration
181
- @pytest.mark.asyncio
182
84
  async def test_live_run_with_tools_and_response_async(self, tools):
183
85
  """
184
86
  Integration test that the AIMLAPIChatGenerator component can run with tools and get a response.
@@ -219,13 +121,8 @@ class TestAIMLAPIChatGeneratorAsync:
219
121
  assert not final_message.tool_call
220
122
  assert len(final_message.text) > 0
221
123
  assert "paris" in final_message.text.lower()
124
+ await component.close_async()
222
125
 
223
- @pytest.mark.skipif(
224
- not os.environ.get("AIMLAPI_API_KEY", None),
225
- reason="Export an env var called AIMLAPI_API_KEY containing the AIMLAPI API key to run this test.",
226
- )
227
- @pytest.mark.integration
228
- @pytest.mark.asyncio
229
126
  async def test_live_run_with_tools_streaming_async(self, tools):
230
127
  """
231
128
  Integration test that the AIMLAPIChatGenerator component can run with tools and streaming.
@@ -238,8 +135,8 @@ class TestAIMLAPIChatGeneratorAsync:
238
135
  nonlocal counter
239
136
  nonlocal tool_calls
240
137
  counter += 1
241
- if chunk.meta.get("tool_calls"):
242
- tool_calls.extend(chunk.meta["tool_calls"])
138
+ if chunk.tool_calls:
139
+ tool_calls.extend(chunk.tool_calls)
243
140
 
244
141
  component = AIMLAPIChatGenerator(tools=tools, streaming_callback=callback, model="openai/gpt-5-nano-2025-08-07")
245
142
  results = await component.run_async(
@@ -267,3 +164,4 @@ class TestAIMLAPIChatGeneratorAsync:
267
164
  assert tool_call.tool_name == "weather"
268
165
  assert tool_call.arguments == {"city": "Paris"}
269
166
  assert tool_message.meta["finish_reason"] == "tool_calls"
167
+ await component.close_async()