Skip to content
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
2 changes: 1 addition & 1 deletion haystack/components/generators/chat/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def to_dict(self) -> dict[str, Any]:
# If it's already a json schema, it's left as is
generation_kwargs = self.generation_kwargs.copy()
response_format = generation_kwargs.get("response_format")
if response_format and issubclass(response_format, BaseModel):
if response_format and isinstance(response_format, type) and issubclass(response_format, BaseModel):
json_schema = {
"type": "json_schema",
"json_schema": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
Fix crash in ``AzureOpenAIChatGenerator.to_dict()`` when ``response_format`` is passed as a dictionary.
16 changes: 15 additions & 1 deletion test/components/generators/chat/test_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,22 @@ def test_to_dict_with_parameters(self, monkeypatch: pytest.MonkeyPatch, calendar
},
}

def test_from_dict(self, monkeypatch: pytest.MonkeyPatch) -> None:
@pytest.mark.parametrize(
"rf",
[
{"type": "json_object"},
{"type": "json_schema", "json_schema": {"name": "MySchema", "strict": True, "schema": {}}},
],
)
def test_to_dict_with_dict_response_format(self, monkeypatch: pytest.MonkeyPatch, rf: dict[str, Any]) -> None:
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "test-api-key")
component = AzureOpenAIChatGenerator(
azure_endpoint="some-non-existing-endpoint", generation_kwargs={"response_format": rf}
)
data = component.to_dict()
assert data["init_parameters"]["generation_kwargs"]["response_format"] == rf

def test_from_dict(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "test-api-key")
monkeypatch.setenv("AZURE_OPENAI_AD_TOKEN", "test-ad-token")
data = {
Expand Down
Loading