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
2 changes: 2 additions & 0 deletions haystack/components/generators/openai_image_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ def to_dict(self) -> dict[str, Any]:
api_key=self.api_key,
api_base_url=self.api_base_url,
organization=self.organization,
timeout=self.timeout,
max_retries=self.max_retries,
http_client_kwargs=self.http_client_kwargs,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
fixes:
- |
Add the init parameters ``timeout`` and ``max_retries`` to the ``to_dict`` method of
``OpenAIImageGenerator``. They were dropped on serialization, so a pipeline saved and
reloaded silently fell back to the ``OPENAI_TIMEOUT``/``OPENAI_MAX_RETRIES`` defaults
instead of the configured values. This matches ``OpenAIChatGenerator``,
``OpenAITextEmbedder`` and ``OpenAIDocumentEmbedder``, which already serialize both.
16 changes: 16 additions & 0 deletions test/components/generators/test_openai_image_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ def test_to_dict(self) -> None:
"api_key": {"type": "env_var", "env_vars": ["OPENAI_API_KEY"], "strict": True},
"api_base_url": None,
"organization": None,
"timeout": None,
"max_retries": None,
"http_client_kwargs": None,
},
}
Expand All @@ -114,6 +116,8 @@ def test_to_dict_with_params(self) -> None:
"api_key": {"type": "env_var", "env_vars": ["EXAMPLE_API_KEY"], "strict": True},
"api_base_url": "https://api.openai.com",
"organization": "test-org",
"timeout": 60,
"max_retries": 10,
"http_client_kwargs": {"proxy": "http://localhost:8080"},
},
}
Expand All @@ -138,6 +142,18 @@ def test_from_dict(self) -> None:
assert generator.api_key.to_dict() == {"type": "env_var", "env_vars": ["OPENAI_API_KEY"], "strict": True}
assert generator.http_client_kwargs is None

def test_to_dict_from_dict_roundtrip_preserves_client_settings(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""`timeout` and `max_retries` decide the client the component builds, so a pipeline
that survives a save/load round trip must keep them. Without them in `to_dict` they
silently revert to the `OPENAI_TIMEOUT`/`OPENAI_MAX_RETRIES` fallbacks (30s, 5)."""
monkeypatch.setenv("OPENAI_API_KEY", "test-api-key")
generator = OpenAIImageGenerator(timeout=120.0, max_retries=10)
restored = OpenAIImageGenerator.from_dict(generator.to_dict())
assert restored.timeout == 120.0
assert restored.max_retries == 10
assert restored._client_kwargs()["timeout"] == 120.0
assert restored._client_kwargs()["max_retries"] == 10

def test_from_dict_default_params(self) -> None:
data = {
"type": "haystack.components.generators.openai_image_generator.OpenAIImageGenerator",
Expand Down
Loading