Skip to content

Conversation

@rgbkrk
Copy link

@rgbkrk rgbkrk commented Jan 27, 2026

Allow passing a pre-configured httpx.Client or httpx.AsyncClient instance to reuse connections and custom configurations.

import httpx
from ollama import Client

custom_httpx_client = httpx.Client(timeout=30.0)
client = Client(client=custom_httpx_client)

messages = [
    {
      'role': 'user',
      'content': 'Why is the sky blue?',
    },
]

for part in client.chat('gpt-oss:120b-cloud', messages=messages, stream=True):
    print(part.message.content, end='', flush=True)

Note: this is an updated minimal version of #380 to support a custom httpx client.

I took the approach of checking for a class to match the old behavior instead of isinstance so that nothing has to be a direct instance of an httpx.Client or httpx.AsyncClient. It's up to the user to provide the right custom client.

Allow passing a pre-configured `httpx.Client` or `httpx.AsyncClient`
instance to reuse connections and custom configurations.

```python
import httpx
from ollama import Client

custom_httpx_client = httpx.Client(timeout=30.0)
client = Client(client=custom_httpx_client)

messages = [
    {
      'role': 'user',
      'content': 'Why is the sky blue?',
    },
]

for part in client.chat('gpt-oss:120b-cloud', messages=messages, stream=True):
    print(part.message.content, end='', flush=True)
```

Note: this is an updated minimal version of
ollama#380 to support a custom
`httpx` client.

I took the approach of checking for the class instead of isinstance so
that nothing has to be a direct instance of an httpx.Client or
httpx.AsyncClient. It's up to the user to provide the right custom
client.
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.

1 participant