Python: convert Pydantic model class response_format to JSON schema in OllamaChatClient#6782
Open
anneheartrecord wants to merge 1 commit into
Open
Conversation
…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.
Contributor
There was a problem hiding this comment.
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_formatintomodel_json_schema()when translating to Ollama’sformatrequest parameter. - Update option documentation to explicitly describe support for Pydantic model classes.
- Add a unit test asserting the outgoing
formatis 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
Closes #6781.
OllamaChatClientforwardedresponse_formatstraight to Ollama'sformatparam without converting a Pydantic model class to its JSON schema. Ollama'sformatonly accepts'','json', or a JSON-schema dict, so passing a model class — the formOpenAIChatClient/FoundryChatClientaccept, and the formcreate_harness_agent's plan mode uses — raised aValidationErrorwhile 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 mappingresponse_format→format, convert a Pydantic model class to its JSON schema (model_json_schema()), keeping the existing''/'json'/ dict pass-through. The originalresponse_formatclass is left untouched inoptions, 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 outgoingformatkwarg is the JSON schema dict (not the class) and that the response parses back into the model instance.Contribution Checklist
test_cmc_response_format_pydantic_model; full ollama suite green)