Skip to content

Commit a341e56

Browse files
authored
docs: document how to use a custom AsyncOpenAI in the provider (#2073)
1 parent 64a1186 commit a341e56

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

docs/models/openai.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,21 @@ agent = Agent(model)
6161

6262
`OpenAIProvider` also accepts a custom `AsyncOpenAI` client via the `openai_client` parameter, so you can customise the `organization`, `project`, `base_url` etc. as defined in the [OpenAI API docs](https://platform.openai.com/docs/api-reference).
6363

64-
You could also use the [`AsyncAzureOpenAI`](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/switching-endpoints) client to use the Azure OpenAI API.
64+
```python {title="custom_openai_client.py"}
65+
from openai import AsyncOpenAI
66+
67+
from pydantic_ai import Agent
68+
from pydantic_ai.models.openai import OpenAIModel
69+
from pydantic_ai.providers.openai import OpenAIProvider
70+
71+
client = AsyncOpenAI(max_retries=3)
72+
model = OpenAIModel('gpt-4o', provider=OpenAIProvider(openai_client=client))
73+
agent = Agent(model)
74+
...
75+
```
76+
77+
You could also use the [`AsyncAzureOpenAI`](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/switching-endpoints) client
78+
to use the Azure OpenAI API. Note that the `AsyncAzureOpenAI` is a subclass of `AsyncOpenAI`.
6579

6680
```python
6781
from openai import AsyncAzureOpenAI

0 commit comments

Comments
 (0)