-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add OpenRouterModel as OpenAIChatModel subclass #3089
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
Open
ajac-zero
wants to merge
45
commits into
pydantic:main
Choose a base branch
from
ajac-zero:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
227e873
Add OpenRouter support and test coverage
ajac-zero c3c1546
Add OpenRouter reasoning config and refactor response details
ajac-zero 10a1a17
Move OpenRouterModelSettings import into try block
ajac-zero 5e64a62
Update pydantic_ai_slim/pydantic_ai/models/openrouter.py
ajac-zero 5e787da
Merge branch 'main' into main
ajac-zero e219b8c
Handle OpenRouter errors and extract response metadata
ajac-zero c5e0600
Merge branch 'pydantic:main' into main
ajac-zero 6f99fb2
Add type ignores to tests
ajac-zero 83d14b1
Merge branch 'pydantic:main' into main
ajac-zero ef3c6dd
Send back reasoning_details/signature
ajac-zero 0ba3691
Merge branch 'pydantic:main' into main
ajac-zero ed9e7df
add OpenRouterChatCompletion model
ajac-zero 0689e29
Merge branch 'pydantic:main' into main
ajac-zero 75adbb4
Update pydantic_ai_slim/pydantic_ai/models/openrouter.py
ajac-zero ab9d690
Update pydantic_ai_slim/pydantic_ai/models/openrouter.py
ajac-zero db1630d
Merge branch 'main' into main
ajac-zero 5700a19
fix spelling mistake
ajac-zero ee93121
add openrouter web plugin
ajac-zero ca45f8a
WIP build reasoning_details from ThinkingParts
ajac-zero 113c1cb
Merge branch 'main' into main
ajac-zero 5db26d0
Merge branch 'main' into main
ajac-zero 91bee62
Merge branch 'main' into main
ajac-zero b325816
wip reasoning details conversion
ajac-zero 1db529f
finish openrouter thinking part
ajac-zero 3d7f1b4
add preserve reasoning tokens test
ajac-zero 1d7a8a4
fix typing
ajac-zero e81621b
Merge branch 'main' into main
ajac-zero c6aca8d
Merge branch 'main' into main
ajac-zero 516e823
remove <thinking> tags from content
ajac-zero b8406d0
Merge branch 'main' into main
ajac-zero c16c960
fix typing
ajac-zero 63d1b84
Merge branch 'main' into main
ajac-zero 0835073
Merge branch 'main' into main
ajac-zero baede41
add _map_model_response method
ajac-zero 89ef9a8
move assert_never import to typing_extensions
ajac-zero ebc8d08
add tool calling test
ajac-zero 21a78e4
replace process_response with hooks
ajac-zero 0b37792
add stream hooks
ajac-zero 8d090f0
simplify hooks
ajac-zero e8c3c81
fix coverage/linting
ajac-zero 02b8527
Merge branch 'main' into main
ajac-zero 895ea03
Merge branch 'main' into main
ajac-zero 7c50f07
fix lint
ajac-zero 8e32475
replace OpenRouterThinking with encoding in 'id'
ajac-zero 9d57be0
Merge branch 'main' into main
ajac-zero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
tests/models/cassettes/test_openrouter/test_openrouter_errors_raised.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| interactions: | ||
| - request: | ||
| headers: | ||
| accept: | ||
| - application/json | ||
| accept-encoding: | ||
| - gzip, deflate | ||
| connection: | ||
| - keep-alive | ||
| content-length: | ||
| - '158' | ||
| content-type: | ||
| - application/json | ||
| host: | ||
| - openrouter.ai | ||
| method: POST | ||
| parsed_body: | ||
| messages: | ||
| - content: Be helpful. | ||
| role: system | ||
| - content: Tell me a joke. | ||
| role: user | ||
| model: google/gemini-2.0-flash-exp:free | ||
| stream: false | ||
| uri: https://openrouter.ai/api/v1/chat/completions | ||
| response: | ||
| headers: | ||
| access-control-allow-origin: | ||
| - '*' | ||
| connection: | ||
| - keep-alive | ||
| content-type: | ||
| - application/json | ||
| permissions-policy: | ||
| - payment=(self "https://checkout.stripe.com" "https://connect-js.stripe.com" "https://js.stripe.com" "https://*.js.stripe.com" | ||
| "https://hooks.stripe.com") | ||
| referrer-policy: | ||
| - no-referrer, strict-origin-when-cross-origin | ||
| transfer-encoding: | ||
| - chunked | ||
| vary: | ||
| - Accept-Encoding | ||
| parsed_body: | ||
| error: | ||
| code: 429 | ||
| message: Provider returned error | ||
| metadata: | ||
| provider_name: Google | ||
| raw: 'google/gemini-2.0-flash-exp:free is temporarily rate-limited upstream. Please retry shortly, or add your own | ||
| key to accumulate your rate limits: https://openrouter.ai/settings/integrations' | ||
| user_id: user_2wT5ElBE4Es3R4QrNLpZiXICmQP | ||
| status: | ||
| code: 429 | ||
| message: Too Many Requests | ||
| - request: | ||
| headers: | ||
| accept: | ||
| - application/json | ||
| accept-encoding: | ||
| - gzip, deflate | ||
| connection: | ||
| - keep-alive | ||
| content-length: | ||
| - '158' | ||
| content-type: | ||
| - application/json | ||
| host: | ||
| - openrouter.ai | ||
| method: POST | ||
| parsed_body: | ||
| messages: | ||
| - content: Be helpful. | ||
| role: system | ||
| - content: Tell me a joke. | ||
| role: user | ||
| model: google/gemini-2.0-flash-exp:free | ||
| stream: false | ||
| uri: https://openrouter.ai/api/v1/chat/completions | ||
| response: | ||
| headers: | ||
| access-control-allow-origin: | ||
| - '*' | ||
| connection: | ||
| - keep-alive | ||
| content-type: | ||
| - application/json | ||
| permissions-policy: | ||
| - payment=(self "https://checkout.stripe.com" "https://connect-js.stripe.com" "https://js.stripe.com" "https://*.js.stripe.com" | ||
| "https://hooks.stripe.com") | ||
| referrer-policy: | ||
| - no-referrer, strict-origin-when-cross-origin | ||
| transfer-encoding: | ||
| - chunked | ||
| vary: | ||
| - Accept-Encoding | ||
| parsed_body: | ||
| error: | ||
| code: 429 | ||
| message: Provider returned error | ||
| metadata: | ||
| provider_name: Google | ||
| raw: 'google/gemini-2.0-flash-exp:free is temporarily rate-limited upstream. Please retry shortly, or add your own | ||
| key to accumulate your rate limits: https://openrouter.ai/settings/integrations' | ||
| user_id: user_2wT5ElBE4Es3R4QrNLpZiXICmQP | ||
| status: | ||
| code: 429 | ||
| message: Too Many Requests | ||
| - request: | ||
| headers: | ||
| accept: | ||
| - application/json | ||
| accept-encoding: | ||
| - gzip, deflate | ||
| connection: | ||
| - keep-alive | ||
| content-length: | ||
| - '158' | ||
| content-type: | ||
| - application/json | ||
| host: | ||
| - openrouter.ai | ||
| method: POST | ||
| parsed_body: | ||
| messages: | ||
| - content: Be helpful. | ||
| role: system | ||
| - content: Tell me a joke. | ||
| role: user | ||
| model: google/gemini-2.0-flash-exp:free | ||
| stream: false | ||
| uri: https://openrouter.ai/api/v1/chat/completions | ||
| response: | ||
| headers: | ||
| access-control-allow-origin: | ||
| - '*' | ||
| connection: | ||
| - keep-alive | ||
| content-type: | ||
| - application/json | ||
| permissions-policy: | ||
| - payment=(self "https://checkout.stripe.com" "https://connect-js.stripe.com" "https://js.stripe.com" "https://*.js.stripe.com" | ||
| "https://hooks.stripe.com") | ||
| referrer-policy: | ||
| - no-referrer, strict-origin-when-cross-origin | ||
| transfer-encoding: | ||
| - chunked | ||
| vary: | ||
| - Accept-Encoding | ||
| parsed_body: | ||
| error: | ||
| code: 429 | ||
| message: Provider returned error | ||
| metadata: | ||
| provider_name: Google | ||
| raw: 'google/gemini-2.0-flash-exp:free is temporarily rate-limited upstream. Please retry shortly, or add your own | ||
| key to accumulate your rate limits: https://openrouter.ai/settings/integrations' | ||
| user_id: user_2wT5ElBE4Es3R4QrNLpZiXICmQP | ||
| status: | ||
| code: 429 | ||
| message: Too Many Requests | ||
| version: 1 |
96 changes: 96 additions & 0 deletions
96
tests/models/cassettes/test_openrouter/test_openrouter_map_messages_reasoning.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| interactions: | ||
| - request: | ||
| headers: | ||
| accept: | ||
| - application/json | ||
| accept-encoding: | ||
| - gzip, deflate | ||
| connection: | ||
| - keep-alive | ||
| content-length: | ||
| - '133' | ||
| content-type: | ||
| - application/json | ||
| host: | ||
| - openrouter.ai | ||
| method: POST | ||
| parsed_body: | ||
| messages: | ||
| - content: Who are you. Think about it. | ||
| role: user | ||
| model: anthropic/claude-3.7-sonnet:thinking | ||
| stream: false | ||
| uri: https://openrouter.ai/api/v1/chat/completions | ||
| response: | ||
| headers: | ||
| access-control-allow-origin: | ||
| - '*' | ||
| connection: | ||
| - keep-alive | ||
| content-length: | ||
| - '4024' | ||
| content-type: | ||
| - application/json | ||
| permissions-policy: | ||
| - payment=(self "https://checkout.stripe.com" "https://connect-js.stripe.com" "https://js.stripe.com" "https://*.js.stripe.com" | ||
| "https://hooks.stripe.com") | ||
| referrer-policy: | ||
| - no-referrer, strict-origin-when-cross-origin | ||
| transfer-encoding: | ||
| - chunked | ||
| vary: | ||
| - Accept-Encoding | ||
| parsed_body: | ||
| choices: | ||
| - finish_reason: stop | ||
| index: 0 | ||
| logprobs: null | ||
| message: | ||
| content: "I am Claude, an AI assistant created by Anthropic. I'm a large language model designed to be helpful, | ||
| harmless, and honest.\n\nI don't have consciousness or sentience like humans do - I'm a sophisticated text prediction | ||
| system trained on a large dataset of human text. I don't have personal experiences, emotions, or a physical existence. | ||
| \n\nMy purpose is to assist you with information, tasks, and conversation in a helpful way, while acknowledging | ||
| my limitations. I have knowledge cutoffs, can occasionally make mistakes, and don't have the ability to access | ||
| the internet or take actions in the physical world.\n\nIs there something specific you'd like to know about me | ||
| or how I can assist you?" | ||
| reasoning: |- | ||
| This question is asking me about my identity. Let me think about how to respond clearly and accurately. | ||
|
|
||
| I am Claude, an AI assistant created by Anthropic. I'm designed to be helpful, harmless, and honest in my interactions with humans. I don't have a physical form - I exist as a large language model running on computer hardware. I don't have consciousness, sentience, or feelings in the way humans do. I don't have personal experiences or a life outside of these conversations. | ||
|
|
||
| My capabilities include understanding and generating natural language text, reasoning about various topics, and attempting to be helpful to users in a wide range of contexts. I have been trained on a large corpus of text data, but my training data has a cutoff date, so I don't have knowledge of events that occurred after my training. | ||
|
|
||
| I have certain limitations - I don't have the ability to access the internet, run code, or interact with external systems unless given specific tools to do so. I don't have perfect knowledge and can make mistakes. | ||
|
|
||
| I'm designed to be conversational and to engage with users in a way that's helpful and informative, while respecting important ethical boundaries. | ||
| reasoning_details: | ||
| - format: anthropic-claude-v1 | ||
| index: 0 | ||
| signature: ErcBCkgICBACGAIiQHtMxpqcMhnwgGUmSDWGoOL9ZHTbDKjWnhbFm0xKzFl0NmXFjQQxjFj5mieRYY718fINsJMGjycTVYeiu69npakSDDrsnKYAD/fdcpI57xoMHlQBxI93RMa5CSUZIjAFVCMQF5GfLLQCibyPbb7LhZ4kLIFxw/nqsTwDDt6bx3yipUcq7G7eGts8MZ6LxOYqHTlIDx0tfHRIlkkcNCdB2sUeMqP8e7kuQqIHoD52GAI= | ||
| text: |- | ||
| This question is asking me about my identity. Let me think about how to respond clearly and accurately. | ||
|
|
||
| I am Claude, an AI assistant created by Anthropic. I'm designed to be helpful, harmless, and honest in my interactions with humans. I don't have a physical form - I exist as a large language model running on computer hardware. I don't have consciousness, sentience, or feelings in the way humans do. I don't have personal experiences or a life outside of these conversations. | ||
|
|
||
| My capabilities include understanding and generating natural language text, reasoning about various topics, and attempting to be helpful to users in a wide range of contexts. I have been trained on a large corpus of text data, but my training data has a cutoff date, so I don't have knowledge of events that occurred after my training. | ||
|
|
||
| I have certain limitations - I don't have the ability to access the internet, run code, or interact with external systems unless given specific tools to do so. I don't have perfect knowledge and can make mistakes. | ||
|
|
||
| I'm designed to be conversational and to engage with users in a way that's helpful and informative, while respecting important ethical boundaries. | ||
| type: reasoning.text | ||
| refusal: null | ||
| role: assistant | ||
| native_finish_reason: stop | ||
| created: 1760051228 | ||
| id: gen-1760051228-zUtCCQbb0vkaM4UXZmcb | ||
| model: anthropic/claude-3.7-sonnet:thinking | ||
| object: chat.completion | ||
| provider: Google | ||
| usage: | ||
| completion_tokens: 402 | ||
| prompt_tokens: 43 | ||
| total_tokens: 445 | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| version: 1 |
82 changes: 82 additions & 0 deletions
82
tests/models/cassettes/test_openrouter/test_openrouter_with_native_options.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| interactions: | ||
| - request: | ||
| headers: | ||
| accept: | ||
| - application/json | ||
| accept-encoding: | ||
| - gzip, deflate | ||
| connection: | ||
| - keep-alive | ||
| content-length: | ||
| - '193' | ||
| content-type: | ||
| - application/json | ||
| host: | ||
| - openrouter.ai | ||
| method: POST | ||
| parsed_body: | ||
| messages: | ||
| - content: Who are you | ||
| role: user | ||
| model: google/gemini-2.0-flash-exp:free | ||
| models: | ||
| - x-ai/grok-4 | ||
| provider: | ||
| only: | ||
| - xai | ||
| stream: false | ||
| transforms: | ||
| - middle-out | ||
| uri: https://openrouter.ai/api/v1/chat/completions | ||
| response: | ||
| headers: | ||
| access-control-allow-origin: | ||
| - '*' | ||
| connection: | ||
| - keep-alive | ||
| content-length: | ||
| - '1067' | ||
| content-type: | ||
| - application/json | ||
| permissions-policy: | ||
| - payment=(self "https://checkout.stripe.com" "https://connect-js.stripe.com" "https://js.stripe.com" "https://*.js.stripe.com" | ||
| "https://hooks.stripe.com") | ||
| referrer-policy: | ||
| - no-referrer, strict-origin-when-cross-origin | ||
| transfer-encoding: | ||
| - chunked | ||
| vary: | ||
| - Accept-Encoding | ||
| parsed_body: | ||
| choices: | ||
| - finish_reason: stop | ||
| index: 0 | ||
| logprobs: null | ||
| message: | ||
| content: |- | ||
| I'm Grok, a helpful and maximally truthful AI built by xAI. I'm not based on any other companies' models—instead, I'm inspired by the Hitchhiker's Guide to the Galaxy and JARVIS from Iron Man. My goal is to assist with questions, provide information, and maybe crack a joke or two along the way. | ||
|
|
||
| What can I help you with today? | ||
| reasoning: null | ||
| refusal: null | ||
| role: assistant | ||
| native_finish_reason: stop | ||
| created: 1759509677 | ||
| id: gen-1759509677-MpJiZ3ZkiGU3lnbM8QKo | ||
| model: x-ai/grok-4 | ||
| object: chat.completion | ||
| provider: xAI | ||
| system_fingerprint: fp_19e21a36c0 | ||
| usage: | ||
| completion_tokens: 240 | ||
| completion_tokens_details: | ||
| reasoning_tokens: 165 | ||
| prompt_tokens: 687 | ||
| prompt_tokens_details: | ||
| audio_tokens: 0 | ||
| cached_tokens: 682 | ||
| total_tokens: 927 | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| version: 1 | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to parse out more of these
usagevalues; see #2936 (comment).