Skip to content

Running Ollama Cloud with Pydantic AI #2996

@knana1662

Description

@knana1662

Initial Checks

Description

I have this code where i am using ollama cloud with pydantic ai. Please kindly assist as i am currently getting the error below:

C:\Users\NanaKwameAsanteDanso>python -u "c:\Users\NanaKwameAsanteDanso\test_ollama.py"
Traceback (most recent call last):
  File "C:\Users\NanaKwameAsanteDanso\OneDrive - Dynamic Data Solutions Ltd\Desktop\core\Lib\site-packages\pydantic_ai\models\openai.py", line 523, in _process_response
    response = chat.ChatCompletion.model_validate(response.model_dump())
  File "C:\Users\NanaKwameAsanteDanso\OneDrive - Dynamic Data Solutions Ltd\Desktop\core\Lib\site-packages\pydantic\main.py", line 705, in model_validate
    return cls.__pydantic_validator__.validate_python(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        obj, strict=strict, from_attributes=from_attributes, context=context, by_alias=by_alias, by_name=by_name
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
pydantic_core._pydantic_core.ValidationError: 3 validation errors for ChatCompletion
id
  Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.11/v/string_type
choices.0.index
  Input should be a valid integer [type=int_type, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.11/v/int_type
object
  Input should be 'chat.completion' [type=literal_error, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.11/v/literal_error

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "c:\Users\NanaKwameAsanteDanso\test_ollama.py", line 17, in <module>
    response = agent.run_sync("Why is the sky blue?")  # Use run_sync instead of run
  File "C:\Users\NanaKwameAsanteDanso\OneDrive - Dynamic Data Solutions Ltd\Desktop\core\Lib\site-packages\pydantic_ai\agent\abstract.py", line 317, in run_sync
    return get_event_loop().run_until_complete(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        self.run(
        ^^^^^^^^^
    ...<12 lines>...
        )
        ^
    )
    ^
  File "C:\Users\NanaKwameAsanteDanso\AppData\Local\Programs\Python\Python313\Lib\asyncio\base_events.py", line 725, in run_until_complete
    return future.result()
           ~~~~~~~~~~~~~^^
  File "C:\Users\NanaKwameAsanteDanso\OneDrive - Dynamic Data Solutions Ltd\Desktop\core\Lib\site-packages\pydantic_ai\agent\abstract.py", line 218, in run
    async for node in agent_run:
    ...<4 lines>...
                await event_stream_handler(_agent_graph.build_run_context(agent_run.ctx), stream)
  File "C:\Users\NanaKwameAsanteDanso\OneDrive - Dynamic Data Solutions Ltd\Desktop\core\Lib\site-packages\pydantic_ai\run.py", line 149, in __anext__
    next_node = await self._graph_run.__anext__()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\NanaKwameAsanteDanso\OneDrive - Dynamic Data Solutions Ltd\Desktop\core\Lib\site-packages\pydantic_graph\graph.py", line 758, in __anext__
    return await self.next(self._next_node)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\NanaKwameAsanteDanso\OneDrive - Dynamic Data Solutions Ltd\Desktop\core\Lib\site-packages\pydantic_graph\graph.py", line 731, in next
    self._next_node = await node.run(ctx)
                      ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\NanaKwameAsanteDanso\OneDrive - Dynamic Data Solutions Ltd\Desktop\core\Lib\site-packages\pydantic_ai\_agent_graph.py", line 399, in run
    return await self._make_request(ctx)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\NanaKwameAsanteDanso\OneDrive - Dynamic Data Solutions Ltd\Desktop\core\Lib\site-packages\pydantic_ai\_agent_graph.py", line 441, in _make_request
    model_response = await ctx.deps.model.request(message_history, model_settings, model_request_parameters)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\NanaKwameAsanteDanso\OneDrive - Dynamic Data Solutions Ltd\Desktop\core\Lib\site-packages\pydantic_ai\models\openai.py", line 399, in request
    model_response = self._process_response(response)
  File "C:\Users\NanaKwameAsanteDanso\OneDrive - Dynamic Data Solutions Ltd\Desktop\core\Lib\site-packages\pydantic_ai\models\openai.py", line 525, in _process_response
    raise UnexpectedModelBehavior(f'Invalid response from OpenAI chat completions endpoint: {e}') from e
pydantic_ai.exceptions.UnexpectedModelBehavior: Invalid response from OpenAI chat completions endpoint: 3 validation errors for ChatCompletion
id
  Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.11/v/string_type
choices.0.index
  Input should be a valid integer [type=int_type, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.11/v/int_type
object
  Input should be 'chat.completion' [type=literal_error, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.11/v/literal_error

C:\Users\NanaKwameAsanteDanso>python -u "c:\Users\NanaKwameAsanteDanso\test_ollama.py"
Traceback (most recent call last):
  File "C:\Users\NanaKwameAsanteDanso\OneDrive - Dynamic Data Solutions Ltd\Desktop\core\Lib\site-packages\pydantic_ai\models\openai.py", line 523, in _process_response
    response = chat.ChatCompletion.model_validate(response.model_dump())
  File "C:\Users\NanaKwameAsanteDanso\OneDrive - Dynamic Data Solutions Ltd\Desktop\core\Lib\site-packages\pydantic\main.py", line 705, in model_validate
    return cls.__pydantic_validator__.validate_python(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        obj, strict=strict, from_attributes=from_attributes, context=context, by_alias=by_alias, by_name=by_name
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
pydantic_core._pydantic_core.ValidationError: 3 validation errors for ChatCompletion
id
  Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.11/v/string_type
choices.0.index
  Input should be a valid integer [type=int_type, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.11/v/int_type
object
  Input should be 'chat.completion' [type=literal_error, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.11/v/literal_error

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "c:\Users\NanaKwameAsanteDanso\test_ollama.py", line 17, in <module>
    response = agent.run_sync("Why is the sky blue?")  # Use run_sync instead of run
  File "C:\Users\NanaKwameAsanteDanso\OneDrive - Dynamic Data Solutions Ltd\Desktop\core\Lib\site-packages\pydantic_ai\agent\abstract.py", line 317, in run_sync
    return get_event_loop().run_until_complete(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        self.run(
        ^^^^^^^^^
    ...<12 lines>...
        )
        ^
    )
    ^
  File "C:\Users\NanaKwameAsanteDanso\AppData\Local\Programs\Python\Python313\Lib\asyncio\base_events.py", line 725, in run_until_complete
    return future.result()
           ~~~~~~~~~~~~~^^
  File "C:\Users\NanaKwameAsanteDanso\OneDrive - Dynamic Data Solutions Ltd\Desktop\core\Lib\site-packages\pydantic_ai\agent\abstract.py", line 218, in run
    async for node in agent_run:
    ...<4 lines>...
                await event_stream_handler(_agent_graph.build_run_context(agent_run.ctx), stream)
  File "C:\Users\NanaKwameAsanteDanso\OneDrive - Dynamic Data Solutions Ltd\Desktop\core\Lib\site-packages\pydantic_ai\run.py", line 149, in __anext__
    next_node = await self._graph_run.__anext__()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\NanaKwameAsanteDanso\OneDrive - Dynamic Data Solutions Ltd\Desktop\core\Lib\site-packages\pydantic_graph\graph.py", line 758, in __anext__
    return await self.next(self._next_node)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\NanaKwameAsanteDanso\OneDrive - Dynamic Data Solutions Ltd\Desktop\core\Lib\site-packages\pydantic_graph\graph.py", line 731, in next
    self._next_node = await node.run(ctx)
                      ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\NanaKwameAsanteDanso\OneDrive - Dynamic Data Solutions Ltd\Desktop\core\Lib\site-packages\pydantic_ai\_agent_graph.py", line 399, in run
    return await self._make_request(ctx)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\NanaKwameAsanteDanso\OneDrive - Dynamic Data Solutions Ltd\Desktop\core\Lib\site-packages\pydantic_ai\_agent_graph.py", line 441, in _make_request
    model_response = await ctx.deps.model.request(message_history, model_settings, model_request_parameters)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\NanaKwameAsanteDanso\OneDrive - Dynamic Data Solutions Ltd\Desktop\core\Lib\site-packages\pydantic_ai\models\openai.py", line 399, in request
    model_response = self._process_response(response)
  File "C:\Users\NanaKwameAsanteDanso\OneDrive - Dynamic Data Solutions Ltd\Desktop\core\Lib\site-packages\pydantic_ai\models\openai.py", line 525, in _process_response
    raise UnexpectedModelBehavior(f'Invalid response from OpenAI chat completions endpoint: {e}') from e
pydantic_ai.exceptions.UnexpectedModelBehavior: Invalid response from OpenAI chat completions endpoint: 3 validation errors for ChatCompletion
id
  Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.11/v/string_type
choices.0.index
  Input should be a valid integer [type=int_type, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.11/v/int_type
object
  Input should be 'chat.completion' [type=literal_error, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.11/v/literal_error

Example Code

from pydantic_ai.models.openai import OpenAIChatModel  # Updated class name
from pydantic_ai.providers.openai import OpenAIProvider  # Correct import
from pydantic_ai import Agent

# Ollama running with OpenAI-compatible endpoint (Ollama Cloud)
ollama_cloud_gpt_oss_120b = OpenAIChatModel(
    model_name="gpt-oss:120b",
    provider=OpenAIProvider(
        base_url="https://ollama.com/v1",   # OpenAI-compatible endpoint
        api_key="<api_key>"                       # API key if required
    )
)

# --- Usage Example ---
agent = Agent(ollama_cloud_gpt_oss_120b)

response = agent.run_sync("Why is the sky blue?")  # Use run_sync instead of run
print(response.output)  # Use response.output instead of response.data

Python, Pydantic AI & LLM client version

Python 3.13.2
pydantic-ai==1.0.10

Metadata

Metadata

Assignees

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