diff --git a/haystack/components/generators/chat/azure.py b/haystack/components/generators/chat/azure.py index 5b78d03c35..f5907fcdbe 100644 --- a/haystack/components/generators/chat/azure.py +++ b/haystack/components/generators/chat/azure.py @@ -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": { diff --git a/releasenotes/notes/fix-azure-chat-generator-to-dict-response-format-1b844d5eaadcbd81.yaml b/releasenotes/notes/fix-azure-chat-generator-to-dict-response-format-1b844d5eaadcbd81.yaml new file mode 100644 index 0000000000..d6772df2f4 --- /dev/null +++ b/releasenotes/notes/fix-azure-chat-generator-to-dict-response-format-1b844d5eaadcbd81.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - | + Fix crash in ``AzureOpenAIChatGenerator.to_dict()`` when ``response_format`` is passed as a dictionary. diff --git a/test/components/generators/chat/test_azure.py b/test/components/generators/chat/test_azure.py index 7b0c1ad019..42c4908834 100644 --- a/test/components/generators/chat/test_azure.py +++ b/test/components/generators/chat/test_azure.py @@ -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 = {