feat(ollama): support response_format (#34612)

Fixes #34610

---

This PR resolves an issue where `ChatOllama` would raise an `unexpected
keyword argument 'response_format'` error when used with `create_agent`
or when passed an OpenAI-style `response_format`.

When using `create_agent` (especially with models like `gpt-oss`),
LangChain creates a `response_format` argument (e.g., `{"type":
"json_schema", ...}`). `ChatOllama` previously passed this argument
directly to the underlying Ollama client, which does not support
`response_format` and instead expects a `format` parameter.

## The Fix
I updated `_chat_params` in
`libs/partners/ollama/langchain_ollama/chat_models.py` to:
1.  Intercept the `response_format` argument.
2.  Map it to the native Ollama `format` parameter:
* `{"type": "json_schema", "json_schema": {"schema": ...}}` ->
`format=schema`
    *   `{"type": "json_object"}` -> `format="json"`
3.  Remove `response_format` from the kwargs passed to the client.

## Validation
* **Reproduction Script**: Verified the fix with a script covering
`json_schema`, `json_object`, and explicit `format` priority scenarios.
* **New Tests**: Added 3 new unit tests to
`libs/partners/ollama/tests/unit_tests/test_chat_models.py` covering
these scenarios.
* **Regression**: Ran the full test suite (`make -C libs/partners/ollama
test`), passing 29 tests (previously 26).
* **Lint/Format**: Verified with `make lint_package` and `make format`.

---------

Co-authored-by: Mohan Kumar Sagadevan <mohankumarsagadevan@Mohans-MacBook-Air.local>
Co-authored-by: Mason Daugherty <mason@langchain.dev>
Co-authored-by: Mason Daugherty <github@mdrxy.com>
This commit is contained in:
Mohan Kumar S
2026-04-07 07:53:57 +05:30
committed by GitHub
parent 2bc982b73c
commit 3beba77e2e
3 changed files with 369 additions and 2 deletions

View File

@@ -2,6 +2,7 @@
from __future__ import annotations
import json
from typing import Annotated
from unittest.mock import MagicMock, patch
@@ -68,7 +69,7 @@ def test_structured_output(method: str) -> None:
setup: str = Field(description="question to set up a joke")
punchline: str = Field(description="answer to resolve the joke")
llm = ChatOllama(model=DEFAULT_MODEL_NAME, temperature=0)
llm = ChatOllama(model=DEFAULT_MODEL_NAME, temperature=0.3)
query = "Tell me a joke about cats."
# Pydantic
@@ -112,6 +113,42 @@ def test_structured_output(method: str) -> None:
assert set(chunk.keys()) == {"setup", "punchline"}
@pytest.mark.parametrize(
"response_format",
[
{"type": "json_object"},
{
"type": "json_schema",
"json_schema": {
"name": "joke",
"schema": {
"type": "object",
"properties": {
"setup": {"type": "string"},
"punchline": {"type": "string"},
},
"required": ["setup", "punchline"],
},
},
},
],
ids=["json_object", "json_schema"],
)
def test_response_format(response_format: dict) -> None:
"""Test that OpenAI-style response_format is translated and honored."""
llm = ChatOllama(model=DEFAULT_MODEL_NAME, temperature=0)
result = llm.invoke(
[HumanMessage("Tell me a joke about cats. Return JSON with setup/punchline.")],
response_format=response_format,
)
assert isinstance(result, AIMessage)
parsed = json.loads(str(result.content))
assert isinstance(parsed, dict)
if response_format["type"] == "json_schema":
assert "setup" in parsed
assert "punchline" in parsed
@pytest.mark.parametrize(("model"), [(DEFAULT_MODEL_NAME)])
def test_structured_output_deeply_nested(model: str) -> None:
"""Test to verify structured output with a nested objects."""