langchain - ✅(Solved) Fix [Bug]: ChatOllama crashes on Optional fields due to anyOf: [T, null] schema pattern [2 pull requests, 2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
langchain-ai/langchain#36032Fetched 2026-04-08 00:52:56
View on GitHub
Comments
2
Participants
2
Timeline
6
Reactions
0
Author
Participants
Timeline (top)
commented ×2cross-referenced ×2labeled ×1referenced ×1

Fix Action

Fixed

PR fix notes

PR #36033: fix(ollama): flatten Pydantic v2 anyOf schema for tool calling compatibility

Description (problem / solution / changelog)

Closes #36032. Ollama's model templates expect flat schemas and do not handle the anyOf: [T, null] pattern that Pydantic v2 generates for Optional fields. This recursively collapses those to their base type within ChatOllama.bind_tools.

Changed files

  • libs/partners/ollama/langchain_ollama/chat_models.py (modified, +46/-1)

PR #36028: fix(ollama): flatten Pydantic v2 anyOf schema for tool calling compatibility

Description (problem / solution / changelog)

Closes #36032. Ollama's model templates expect flat schemas and do not handle the anyOf: [T, null] pattern that Pydantic v2 generates for Optional fields. This recursively collapses those to their base type within ChatOllama.bind_tools.

Changed files

  • libs/partners/ollama/langchain_ollama/chat_models.py (modified, +46/-1)
RAW_BUFFERClick to expand / collapse

Ollama's model templates expect flat schemas and do not handle the anyOf: [T, null] pattern that Pydantic v2 generates for Optional fields. This causes crashes during tool binding in ChatOllama.

extent analysis

Fix Plan

To resolve the issue with Ollama's model templates not handling the anyOf: [T, null] pattern generated by Pydantic v2 for Optional fields, we need to modify the schema generation to handle this pattern.

Steps to Fix

  • Update Pydantic to use a custom JSON schema generator that handles Optional fields correctly.
  • Modify the Ollama model templates to handle the anyOf pattern.

Example Code

from pydantic import BaseModel
from pydantic.schema import date, datetime, UUID
from typing import Optional

# Define a custom JSON schema generator
def custom_json_schema(model: BaseModel):
    schema = model.schema()
    properties = schema["properties"]
    for prop, value in properties.items():
        if "anyOf" in value and len(value["anyOf"]) == 2 and value["anyOf"][1] == {"type": "null"}:
            # Replace anyOf with a nullable type
            properties[prop] = {"type": ["null", value["anyOf"][0]["type"]]}
    return schema

# Define a Pydantic model with an Optional field
class ExampleModel(BaseModel):
    id: int
    name: Optional[str]

# Generate the custom JSON schema
example_schema = custom_json_schema(ExampleModel)
print(example_schema)

Verification

To verify the fix, check that the generated JSON schema for the ExampleModel no longer contains the anyOf pattern for the name field, and that Ollama's model templates can handle the updated schema without crashing.

Extra Tips

  • Make sure to update all Pydantic models to use the custom JSON schema generator.
  • Test the updated schema with Ollama's model templates to ensure compatibility.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING