Skip to content

[Bug]: Structured Output not working with Ollama #10616

Closed
@rosmur

Description

@rosmur

What happened?

Following the example here:

Running the following code:

messages=[
        {"role": "system", "content": "Extract the event information."},
        {"role": "user", "content": "Alice and Bob are going to a science fair on Friday."},
    ]

litellm.enable_json_schema_validation = True
litellm.set_verbose = True # see the raw request made by litellm

class CalendarEvent(BaseModel):
  name: str 
  date: str
  participants: list[str]

resp = completion(
    model="ollama/deepcoder",
    messages=messages,
    response_format=CalendarEvent,
    api_base="http://localhost:11434"
)

print("Received={}".format(resp)) 

Results in the error Access raw response with e.raw_response.

Full traceback below:

Relevant log output

---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
File ~/localcode_mbpm1/tac/.venv/lib/python3.9/site-packages/litellm/litellm_core_utils/json_validation_rule.py:17, in validate_schema(schema, response)
     16 try:
---> 17     response_dict = json.loads(response)
     18 except json.JSONDecodeError:

File /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/__init__.py:346, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    343 if (cls is None and object_hook is None and
    344         parse_int is None and parse_float is None and
    345         parse_constant is None and object_pairs_hook is None and not kw):
--> 346     return _default_decoder.decode(s)
    347 if cls is None:

File /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/decoder.py:337, in JSONDecoder.decode(self, s, _w)
    333 """Return the Python representation of ``s`` (a ``str`` instance
    334 containing a JSON document).
    335 
    336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338 end = _w(s, end).end()

File /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/decoder.py:355, in JSONDecoder.raw_decode(self, s, idx)
    354 except StopIteration as err:
--> 355     raise JSONDecodeError("Expecting value", s, err.value) from None
...
- **Event Name:** Science Fair
- **Date:** Friday
- **Location:** Not specified in the message
- **Attendees:** Alice and Bob.
Access raw response with `e.raw_response`
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...

Are you a ML Ops Team?

No

What LiteLLM version are you on ?

1.68.0

Twitter / LinkedIn details

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions