mirror of
https://github.com/hwchase17/langchain.git
synced 2026-06-09 10:17:00 +00:00
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:
@@ -792,12 +792,17 @@ class ChatOllama(BaseChatModel):
|
||||
if v is not None
|
||||
}
|
||||
|
||||
format_param = self._resolve_format_param(
|
||||
kwargs.pop("format", self.format),
|
||||
kwargs.pop("response_format", None),
|
||||
)
|
||||
|
||||
params = {
|
||||
"messages": ollama_messages,
|
||||
"stream": kwargs.pop("stream", True),
|
||||
"model": kwargs.pop("model", self.model),
|
||||
"think": kwargs.pop("reasoning", self.reasoning),
|
||||
"format": kwargs.pop("format", self.format),
|
||||
"format": format_param,
|
||||
"logprobs": kwargs.pop("logprobs", self.logprobs),
|
||||
"top_logprobs": kwargs.pop("top_logprobs", self.top_logprobs),
|
||||
"options": options_dict,
|
||||
@@ -815,6 +820,107 @@ class ChatOllama(BaseChatModel):
|
||||
|
||||
return params
|
||||
|
||||
def _resolve_format_param(
|
||||
self,
|
||||
format_param: str | dict[str, Any] | None,
|
||||
response_format: Any | None,
|
||||
) -> str | dict[str, Any] | None:
|
||||
"""Resolve the format parameter.
|
||||
|
||||
Converts an OpenAI-style `response_format` dict to the `format`
|
||||
parameter expected by Ollama.
|
||||
|
||||
Args:
|
||||
format_param: The explicit `format` value (takes priority).
|
||||
response_format: An OpenAI-style `response_format` dict.
|
||||
|
||||
Returns:
|
||||
The resolved format value to pass to the Ollama client.
|
||||
"""
|
||||
if format_param is not None:
|
||||
if response_format is not None:
|
||||
warnings.warn(
|
||||
"Both 'format' and 'response_format' were provided. "
|
||||
"'response_format' will be ignored in favor of 'format'.",
|
||||
UserWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
return format_param
|
||||
|
||||
if response_format is None:
|
||||
return None
|
||||
|
||||
return self._convert_response_format(response_format)
|
||||
|
||||
def _convert_response_format(
|
||||
self,
|
||||
response_format: Any,
|
||||
) -> str | dict[str, Any] | None:
|
||||
"""Convert an OpenAI-style `response_format` to an Ollama `format` value.
|
||||
|
||||
Args:
|
||||
response_format: The `response_format` value to convert.
|
||||
|
||||
Returns:
|
||||
The Ollama-compatible `format` value, or `None` if conversion fails.
|
||||
"""
|
||||
if not isinstance(response_format, dict):
|
||||
warnings.warn(
|
||||
f"Ignored invalid 'response_format' type: {type(response_format)}. "
|
||||
"Expected a dictionary.",
|
||||
UserWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
return None
|
||||
|
||||
fmt_type = response_format.get("type")
|
||||
if fmt_type == "json_object":
|
||||
return "json"
|
||||
if fmt_type == "json_schema":
|
||||
return self._extract_json_schema(response_format)
|
||||
|
||||
warnings.warn(
|
||||
f"Ignored unrecognized 'response_format' type: {fmt_type}. "
|
||||
"Expected 'json_object' or 'json_schema'.",
|
||||
UserWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
return None
|
||||
|
||||
def _extract_json_schema(
|
||||
self,
|
||||
response_format: dict[str, Any],
|
||||
) -> dict[str, Any] | None:
|
||||
"""Extract the raw JSON schema from an OpenAI ``json_schema`` envelope.
|
||||
|
||||
Args:
|
||||
response_format: A dict with ``type: "json_schema"``.
|
||||
|
||||
Returns:
|
||||
The raw JSON schema dict, or ``None`` if extraction fails.
|
||||
"""
|
||||
json_schema_block = response_format.get("json_schema")
|
||||
if not isinstance(json_schema_block, dict):
|
||||
warnings.warn(
|
||||
"response_format has type 'json_schema' but 'json_schema' "
|
||||
f"value is {type(json_schema_block)}, expected a dict "
|
||||
"containing a 'schema' key. "
|
||||
"The format parameter will not be set.",
|
||||
UserWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
return None
|
||||
schema = json_schema_block.get("schema")
|
||||
if schema is None:
|
||||
warnings.warn(
|
||||
"response_format has type 'json_schema' but no 'schema' "
|
||||
"key was found in 'json_schema'. "
|
||||
"The format parameter will not be set.",
|
||||
UserWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
return schema
|
||||
|
||||
@model_validator(mode="after")
|
||||
def _set_clients(self) -> Self:
|
||||
"""Set clients to use for ollama."""
|
||||
|
||||
Reference in New Issue
Block a user