Skip to content

Conversation

@dsfaccini
Copy link
Contributor

@dsfaccini dsfaccini commented Nov 20, 2025

currently the doc only shows how to instantiate an Agent using the shorthand, this PR adds an example for passing the api key and also one example to use a non-default upstream provider

This is the view from http://localhost:8000/api/providers/#pydantic-ai-gateway-provider i.e. https://ai.pydantic.dev/api/providers/#pydantic-ai-gateway-provider

image

docs/gateway.md Outdated
from pydantic_ai.models.openai import OpenAIChatModel

provider = gateway_provider('openai', api_key='PYDANTIC_AI_GATEWAY_API_KEY')
model = OpenAIChatModel('gpt-5', provider=provider, route='modal-ai')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

route is an arg on gateway_provider not the model class, rightr?

docs/gateway.md Outdated
The first known use of "hello, world" was in a 1974 textbook about the C programming language.
"""
```
```python {title="passing_api_key.py"}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this to the "Usage > Pydantic AI" section below, and make sure there's some text before each code sample explaining what it shows.

docs/gateway.md Outdated

=== "Using a different upstream provider"

If you're using a different upstream provider, you can specify it in the route parameter:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmontagu Can you review the terminology here please? I don't think route and upstream provider are interchangeable like this

```python {title="passing_api_key.py"}
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.gateway import gateway_provider
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should link to the API docs for this function, so people can read the docstrings etc

docs/gateway.md Outdated
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.gateway import gateway_provider

provider = gateway_provider('openai', api_key='PYDANTIC_AI_GATEWAY_API_KEY')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, but I feel like it makes sense to make it more clear that this isn't the literal string you are supposed to enter

Suggested change
provider = gateway_provider('openai', api_key='PYDANTIC_AI_GATEWAY_API_KEY')
provider = gateway_provider('openai', api_key='paig_<example_key>')

or similar. (If it needs to match elsewhere, we can change it elsewhere?)

The current value from this example is the exact name of the environment variable that is used, which imo has just a little bit of potential for confusion

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might make sense to make use of the route parameter too as that's a particularly relevant one; if you do, I'd use route='builtin-openai' or similar — those should always work.

(route='builtin-openai' means to use specifically the builtin openai provider, even if you've added your own OpenAI provider to the builtin routing group (which will have fallback.).)

I'm okay leaving that out for now, up to you.

docs/gateway.md Outdated

provider = gateway_provider(
'openai',
api_key='PYDANTIC_AI_GATEWAY_API_KEY',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same suggestion here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants