Skip to content

Python: convert Pydantic model class response_format to JSON schema in OllamaChatClient#6782

Open
anneheartrecord wants to merge 1 commit into
microsoft:mainfrom
anneheartrecord:fix/ollama-response-format-pydantic-6781
Open

Python: convert Pydantic model class response_format to JSON schema in OllamaChatClient#6782
anneheartrecord wants to merge 1 commit into
microsoft:mainfrom
anneheartrecord:fix/ollama-response-format-pydantic-6781

Conversation

@anneheartrecord

Copy link
Copy Markdown
Contributor

Motivation and Context

Closes #6781.

OllamaChatClient forwarded response_format straight to Ollama's format param without converting a Pydantic model class to its JSON schema. Ollama's format only accepts '', 'json', or a JSON-schema dict, so passing a model class — the form OpenAIChatClient/FoundryChatClient accept, and the form create_harness_agent's plan mode uses — raised a ValidationError while building the request, before any call reached Ollama. This made structured output via a model class unusable on Ollama and broke harness plan mode on every Ollama model.

Description

In _prepare_options, when mapping response_formatformat, convert a Pydantic model class to its JSON schema (model_json_schema()), keeping the existing '' / 'json' / dict pass-through. The original response_format class is left untouched in options, so typed parsing of the response (ChatResponse.value → model instance) still works — matching the OpenAI/Foundry clients and Ollama's documented structured-output usage.

Added test_cmc_response_format_pydantic_model: asserts the outgoing format kwarg is the JSON schema dict (not the class) and that the response parses back into the model instance.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • I have added tests for my changes (added test_cmc_response_format_pydantic_model; full ollama suite green)
  • ruff format/check, pyright, and mypy pass on the changed files

…n OllamaChatClient

Ollama's `format` param only accepts '', 'json', or a JSON-schema dict, so
passing a Pydantic model class (the form OpenAIChatClient/FoundryChatClient and
create_harness_agent plan mode use) raised a ValidationError while building the
request. Convert a model class to its JSON schema when mapping response_format
-> format, keeping the original class for typed response parsing.
Copilot AI review requested due to automatic review settings June 28, 2026 02:16
@moonbox3 moonbox3 added the python Usage: [Issues, PRs], Target: Python label Jun 28, 2026

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.

Pull request overview

Fixes structured-output support for the Python OllamaChatClient when callers provide options["response_format"] as a Pydantic model class (the same form accepted by other providers and used by harness plan mode). The client now converts the model class to a JSON Schema dict for Ollama’s format parameter while preserving the original class for typed response parsing.

Changes:

  • Convert Pydantic model classes passed via response_format into model_json_schema() when translating to Ollama’s format request parameter.
  • Update option documentation to explicitly describe support for Pydantic model classes.
  • Add a unit test asserting the outgoing format is a JSON Schema dict and the parsed response is still an instance of the model.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
python/packages/ollama/agent_framework_ollama/_chat_client.py Converts response_format model classes to JSON Schema for Ollama request format, preserving the class for typed parsing.
python/packages/ollama/tests/test_ollama_chat_client.py Adds coverage for model-class response_format conversion + typed response parsing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

3 participants