Skip to content
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

Pydantic AI giving connection error when trying to use ollama locally #956

Open
This-Is-NPC opened this issue Feb 20, 2025 · 1 comment
Open

Comments

@This-Is-NPC
Copy link

I'm getting the error even though I set the api_key to keys with the same format as openai

Python 3.13.1
pydantic-ai==0.0.24
openai==1.63.2

Error

  File "C:\Users\xxxx\AppData\Roaming\Python\Python313\site-packages\openai\_base_client.py", line 1623, in _request
    raise APIConnectionError(request=request) from err
openai.APIConnectionError: Connection error.

My Code

from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel

name_model = 'llama3.2:3b'
ollama_url = 'http://localhost:11434/v1'
openai_key = 'SkllQ4R6e2vL9K3Gg5z2B'

model_ollama = OpenAIModel(
    model_name = name_model,
    base_url = ollama_url,
    api_key = openai_key
)

agent = Agent(
    model=model_ollama,
    system_prompt=['Reply in one sentence']
)

response = agent.run_sync('What is the capital of japan?')
print(response.data)

Originally posted by @This-Is-NPC in #812

Opening a new issue because the problem is different

Yes, my ollama server is working, when I use the ollama library everything works normally, only when I use Pydantic it doesn't. note that the error is different from the mentioned issue, before the error was due to the api_key property being empty and now it is an error when trying to execute the request with an invalid api_key.

Other Error

openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

My error:

openai.APIConnectionError: Connection error
@CarlosR759
Copy link

CarlosR759 commented Feb 20, 2025

I'm also having that error. I manage to make pydantic-ai work with release 0.0.19 and with the latest realease of ollama locally but not using the OpenAiModel import, just only using the Agent import like this:

pip install pydantic-ai[logfire]==0.0.19 --force-reinstall

after that launch the model like this:

agent = Agent('yourModelName')

I went back until 0.0.17 and using the OpenAIModel still does have either one of the errors mentioned before. If I'm not wrong the error changes in release 0.0.20 to

openai.APIConnectionError: Connection error

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

No branches or pull requests

2 participants