Skip to content

fix: AzureOpenAIChatGenerator.to_dict() crashes when response_format is a plain dict - #12407

Merged
sjrl merged 3 commits into
deepset-ai:mainfrom
rautaditya2606:fix-azure-chat-generator-to-dict-response-format
Aug 20, 2026
Merged

fix: AzureOpenAIChatGenerator.to_dict() crashes when response_format is a plain dict#12407
sjrl merged 3 commits into
deepset-ai:mainfrom
rautaditya2606:fix-azure-chat-generator-to-dict-response-format

Conversation

@rautaditya2606

Copy link
Copy Markdown
Contributor

Related Issues

Closes #12406

Proposed Changes

  • Added the missing isinstance(response_format, type) guard before calling issubclass(response_format, BaseModel) in AzureOpenAIChatGenerator.to_dict().
  • Added a parametrized regression test (test_to_dict_with_dict_response_format) in test/components/generators/chat/test_azure.py covering both dictionary-based structured output formats: {"type": "json_object"} and {"type": "json_schema", ...}.
  • Added release note in releasenotes/notes/fix-azure-chat-generator-to-dict-response-format-1b844d5eaadcbd81.yaml.

How did you test it?

hatch -e test run pytest test/components/generators/chat/test_azure.py

30 passed, 5 skipped in 0.57s.

hatch -e test run pytest test/components/generators/chat/test_azure.py -k test_to_dict_with_dict_response_format

2 passed, 34 deselected in 0.19s.

Notes for the reviewer

  • Root cause: issubclass() expects a class (type) as its first argument. When response_format is provided in generation_kwargs as a plain dictionary (standard for OpenAI JSON mode and Structured Outputs), evaluating issubclass(dict_instance, BaseModel) raises an unhandled TypeError: issubclass() arg 1 must be a class during to_dict() serialization.

  • This change aligns AzureOpenAIChatGenerator.to_dict() with the existing pattern in OpenAIChatGenerator.to_dict() (haystack/components/generators/chat/openai.py) and AzureOpenAIResponsesGenerator.to_dict() (haystack/components/generators/chat/azure_responses.py).

  • Reproducer:

    from haystack.components.generators.chat import AzureOpenAIChatGenerator

    generator = AzureOpenAIChatGenerator(
        azure_endpoint="https://example-resource.azure.openai.com/",
        generation_kwargs={"response_format": {"type": "json_object"}},
    )

    # Before fix -> TypeError: issubclass() arg 1 must be a class
    # After fix  -> serializes successfully with response_format preserved
    generator.to_dict()

Checklist

  • Read contributors guidelines and code of conduct
  • Updated related issue
  • Added unit tests and updated docstrings
  • Used conventional commit type in PR title (fix: AzureOpenAIChatGenerator.to_dict() crashes when response_format is a plain dict)
  • Documented code
  • Added release note (releasenotes/notes/fix-azure-chat-generator-to-dict-response-format-1b844d5eaadcbd81.yaml)
  • Run pre-commit hooks and fixed any issues

@rautaditya2606
rautaditya2606 requested a review from a team as a code owner August 19, 2026 10:41
@rautaditya2606
rautaditya2606 requested review from bogdankostic and a lite review from Copilot and removed request for a team August 19, 2026 10:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@vercel

vercel Bot commented Aug 19, 2026

Copy link
Copy Markdown

@rautaditya2606 is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions

github-actions Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/generators/chat
  azure.py
Project Total  

This report was generated by python-coverage-comment-action

Comment thread test/components/generators/chat/test_azure.py Outdated
@sjrl

sjrl commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

@bogdankostic I can take this review!

@sjrl
sjrl removed the request for review from bogdankostic August 20, 2026 13:03
@github-actions github-actions Bot added the type:documentation Improvements on the docs label Aug 20, 2026

@sjrl sjrl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@sjrl
sjrl merged commit 6fd166e into deepset-ai:main Aug 20, 2026
22 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AzureOpenAIChatGenerator.to_dict() raises TypeError when response_format is a plain dict

3 participants