Skip to content

Fixed Ollama Structured Response not working #10616 #10617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 12, 2025

Conversation

imdigitalashish
Copy link
Contributor

@imdigitalashish imdigitalashish commented May 7, 2025

Title

feat(ollama): Allow direct Pydantic model usage in response_format for ollama_chat

Relevant issues

Fixes #10616

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • have added a screenshot of my new test passing locally
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem

Type

🐛 Bug Fix

Changes

The key changes are:

  1. Bug Resolution: This resolves the underlying issue that led to KeyError: 'schema' when users attempted to use Pydantic models without the precise nested structure, as reported in [Bug]: Structured Output not working with Ollama #10616. The PR ensures the schema is correctly processed by litellm before being passed to the Ollama API.

These changes enhance the developer experience for using structured outputs with Ollama via litellm and address the incompatibility noted in the referenced issue.

image

Copy link

vercel bot commented May 7, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
litellm ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 8, 2025 9:50am

@CLAassistant
Copy link

CLAassistant commented May 7, 2025

CLA assistant check
All committers have signed the CLA.

The response_format parameter conversion is already handled in utils.py's
get_optional_params function, making the duplicate check in ollama_chat.py
unnecessary. This change removes the redundant code while maintaining the
same functionality.
@yelonek
Copy link

yelonek commented May 12, 2025

Hi @imdigitalashish and @krrishdholakia, the lack of structured output in ollama via litellm drove me crazy far enough that I looked into it today.

I see you check for type json_object and json_schema, but what is sent as a parameter to response_format is the result of model_json_schema() and that is a dict where type is set to "object", not "json_object".

image

I just added additional if and then it worked as expected:

if param == "response_format" and value["type"] == "object":
                optional_params["format"] = value

I am using pydantic '2.11.4' and litellm '1.69.1'

Let me know what you think!

@krrishdholakia
Copy link
Contributor

Hey @yelonek is this an alternative to @imdigitalashish's PR?

if so - can you file it separately with a unit test in tests/litellm?

@krrishdholakia krrishdholakia merged commit 0eb0cf4 into BerriAI:main May 12, 2025
5 checks passed
@yelonek
Copy link

yelonek commented May 14, 2025

Hey @yelonek is this an alternative to @imdigitalashish's PR?

if so - can you file it separately with a unit test in tests/litellm?

I think it is complementary.
First I need to figure out how to set up the dev environment for litellm. So far I figured out, that Windows will not cut it. ;)

@krrishdholakia
Copy link
Contributor

What's the issue you see? @yelonek

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Structured Output not working with Ollama
4 participants