openrouter 0.0.8__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- openrouter-0.0.8/PKG-INFO +262 -0
- openrouter-0.0.8/README.md +251 -0
- openrouter-0.0.8/pyproject.toml +52 -0
- openrouter-0.0.8/setup.cfg +4 -0
- openrouter-0.0.8/src/openrouter/__init__.py +17 -0
- openrouter-0.0.8/src/openrouter/_hooks/__init__.py +5 -0
- openrouter-0.0.8/src/openrouter/_hooks/registration.py +13 -0
- openrouter-0.0.8/src/openrouter/_hooks/sdkhooks.py +76 -0
- openrouter-0.0.8/src/openrouter/_hooks/types.py +112 -0
- openrouter-0.0.8/src/openrouter/_version.py +15 -0
- openrouter-0.0.8/src/openrouter/analytics.py +229 -0
- openrouter-0.0.8/src/openrouter/api_keys.py +1332 -0
- openrouter-0.0.8/src/openrouter/basesdk.py +368 -0
- openrouter-0.0.8/src/openrouter/beta.py +21 -0
- openrouter-0.0.8/src/openrouter/chat.py +751 -0
- openrouter-0.0.8/src/openrouter/completions.py +372 -0
- openrouter-0.0.8/src/openrouter/credits.py +453 -0
- openrouter-0.0.8/src/openrouter/embeddings.py +567 -0
- openrouter-0.0.8/src/openrouter/endpoints.py +383 -0
- openrouter-0.0.8/src/openrouter/errors/__init__.py +177 -0
- openrouter-0.0.8/src/openrouter/errors/badgatewayresponse_error.py +36 -0
- openrouter-0.0.8/src/openrouter/errors/badrequestresponse_error.py +36 -0
- openrouter-0.0.8/src/openrouter/errors/chaterror.py +29 -0
- openrouter-0.0.8/src/openrouter/errors/edgenetworktimeoutresponse_error.py +38 -0
- openrouter-0.0.8/src/openrouter/errors/forbiddenresponse_error.py +36 -0
- openrouter-0.0.8/src/openrouter/errors/internalserverresponse_error.py +36 -0
- openrouter-0.0.8/src/openrouter/errors/no_response_error.py +17 -0
- openrouter-0.0.8/src/openrouter/errors/notfoundresponse_error.py +36 -0
- openrouter-0.0.8/src/openrouter/errors/openrouterdefaulterror.py +40 -0
- openrouter-0.0.8/src/openrouter/errors/openroutererror.py +30 -0
- openrouter-0.0.8/src/openrouter/errors/payloadtoolargeresponse_error.py +36 -0
- openrouter-0.0.8/src/openrouter/errors/paymentrequiredresponse_error.py +36 -0
- openrouter-0.0.8/src/openrouter/errors/provideroverloadedresponse_error.py +38 -0
- openrouter-0.0.8/src/openrouter/errors/requesttimeoutresponse_error.py +36 -0
- openrouter-0.0.8/src/openrouter/errors/responsevalidationerror.py +27 -0
- openrouter-0.0.8/src/openrouter/errors/serviceunavailableresponse_error.py +38 -0
- openrouter-0.0.8/src/openrouter/errors/toomanyrequestsresponse_error.py +36 -0
- openrouter-0.0.8/src/openrouter/errors/unauthorizedresponse_error.py +36 -0
- openrouter-0.0.8/src/openrouter/errors/unprocessableentityresponse_error.py +38 -0
- openrouter-0.0.8/src/openrouter/generations.py +287 -0
- openrouter-0.0.8/src/openrouter/httpclient.py +125 -0
- openrouter-0.0.8/src/openrouter/models/__init__.py +2463 -0
- openrouter-0.0.8/src/openrouter/models/activityitem.py +66 -0
- openrouter-0.0.8/src/openrouter/models/assistantmessage.py +88 -0
- openrouter-0.0.8/src/openrouter/models/badgatewayresponseerrordata.py +61 -0
- openrouter-0.0.8/src/openrouter/models/badrequestresponseerrordata.py +61 -0
- openrouter-0.0.8/src/openrouter/models/chatcompletionfinishreason.py +17 -0
- openrouter-0.0.8/src/openrouter/models/chaterror.py +66 -0
- openrouter-0.0.8/src/openrouter/models/chatgenerationparams.py +291 -0
- openrouter-0.0.8/src/openrouter/models/chatgenerationtokenusage.py +134 -0
- openrouter-0.0.8/src/openrouter/models/chatmessagecontentitem.py +45 -0
- openrouter-0.0.8/src/openrouter/models/chatmessagecontentitemaudio.py +55 -0
- openrouter-0.0.8/src/openrouter/models/chatmessagecontentitemimage.py +47 -0
- openrouter-0.0.8/src/openrouter/models/chatmessagecontentitemtext.py +23 -0
- openrouter-0.0.8/src/openrouter/models/chatmessagecontentitemvideo.py +33 -0
- openrouter-0.0.8/src/openrouter/models/chatmessagetokenlogprob.py +99 -0
- openrouter-0.0.8/src/openrouter/models/chatmessagetokenlogprobs.py +52 -0
- openrouter-0.0.8/src/openrouter/models/chatmessagetoolcall.py +37 -0
- openrouter-0.0.8/src/openrouter/models/chatresponse.py +83 -0
- openrouter-0.0.8/src/openrouter/models/chatresponsechoice.py +69 -0
- openrouter-0.0.8/src/openrouter/models/chatstreamingchoice.py +72 -0
- openrouter-0.0.8/src/openrouter/models/chatstreamingmessagechunk.py +70 -0
- openrouter-0.0.8/src/openrouter/models/chatstreamingmessagetoolcall.py +42 -0
- openrouter-0.0.8/src/openrouter/models/chatstreamingresponsechunk.py +105 -0
- openrouter-0.0.8/src/openrouter/models/chatstreamoptions.py +14 -0
- openrouter-0.0.8/src/openrouter/models/completionchoice.py +69 -0
- openrouter-0.0.8/src/openrouter/models/completioncreateparams.py +277 -0
- openrouter-0.0.8/src/openrouter/models/completionlogprobs.py +54 -0
- openrouter-0.0.8/src/openrouter/models/completionresponse.py +43 -0
- openrouter-0.0.8/src/openrouter/models/completionusage.py +19 -0
- openrouter-0.0.8/src/openrouter/models/createauthkeyscodeop.py +133 -0
- openrouter-0.0.8/src/openrouter/models/createchargerequest.py +36 -0
- openrouter-0.0.8/src/openrouter/models/createcoinbasechargeop.py +121 -0
- openrouter-0.0.8/src/openrouter/models/createembeddingsop.py +366 -0
- openrouter-0.0.8/src/openrouter/models/createkeysop.py +255 -0
- openrouter-0.0.8/src/openrouter/models/createresponsesop.py +53 -0
- openrouter-0.0.8/src/openrouter/models/defaultparameters.py +60 -0
- openrouter-0.0.8/src/openrouter/models/deletekeysop.py +38 -0
- openrouter-0.0.8/src/openrouter/models/edgenetworktimeoutresponseerrordata.py +61 -0
- openrouter-0.0.8/src/openrouter/models/endpointstatus.py +18 -0
- openrouter-0.0.8/src/openrouter/models/exchangeauthcodeforapikeyop.py +130 -0
- openrouter-0.0.8/src/openrouter/models/filecitation.py +26 -0
- openrouter-0.0.8/src/openrouter/models/filepath.py +23 -0
- openrouter-0.0.8/src/openrouter/models/forbiddenresponseerrordata.py +61 -0
- openrouter-0.0.8/src/openrouter/models/getcreditsop.py +13 -0
- openrouter-0.0.8/src/openrouter/models/getcurrentkeyop.py +187 -0
- openrouter-0.0.8/src/openrouter/models/getgenerationop.py +268 -0
- openrouter-0.0.8/src/openrouter/models/getkeyop.py +180 -0
- openrouter-0.0.8/src/openrouter/models/getmodelsop.py +24 -0
- openrouter-0.0.8/src/openrouter/models/getparametersop.py +195 -0
- openrouter-0.0.8/src/openrouter/models/getuseractivityop.py +37 -0
- openrouter-0.0.8/src/openrouter/models/imagegenerationstatus.py +16 -0
- openrouter-0.0.8/src/openrouter/models/inputmodality.py +17 -0
- openrouter-0.0.8/src/openrouter/models/instructtype.py +35 -0
- openrouter-0.0.8/src/openrouter/models/internalserverresponseerrordata.py +61 -0
- openrouter-0.0.8/src/openrouter/models/jsonschemaconfig.py +61 -0
- openrouter-0.0.8/src/openrouter/models/listendpointsop.py +36 -0
- openrouter-0.0.8/src/openrouter/models/listendpointsresponse.py +145 -0
- openrouter-0.0.8/src/openrouter/models/listendpointszdrop.py +19 -0
- openrouter-0.0.8/src/openrouter/models/listmodelsuserop.py +24 -0
- openrouter-0.0.8/src/openrouter/models/listop.py +186 -0
- openrouter-0.0.8/src/openrouter/models/listprovidersop.py +89 -0
- openrouter-0.0.8/src/openrouter/models/message.py +68 -0
- openrouter-0.0.8/src/openrouter/models/model.py +132 -0
- openrouter-0.0.8/src/openrouter/models/modelarchitecture.py +122 -0
- openrouter-0.0.8/src/openrouter/models/modelgroup.py +32 -0
- openrouter-0.0.8/src/openrouter/models/modelscountresponse.py +33 -0
- openrouter-0.0.8/src/openrouter/models/modelslistresponse.py +21 -0
- openrouter-0.0.8/src/openrouter/models/namedtoolchoice.py +31 -0
- openrouter-0.0.8/src/openrouter/models/notfoundresponseerrordata.py +61 -0
- openrouter-0.0.8/src/openrouter/models/openairesponsesannotation.py +19 -0
- openrouter-0.0.8/src/openrouter/models/openairesponsesincludable.py +17 -0
- openrouter-0.0.8/src/openrouter/models/openairesponsesincompletedetails.py +27 -0
- openrouter-0.0.8/src/openrouter/models/openairesponsesinput_union.py +330 -0
- openrouter-0.0.8/src/openrouter/models/openairesponsesprompt.py +73 -0
- openrouter-0.0.8/src/openrouter/models/openairesponsesreasoningconfig.py +63 -0
- openrouter-0.0.8/src/openrouter/models/openairesponsesreasoningeffort.py +16 -0
- openrouter-0.0.8/src/openrouter/models/openairesponsesrefusalcontent.py +20 -0
- openrouter-0.0.8/src/openrouter/models/openairesponsesresponsestatus.py +18 -0
- openrouter-0.0.8/src/openrouter/models/openairesponsesservicetier.py +17 -0
- openrouter-0.0.8/src/openrouter/models/openairesponsestoolchoice_union.py +87 -0
- openrouter-0.0.8/src/openrouter/models/openairesponsestruncation.py +14 -0
- openrouter-0.0.8/src/openrouter/models/openresponseseasyinputmessage.py +97 -0
- openrouter-0.0.8/src/openrouter/models/openresponseserrorevent.py +64 -0
- openrouter-0.0.8/src/openrouter/models/openresponsesfunctioncalloutput.py +75 -0
- openrouter-0.0.8/src/openrouter/models/openresponsesfunctiontoolcall.py +78 -0
- openrouter-0.0.8/src/openrouter/models/openresponsesimagegencallcompleted.py +32 -0
- openrouter-0.0.8/src/openrouter/models/openresponsesimagegencallgenerating.py +32 -0
- openrouter-0.0.8/src/openrouter/models/openresponsesimagegencallinprogress.py +32 -0
- openrouter-0.0.8/src/openrouter/models/openresponsesimagegencallpartialimage.py +38 -0
- openrouter-0.0.8/src/openrouter/models/openresponsesinput.py +97 -0
- openrouter-0.0.8/src/openrouter/models/openresponsesinputmessageitem.py +83 -0
- openrouter-0.0.8/src/openrouter/models/openresponseslogprobs.py +28 -0
- openrouter-0.0.8/src/openrouter/models/openresponsesnonstreamingresponse.py +324 -0
- openrouter-0.0.8/src/openrouter/models/openresponsesreasoning.py +137 -0
- openrouter-0.0.8/src/openrouter/models/openresponsesreasoningconfig.py +73 -0
- openrouter-0.0.8/src/openrouter/models/openresponsesreasoningdeltaevent.py +36 -0
- openrouter-0.0.8/src/openrouter/models/openresponsesreasoningdoneevent.py +36 -0
- openrouter-0.0.8/src/openrouter/models/openresponsesreasoningsummarypartaddedevent.py +39 -0
- openrouter-0.0.8/src/openrouter/models/openresponsesreasoningsummarytextdeltaevent.py +38 -0
- openrouter-0.0.8/src/openrouter/models/openresponsesreasoningsummarytextdoneevent.py +38 -0
- openrouter-0.0.8/src/openrouter/models/openresponsesrequest.py +654 -0
- openrouter-0.0.8/src/openrouter/models/openresponsesresponsetext.py +83 -0
- openrouter-0.0.8/src/openrouter/models/openresponsesstreamevent.py +644 -0
- openrouter-0.0.8/src/openrouter/models/openresponsestoplogprobs.py +21 -0
- openrouter-0.0.8/src/openrouter/models/openresponsesusage.py +140 -0
- openrouter-0.0.8/src/openrouter/models/openresponseswebsearch20250826tool.py +118 -0
- openrouter-0.0.8/src/openrouter/models/openresponseswebsearchpreview20250311tool.py +77 -0
- openrouter-0.0.8/src/openrouter/models/openresponseswebsearchpreviewtool.py +75 -0
- openrouter-0.0.8/src/openrouter/models/openresponseswebsearchtool.py +118 -0
- openrouter-0.0.8/src/openrouter/models/outputitemimagegenerationcall.py +60 -0
- openrouter-0.0.8/src/openrouter/models/outputmessage.py +77 -0
- openrouter-0.0.8/src/openrouter/models/outputmodality.py +15 -0
- openrouter-0.0.8/src/openrouter/models/parameter.py +35 -0
- openrouter-0.0.8/src/openrouter/models/payloadtoolargeresponseerrordata.py +61 -0
- openrouter-0.0.8/src/openrouter/models/paymentrequiredresponseerrordata.py +61 -0
- openrouter-0.0.8/src/openrouter/models/perrequestlimits.py +24 -0
- openrouter-0.0.8/src/openrouter/models/providername.py +98 -0
- openrouter-0.0.8/src/openrouter/models/provideroverloadedresponseerrordata.py +61 -0
- openrouter-0.0.8/src/openrouter/models/publicendpoint.py +180 -0
- openrouter-0.0.8/src/openrouter/models/publicpricing.py +73 -0
- openrouter-0.0.8/src/openrouter/models/quantization.py +21 -0
- openrouter-0.0.8/src/openrouter/models/reasoningsummarytext.py +20 -0
- openrouter-0.0.8/src/openrouter/models/reasoningsummaryverbosity.py +15 -0
- openrouter-0.0.8/src/openrouter/models/reasoningtextcontent.py +20 -0
- openrouter-0.0.8/src/openrouter/models/requesttimeoutresponseerrordata.py +61 -0
- openrouter-0.0.8/src/openrouter/models/responseformatjsonschema.py +26 -0
- openrouter-0.0.8/src/openrouter/models/responseformattextconfig.py +36 -0
- openrouter-0.0.8/src/openrouter/models/responseformattextgrammar.py +23 -0
- openrouter-0.0.8/src/openrouter/models/responseinputaudio.py +50 -0
- openrouter-0.0.8/src/openrouter/models/responseinputfile.py +70 -0
- openrouter-0.0.8/src/openrouter/models/responseinputimage.py +79 -0
- openrouter-0.0.8/src/openrouter/models/responseinputtext.py +24 -0
- openrouter-0.0.8/src/openrouter/models/responseoutputtext.py +27 -0
- openrouter-0.0.8/src/openrouter/models/responseserrorfield.py +48 -0
- openrouter-0.0.8/src/openrouter/models/responsesformatjsonobject.py +21 -0
- openrouter-0.0.8/src/openrouter/models/responsesformattext.py +21 -0
- openrouter-0.0.8/src/openrouter/models/responsesformattextjsonschemaconfig.py +71 -0
- openrouter-0.0.8/src/openrouter/models/responsesimagegenerationcall.py +60 -0
- openrouter-0.0.8/src/openrouter/models/responsesoutputitem.py +57 -0
- openrouter-0.0.8/src/openrouter/models/responsesoutputitemfilesearchcall.py +29 -0
- openrouter-0.0.8/src/openrouter/models/responsesoutputitemfunctioncall.py +61 -0
- openrouter-0.0.8/src/openrouter/models/responsesoutputitemreasoning.py +104 -0
- openrouter-0.0.8/src/openrouter/models/responsesoutputmessage.py +82 -0
- openrouter-0.0.8/src/openrouter/models/responsessearchcontextsize.py +16 -0
- openrouter-0.0.8/src/openrouter/models/responseswebsearchcalloutput.py +26 -0
- openrouter-0.0.8/src/openrouter/models/responseswebsearchuserlocation.py +70 -0
- openrouter-0.0.8/src/openrouter/models/responsetextconfig.py +83 -0
- openrouter-0.0.8/src/openrouter/models/security.py +25 -0
- openrouter-0.0.8/src/openrouter/models/sendchatcompletionrequestop.py +35 -0
- openrouter-0.0.8/src/openrouter/models/serviceunavailableresponseerrordata.py +61 -0
- openrouter-0.0.8/src/openrouter/models/systemmessage.py +41 -0
- openrouter-0.0.8/src/openrouter/models/toolcallstatus.py +15 -0
- openrouter-0.0.8/src/openrouter/models/tooldefinitionjson.py +77 -0
- openrouter-0.0.8/src/openrouter/models/toolresponsemessage.py +41 -0
- openrouter-0.0.8/src/openrouter/models/toomanyrequestsresponseerrordata.py +61 -0
- openrouter-0.0.8/src/openrouter/models/topproviderinfo.py +66 -0
- openrouter-0.0.8/src/openrouter/models/unauthorizedresponseerrordata.py +61 -0
- openrouter-0.0.8/src/openrouter/models/unprocessableentityresponseerrordata.py +61 -0
- openrouter-0.0.8/src/openrouter/models/updatekeysop.py +274 -0
- openrouter-0.0.8/src/openrouter/models/urlcitation.py +29 -0
- openrouter-0.0.8/src/openrouter/models/usermessage.py +40 -0
- openrouter-0.0.8/src/openrouter/models/websearchpreviewtooluserlocation.py +66 -0
- openrouter-0.0.8/src/openrouter/models/websearchstatus.py +16 -0
- openrouter-0.0.8/src/openrouter/models_.py +564 -0
- openrouter-0.0.8/src/openrouter/oauth.py +486 -0
- openrouter-0.0.8/src/openrouter/parameters.py +234 -0
- openrouter-0.0.8/src/openrouter/providers.py +181 -0
- openrouter-0.0.8/src/openrouter/py.typed +1 -0
- openrouter-0.0.8/src/openrouter/responses.py +1107 -0
- openrouter-0.0.8/src/openrouter/sdk.py +224 -0
- openrouter-0.0.8/src/openrouter/sdkconfiguration.py +53 -0
- openrouter-0.0.8/src/openrouter/types/__init__.py +21 -0
- openrouter-0.0.8/src/openrouter/types/basemodel.py +39 -0
- openrouter-0.0.8/src/openrouter/utils/__init__.py +200 -0
- openrouter-0.0.8/src/openrouter/utils/annotations.py +79 -0
- openrouter-0.0.8/src/openrouter/utils/datetimes.py +23 -0
- openrouter-0.0.8/src/openrouter/utils/enums.py +74 -0
- openrouter-0.0.8/src/openrouter/utils/eventstreaming.py +248 -0
- openrouter-0.0.8/src/openrouter/utils/forms.py +223 -0
- openrouter-0.0.8/src/openrouter/utils/headers.py +136 -0
- openrouter-0.0.8/src/openrouter/utils/logger.py +27 -0
- openrouter-0.0.8/src/openrouter/utils/metadata.py +118 -0
- openrouter-0.0.8/src/openrouter/utils/oauth_create_authorization_url.py +95 -0
- openrouter-0.0.8/src/openrouter/utils/oauth_create_sha256_code_challenge.py +120 -0
- openrouter-0.0.8/src/openrouter/utils/queryparams.py +205 -0
- openrouter-0.0.8/src/openrouter/utils/requestbodies.py +66 -0
- openrouter-0.0.8/src/openrouter/utils/retries.py +217 -0
- openrouter-0.0.8/src/openrouter/utils/security.py +192 -0
- openrouter-0.0.8/src/openrouter/utils/serializers.py +249 -0
- openrouter-0.0.8/src/openrouter/utils/unmarshal_json_response.py +24 -0
- openrouter-0.0.8/src/openrouter/utils/url.py +155 -0
- openrouter-0.0.8/src/openrouter/utils/values.py +137 -0
- openrouter-0.0.8/src/openrouter.egg-info/PKG-INFO +262 -0
- openrouter-0.0.8/src/openrouter.egg-info/SOURCES.txt +236 -0
- openrouter-0.0.8/src/openrouter.egg-info/dependency_links.txt +1 -0
- openrouter-0.0.8/src/openrouter.egg-info/requires.txt +3 -0
- openrouter-0.0.8/src/openrouter.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: openrouter
|
|
3
|
+
Version: 0.0.8
|
|
4
|
+
Summary: Official Python Client SDK for OpenRouter.
|
|
5
|
+
Author: OpenRouter
|
|
6
|
+
Requires-Python: >=3.9.2
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
Requires-Dist: httpcore>=1.0.9
|
|
9
|
+
Requires-Dist: httpx>=0.28.1
|
|
10
|
+
Requires-Dist: pydantic>=2.11.2
|
|
11
|
+
|
|
12
|
+

|
|
13
|
+
|
|
14
|
+
# OpenRouter SDK (Beta)
|
|
15
|
+
|
|
16
|
+
The [OpenRouter SDK](https://openrouter.ai/docs/sdks/python) is a Python toolkit designed to help you build AI-powered features and solutions. Giving you easy access to over 300 models across providers in an easy and type-safe way.
|
|
17
|
+
|
|
18
|
+
To learn more about how to use the OpenRouter SDK, check out our [API Reference](https://openrouter.ai/docs/sdks/python/reference) and [Documentation](https://openrouter.ai/docs/sdks/python).
|
|
19
|
+
|
|
20
|
+
<!-- No Summary [summary] -->
|
|
21
|
+
|
|
22
|
+
<!-- No Table of Contents [toc] -->
|
|
23
|
+
|
|
24
|
+
<!-- Start SDK Installation [installation] -->
|
|
25
|
+
## SDK Installation
|
|
26
|
+
|
|
27
|
+
> [!NOTE]
|
|
28
|
+
> **Python version upgrade policy**
|
|
29
|
+
>
|
|
30
|
+
> Once a Python version reaches its [official end of life date](https://devguide.python.org/versions/), a 3-month grace period is provided for users to upgrade. Following this grace period, the minimum python version supported in the SDK will be updated.
|
|
31
|
+
|
|
32
|
+
The SDK can be installed with *uv*, *pip*, or *poetry* package managers.
|
|
33
|
+
|
|
34
|
+
### uv
|
|
35
|
+
|
|
36
|
+
*uv* is a fast Python package installer and resolver, designed as a drop-in replacement for pip and pip-tools. It's recommended for its speed and modern Python tooling capabilities.
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
uv add openrouter
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### PIP
|
|
43
|
+
|
|
44
|
+
*PIP* is the default package installer for Python, enabling easy installation and management of packages from PyPI via the command line.
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pip install openrouter
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Poetry
|
|
51
|
+
|
|
52
|
+
*Poetry* is a modern tool that simplifies dependency management and package publishing by using a single `pyproject.toml` file to handle project metadata and dependencies.
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
poetry add openrouter
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Shell and script usage with `uv`
|
|
59
|
+
|
|
60
|
+
You can use this SDK in a Python shell with [uv](https://docs.astral.sh/uv/) and the `uvx` command that comes with it like so:
|
|
61
|
+
|
|
62
|
+
```shell
|
|
63
|
+
uvx --from openrouter python
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
It's also possible to write a standalone Python script without needing to set up a whole project like so:
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
#!/usr/bin/env -S uv run --script
|
|
70
|
+
# /// script
|
|
71
|
+
# requires-python = ">=3.9"
|
|
72
|
+
# dependencies = [
|
|
73
|
+
# "openrouter",
|
|
74
|
+
# ]
|
|
75
|
+
# ///
|
|
76
|
+
|
|
77
|
+
from openrouter import OpenRouter
|
|
78
|
+
|
|
79
|
+
sdk = OpenRouter(
|
|
80
|
+
# SDK arguments
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
# Rest of script here...
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Once that is saved to a file, you can run it with `uv run script.py` where
|
|
87
|
+
`script.py` can be replaced with the actual file name.
|
|
88
|
+
<!-- End SDK Installation [installation] -->
|
|
89
|
+
|
|
90
|
+
<!-- Start Requirements [requirements] -->
|
|
91
|
+
## Requirements
|
|
92
|
+
|
|
93
|
+
This SDK requires Python 3.9 or higher. For Python version support policy, see the SDK Installation section above.
|
|
94
|
+
<!-- End Requirements [requirements] -->
|
|
95
|
+
|
|
96
|
+
<!-- Start IDE Support [idesupport] -->
|
|
97
|
+
## IDE Support
|
|
98
|
+
|
|
99
|
+
### PyCharm
|
|
100
|
+
|
|
101
|
+
Generally, the SDK will work well with most IDEs out of the box. However, when using PyCharm, you can enjoy much better integration with Pydantic by installing an additional plugin.
|
|
102
|
+
|
|
103
|
+
- [PyCharm Pydantic Plugin](https://docs.pydantic.dev/latest/integrations/pycharm/)
|
|
104
|
+
<!-- End IDE Support [idesupport] -->
|
|
105
|
+
|
|
106
|
+
<!-- No SDK Example Usage [usage] -->
|
|
107
|
+
## SDK Usage
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
# Synchronous Example
|
|
111
|
+
from openrouter import OpenRouter
|
|
112
|
+
import os
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
with OpenRouter(
|
|
116
|
+
api_key=os.getenv("OPENROUTER_API_KEY", ""),
|
|
117
|
+
) as open_router:
|
|
118
|
+
|
|
119
|
+
res = open_router.chat.send(messages=[
|
|
120
|
+
{
|
|
121
|
+
"role": "user",
|
|
122
|
+
"content": "Hello, how are you?",
|
|
123
|
+
},
|
|
124
|
+
], model="anthropic/claude-4.5-sonnet", provider={
|
|
125
|
+
"zdr": True,
|
|
126
|
+
"sort": "price",
|
|
127
|
+
}, stream=True)
|
|
128
|
+
|
|
129
|
+
for event in event_stream:
|
|
130
|
+
# handle event
|
|
131
|
+
print(event, flush=True)
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
</br>
|
|
135
|
+
|
|
136
|
+
The same SDK client can also be used to make asynchronous requests by importing asyncio.
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
# Asynchronous Example
|
|
140
|
+
import asyncio
|
|
141
|
+
from openrouter import OpenRouter
|
|
142
|
+
import os
|
|
143
|
+
|
|
144
|
+
async def main():
|
|
145
|
+
|
|
146
|
+
async with OpenRouter(
|
|
147
|
+
api_key=os.getenv("OPENROUTER_API_KEY", ""),
|
|
148
|
+
) as open_router:
|
|
149
|
+
|
|
150
|
+
res = await open_router.chat.send_async(messages=[
|
|
151
|
+
{
|
|
152
|
+
"role": "user",
|
|
153
|
+
"content": "Hello, how are you?",
|
|
154
|
+
},
|
|
155
|
+
], model="anthropic/claude-4.5-sonnet", provider={
|
|
156
|
+
"zdr": True,
|
|
157
|
+
"sort": "price",
|
|
158
|
+
}, stream=True)
|
|
159
|
+
|
|
160
|
+
async for event in event_stream:
|
|
161
|
+
# handle event
|
|
162
|
+
print(event, flush=True)
|
|
163
|
+
|
|
164
|
+
asyncio.run(main())
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
<!-- No Authentication [security] -->
|
|
168
|
+
|
|
169
|
+
<!-- No Available Resources and Operations [operations] -->
|
|
170
|
+
|
|
171
|
+
<!-- No Standalone functions [standalone-funcs] -->
|
|
172
|
+
|
|
173
|
+
<!-- No React hooks with TanStack Query [react-query] -->
|
|
174
|
+
|
|
175
|
+
<!-- No Server-sent event streaming [eventstream] -->
|
|
176
|
+
|
|
177
|
+
<!-- No Retries [retries] -->
|
|
178
|
+
|
|
179
|
+
<!-- No Error Handling [errors] -->
|
|
180
|
+
|
|
181
|
+
<!-- No Server Selection [server] -->
|
|
182
|
+
|
|
183
|
+
<!-- No Custom HTTP Client [http-client] -->
|
|
184
|
+
|
|
185
|
+
<!-- Start Resource Management [resource-management] -->
|
|
186
|
+
## Resource Management
|
|
187
|
+
|
|
188
|
+
The `OpenRouter` class implements the context manager protocol and registers a finalizer function to close the underlying sync and async HTTPX clients it uses under the hood. This will close HTTP connections, release memory and free up other resources held by the SDK. In short-lived Python programs and notebooks that make a few SDK method calls, resource management may not be a concern. However, in longer-lived programs, it is beneficial to create a single SDK instance via a [context manager][context-manager] and reuse it across the application.
|
|
189
|
+
|
|
190
|
+
[context-manager]: https://docs.python.org/3/reference/datamodel.html#context-managers
|
|
191
|
+
|
|
192
|
+
```python
|
|
193
|
+
from openrouter import OpenRouter
|
|
194
|
+
import os
|
|
195
|
+
def main():
|
|
196
|
+
|
|
197
|
+
with OpenRouter(
|
|
198
|
+
api_key=os.getenv("OPENROUTER_API_KEY", ""),
|
|
199
|
+
) as open_router:
|
|
200
|
+
# Rest of application here...
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
# Or when using async:
|
|
204
|
+
async def amain():
|
|
205
|
+
|
|
206
|
+
async with OpenRouter(
|
|
207
|
+
api_key=os.getenv("OPENROUTER_API_KEY", ""),
|
|
208
|
+
) as open_router:
|
|
209
|
+
# Rest of application here...
|
|
210
|
+
```
|
|
211
|
+
<!-- End Resource Management [resource-management] -->
|
|
212
|
+
|
|
213
|
+
<!-- Start Debugging [debug] -->
|
|
214
|
+
## Debugging
|
|
215
|
+
|
|
216
|
+
You can setup your SDK to emit debug logs for SDK requests and responses.
|
|
217
|
+
|
|
218
|
+
You can pass your own logger class directly into your SDK.
|
|
219
|
+
```python
|
|
220
|
+
from openrouter import OpenRouter
|
|
221
|
+
import logging
|
|
222
|
+
|
|
223
|
+
logging.basicConfig(level=logging.DEBUG)
|
|
224
|
+
s = OpenRouter(debug_logger=logging.getLogger("openrouter"))
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
You can also enable a default debug logger by setting an environment variable `OPENROUTER_DEBUG` to true.
|
|
228
|
+
<!-- End Debugging [debug] -->
|
|
229
|
+
|
|
230
|
+
<!-- Placeholder for Future Speakeasy SDK Sections -->
|
|
231
|
+
|
|
232
|
+
# Development
|
|
233
|
+
|
|
234
|
+
## Running Tests
|
|
235
|
+
|
|
236
|
+
To run the test suite, you'll need to set up your environment with an OpenRouter API key.
|
|
237
|
+
|
|
238
|
+
### Local Development
|
|
239
|
+
|
|
240
|
+
1. Copy the example environment file:
|
|
241
|
+
|
|
242
|
+
```bash
|
|
243
|
+
cp .env.example .env
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
2. Edit `.env` and add your OpenRouter API key:
|
|
247
|
+
|
|
248
|
+
```bash
|
|
249
|
+
OPENROUTER_API_KEY=your_api_key_here
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
3. Run the tests:
|
|
253
|
+
|
|
254
|
+
```bash
|
|
255
|
+
pytest
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
## Maturity
|
|
259
|
+
|
|
260
|
+
This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage
|
|
261
|
+
to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally
|
|
262
|
+
looking for the latest version.
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+

|
|
2
|
+
|
|
3
|
+
# OpenRouter SDK (Beta)
|
|
4
|
+
|
|
5
|
+
The [OpenRouter SDK](https://openrouter.ai/docs/sdks/python) is a Python toolkit designed to help you build AI-powered features and solutions. Giving you easy access to over 300 models across providers in an easy and type-safe way.
|
|
6
|
+
|
|
7
|
+
To learn more about how to use the OpenRouter SDK, check out our [API Reference](https://openrouter.ai/docs/sdks/python/reference) and [Documentation](https://openrouter.ai/docs/sdks/python).
|
|
8
|
+
|
|
9
|
+
<!-- No Summary [summary] -->
|
|
10
|
+
|
|
11
|
+
<!-- No Table of Contents [toc] -->
|
|
12
|
+
|
|
13
|
+
<!-- Start SDK Installation [installation] -->
|
|
14
|
+
## SDK Installation
|
|
15
|
+
|
|
16
|
+
> [!NOTE]
|
|
17
|
+
> **Python version upgrade policy**
|
|
18
|
+
>
|
|
19
|
+
> Once a Python version reaches its [official end of life date](https://devguide.python.org/versions/), a 3-month grace period is provided for users to upgrade. Following this grace period, the minimum python version supported in the SDK will be updated.
|
|
20
|
+
|
|
21
|
+
The SDK can be installed with *uv*, *pip*, or *poetry* package managers.
|
|
22
|
+
|
|
23
|
+
### uv
|
|
24
|
+
|
|
25
|
+
*uv* is a fast Python package installer and resolver, designed as a drop-in replacement for pip and pip-tools. It's recommended for its speed and modern Python tooling capabilities.
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
uv add openrouter
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### PIP
|
|
32
|
+
|
|
33
|
+
*PIP* is the default package installer for Python, enabling easy installation and management of packages from PyPI via the command line.
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install openrouter
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Poetry
|
|
40
|
+
|
|
41
|
+
*Poetry* is a modern tool that simplifies dependency management and package publishing by using a single `pyproject.toml` file to handle project metadata and dependencies.
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
poetry add openrouter
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Shell and script usage with `uv`
|
|
48
|
+
|
|
49
|
+
You can use this SDK in a Python shell with [uv](https://docs.astral.sh/uv/) and the `uvx` command that comes with it like so:
|
|
50
|
+
|
|
51
|
+
```shell
|
|
52
|
+
uvx --from openrouter python
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
It's also possible to write a standalone Python script without needing to set up a whole project like so:
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
#!/usr/bin/env -S uv run --script
|
|
59
|
+
# /// script
|
|
60
|
+
# requires-python = ">=3.9"
|
|
61
|
+
# dependencies = [
|
|
62
|
+
# "openrouter",
|
|
63
|
+
# ]
|
|
64
|
+
# ///
|
|
65
|
+
|
|
66
|
+
from openrouter import OpenRouter
|
|
67
|
+
|
|
68
|
+
sdk = OpenRouter(
|
|
69
|
+
# SDK arguments
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
# Rest of script here...
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Once that is saved to a file, you can run it with `uv run script.py` where
|
|
76
|
+
`script.py` can be replaced with the actual file name.
|
|
77
|
+
<!-- End SDK Installation [installation] -->
|
|
78
|
+
|
|
79
|
+
<!-- Start Requirements [requirements] -->
|
|
80
|
+
## Requirements
|
|
81
|
+
|
|
82
|
+
This SDK requires Python 3.9 or higher. For Python version support policy, see the SDK Installation section above.
|
|
83
|
+
<!-- End Requirements [requirements] -->
|
|
84
|
+
|
|
85
|
+
<!-- Start IDE Support [idesupport] -->
|
|
86
|
+
## IDE Support
|
|
87
|
+
|
|
88
|
+
### PyCharm
|
|
89
|
+
|
|
90
|
+
Generally, the SDK will work well with most IDEs out of the box. However, when using PyCharm, you can enjoy much better integration with Pydantic by installing an additional plugin.
|
|
91
|
+
|
|
92
|
+
- [PyCharm Pydantic Plugin](https://docs.pydantic.dev/latest/integrations/pycharm/)
|
|
93
|
+
<!-- End IDE Support [idesupport] -->
|
|
94
|
+
|
|
95
|
+
<!-- No SDK Example Usage [usage] -->
|
|
96
|
+
## SDK Usage
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
# Synchronous Example
|
|
100
|
+
from openrouter import OpenRouter
|
|
101
|
+
import os
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
with OpenRouter(
|
|
105
|
+
api_key=os.getenv("OPENROUTER_API_KEY", ""),
|
|
106
|
+
) as open_router:
|
|
107
|
+
|
|
108
|
+
res = open_router.chat.send(messages=[
|
|
109
|
+
{
|
|
110
|
+
"role": "user",
|
|
111
|
+
"content": "Hello, how are you?",
|
|
112
|
+
},
|
|
113
|
+
], model="anthropic/claude-4.5-sonnet", provider={
|
|
114
|
+
"zdr": True,
|
|
115
|
+
"sort": "price",
|
|
116
|
+
}, stream=True)
|
|
117
|
+
|
|
118
|
+
for event in event_stream:
|
|
119
|
+
# handle event
|
|
120
|
+
print(event, flush=True)
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
</br>
|
|
124
|
+
|
|
125
|
+
The same SDK client can also be used to make asynchronous requests by importing asyncio.
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
# Asynchronous Example
|
|
129
|
+
import asyncio
|
|
130
|
+
from openrouter import OpenRouter
|
|
131
|
+
import os
|
|
132
|
+
|
|
133
|
+
async def main():
|
|
134
|
+
|
|
135
|
+
async with OpenRouter(
|
|
136
|
+
api_key=os.getenv("OPENROUTER_API_KEY", ""),
|
|
137
|
+
) as open_router:
|
|
138
|
+
|
|
139
|
+
res = await open_router.chat.send_async(messages=[
|
|
140
|
+
{
|
|
141
|
+
"role": "user",
|
|
142
|
+
"content": "Hello, how are you?",
|
|
143
|
+
},
|
|
144
|
+
], model="anthropic/claude-4.5-sonnet", provider={
|
|
145
|
+
"zdr": True,
|
|
146
|
+
"sort": "price",
|
|
147
|
+
}, stream=True)
|
|
148
|
+
|
|
149
|
+
async for event in event_stream:
|
|
150
|
+
# handle event
|
|
151
|
+
print(event, flush=True)
|
|
152
|
+
|
|
153
|
+
asyncio.run(main())
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
<!-- No Authentication [security] -->
|
|
157
|
+
|
|
158
|
+
<!-- No Available Resources and Operations [operations] -->
|
|
159
|
+
|
|
160
|
+
<!-- No Standalone functions [standalone-funcs] -->
|
|
161
|
+
|
|
162
|
+
<!-- No React hooks with TanStack Query [react-query] -->
|
|
163
|
+
|
|
164
|
+
<!-- No Server-sent event streaming [eventstream] -->
|
|
165
|
+
|
|
166
|
+
<!-- No Retries [retries] -->
|
|
167
|
+
|
|
168
|
+
<!-- No Error Handling [errors] -->
|
|
169
|
+
|
|
170
|
+
<!-- No Server Selection [server] -->
|
|
171
|
+
|
|
172
|
+
<!-- No Custom HTTP Client [http-client] -->
|
|
173
|
+
|
|
174
|
+
<!-- Start Resource Management [resource-management] -->
|
|
175
|
+
## Resource Management
|
|
176
|
+
|
|
177
|
+
The `OpenRouter` class implements the context manager protocol and registers a finalizer function to close the underlying sync and async HTTPX clients it uses under the hood. This will close HTTP connections, release memory and free up other resources held by the SDK. In short-lived Python programs and notebooks that make a few SDK method calls, resource management may not be a concern. However, in longer-lived programs, it is beneficial to create a single SDK instance via a [context manager][context-manager] and reuse it across the application.
|
|
178
|
+
|
|
179
|
+
[context-manager]: https://docs.python.org/3/reference/datamodel.html#context-managers
|
|
180
|
+
|
|
181
|
+
```python
|
|
182
|
+
from openrouter import OpenRouter
|
|
183
|
+
import os
|
|
184
|
+
def main():
|
|
185
|
+
|
|
186
|
+
with OpenRouter(
|
|
187
|
+
api_key=os.getenv("OPENROUTER_API_KEY", ""),
|
|
188
|
+
) as open_router:
|
|
189
|
+
# Rest of application here...
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
# Or when using async:
|
|
193
|
+
async def amain():
|
|
194
|
+
|
|
195
|
+
async with OpenRouter(
|
|
196
|
+
api_key=os.getenv("OPENROUTER_API_KEY", ""),
|
|
197
|
+
) as open_router:
|
|
198
|
+
# Rest of application here...
|
|
199
|
+
```
|
|
200
|
+
<!-- End Resource Management [resource-management] -->
|
|
201
|
+
|
|
202
|
+
<!-- Start Debugging [debug] -->
|
|
203
|
+
## Debugging
|
|
204
|
+
|
|
205
|
+
You can setup your SDK to emit debug logs for SDK requests and responses.
|
|
206
|
+
|
|
207
|
+
You can pass your own logger class directly into your SDK.
|
|
208
|
+
```python
|
|
209
|
+
from openrouter import OpenRouter
|
|
210
|
+
import logging
|
|
211
|
+
|
|
212
|
+
logging.basicConfig(level=logging.DEBUG)
|
|
213
|
+
s = OpenRouter(debug_logger=logging.getLogger("openrouter"))
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
You can also enable a default debug logger by setting an environment variable `OPENROUTER_DEBUG` to true.
|
|
217
|
+
<!-- End Debugging [debug] -->
|
|
218
|
+
|
|
219
|
+
<!-- Placeholder for Future Speakeasy SDK Sections -->
|
|
220
|
+
|
|
221
|
+
# Development
|
|
222
|
+
|
|
223
|
+
## Running Tests
|
|
224
|
+
|
|
225
|
+
To run the test suite, you'll need to set up your environment with an OpenRouter API key.
|
|
226
|
+
|
|
227
|
+
### Local Development
|
|
228
|
+
|
|
229
|
+
1. Copy the example environment file:
|
|
230
|
+
|
|
231
|
+
```bash
|
|
232
|
+
cp .env.example .env
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
2. Edit `.env` and add your OpenRouter API key:
|
|
236
|
+
|
|
237
|
+
```bash
|
|
238
|
+
OPENROUTER_API_KEY=your_api_key_here
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
3. Run the tests:
|
|
242
|
+
|
|
243
|
+
```bash
|
|
244
|
+
pytest
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
## Maturity
|
|
248
|
+
|
|
249
|
+
This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage
|
|
250
|
+
to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally
|
|
251
|
+
looking for the latest version.
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "openrouter"
|
|
3
|
+
version = "0.0.8"
|
|
4
|
+
description = "Official Python Client SDK for OpenRouter."
|
|
5
|
+
authors = [{ name = "OpenRouter" },]
|
|
6
|
+
readme = "README.md"
|
|
7
|
+
requires-python = ">=3.9.2"
|
|
8
|
+
dependencies = [
|
|
9
|
+
"httpcore >=1.0.9",
|
|
10
|
+
"httpx >=0.28.1",
|
|
11
|
+
"pydantic >=2.11.2",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
[dependency-groups]
|
|
15
|
+
dev = [
|
|
16
|
+
"mypy ==1.15.0",
|
|
17
|
+
"pylint ==3.2.3",
|
|
18
|
+
"pyright ==1.1.398",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[tool.setuptools.packages.find]
|
|
22
|
+
where = ["src"]
|
|
23
|
+
|
|
24
|
+
[tool.setuptools.package-data]
|
|
25
|
+
"*" = ["py.typed"]
|
|
26
|
+
|
|
27
|
+
[build-system]
|
|
28
|
+
requires = ["setuptools>=80", "wheel"]
|
|
29
|
+
build-backend = "setuptools.build_meta"
|
|
30
|
+
|
|
31
|
+
[tool.pytest.ini_options]
|
|
32
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
33
|
+
pythonpath = ["src"]
|
|
34
|
+
|
|
35
|
+
[tool.mypy]
|
|
36
|
+
disable_error_code = "misc"
|
|
37
|
+
explicit_package_bases = true
|
|
38
|
+
mypy_path = "src"
|
|
39
|
+
|
|
40
|
+
[[tool.mypy.overrides]]
|
|
41
|
+
module = "typing_inspect"
|
|
42
|
+
ignore_missing_imports = true
|
|
43
|
+
|
|
44
|
+
[[tool.mypy.overrides]]
|
|
45
|
+
module = "jsonpath"
|
|
46
|
+
ignore_missing_imports = true
|
|
47
|
+
|
|
48
|
+
[tool.pyright]
|
|
49
|
+
venvPath = "."
|
|
50
|
+
venv = ".venv"
|
|
51
|
+
|
|
52
|
+
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
|
|
2
|
+
|
|
3
|
+
from ._version import (
|
|
4
|
+
__title__,
|
|
5
|
+
__version__,
|
|
6
|
+
__openapi_doc_version__,
|
|
7
|
+
__gen_version__,
|
|
8
|
+
__user_agent__,
|
|
9
|
+
)
|
|
10
|
+
from .sdk import *
|
|
11
|
+
from .sdkconfiguration import *
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
VERSION: str = __version__
|
|
15
|
+
OPENAPI_DOC_VERSION = __openapi_doc_version__
|
|
16
|
+
SPEAKEASY_GENERATOR_VERSION = __gen_version__
|
|
17
|
+
USER_AGENT = __user_agent__
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from .types import Hooks
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
# This file is only ever generated once on the first generation and then is free to be modified.
|
|
5
|
+
# Any hooks you wish to add should be registered in the init_hooks function. Feel free to define them
|
|
6
|
+
# in this file or in separate files in the hooks folder.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def init_hooks(hooks: Hooks):
|
|
10
|
+
# pylint: disable=unused-argument
|
|
11
|
+
"""Add hooks by calling hooks.register{sdk_init/before_request/after_success/after_error}Hook
|
|
12
|
+
with an instance of a hook that implements that specific Hook interface
|
|
13
|
+
Hooks are registered per SDK instance, and are valid for the lifetime of the SDK instance"""
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
|
|
2
|
+
|
|
3
|
+
import httpx
|
|
4
|
+
from .types import (
|
|
5
|
+
SDKInitHook,
|
|
6
|
+
BeforeRequestContext,
|
|
7
|
+
BeforeRequestHook,
|
|
8
|
+
AfterSuccessContext,
|
|
9
|
+
AfterSuccessHook,
|
|
10
|
+
AfterErrorContext,
|
|
11
|
+
AfterErrorHook,
|
|
12
|
+
Hooks,
|
|
13
|
+
)
|
|
14
|
+
from .registration import init_hooks
|
|
15
|
+
from typing import List, Optional, Tuple
|
|
16
|
+
from openrouter.sdkconfiguration import SDKConfiguration
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class SDKHooks(Hooks):
|
|
20
|
+
def __init__(self) -> None:
|
|
21
|
+
self.sdk_init_hooks: List[SDKInitHook] = []
|
|
22
|
+
self.before_request_hooks: List[BeforeRequestHook] = []
|
|
23
|
+
self.after_success_hooks: List[AfterSuccessHook] = []
|
|
24
|
+
self.after_error_hooks: List[AfterErrorHook] = []
|
|
25
|
+
init_hooks(self)
|
|
26
|
+
|
|
27
|
+
def register_sdk_init_hook(self, hook: SDKInitHook) -> None:
|
|
28
|
+
self.sdk_init_hooks.append(hook)
|
|
29
|
+
|
|
30
|
+
def register_before_request_hook(self, hook: BeforeRequestHook) -> None:
|
|
31
|
+
self.before_request_hooks.append(hook)
|
|
32
|
+
|
|
33
|
+
def register_after_success_hook(self, hook: AfterSuccessHook) -> None:
|
|
34
|
+
self.after_success_hooks.append(hook)
|
|
35
|
+
|
|
36
|
+
def register_after_error_hook(self, hook: AfterErrorHook) -> None:
|
|
37
|
+
self.after_error_hooks.append(hook)
|
|
38
|
+
|
|
39
|
+
def sdk_init(self, config: SDKConfiguration) -> SDKConfiguration:
|
|
40
|
+
for hook in self.sdk_init_hooks:
|
|
41
|
+
config = hook.sdk_init(config)
|
|
42
|
+
return config
|
|
43
|
+
|
|
44
|
+
def before_request(
|
|
45
|
+
self, hook_ctx: BeforeRequestContext, request: httpx.Request
|
|
46
|
+
) -> httpx.Request:
|
|
47
|
+
for hook in self.before_request_hooks:
|
|
48
|
+
out = hook.before_request(hook_ctx, request)
|
|
49
|
+
if isinstance(out, Exception):
|
|
50
|
+
raise out
|
|
51
|
+
request = out
|
|
52
|
+
|
|
53
|
+
return request
|
|
54
|
+
|
|
55
|
+
def after_success(
|
|
56
|
+
self, hook_ctx: AfterSuccessContext, response: httpx.Response
|
|
57
|
+
) -> httpx.Response:
|
|
58
|
+
for hook in self.after_success_hooks:
|
|
59
|
+
out = hook.after_success(hook_ctx, response)
|
|
60
|
+
if isinstance(out, Exception):
|
|
61
|
+
raise out
|
|
62
|
+
response = out
|
|
63
|
+
return response
|
|
64
|
+
|
|
65
|
+
def after_error(
|
|
66
|
+
self,
|
|
67
|
+
hook_ctx: AfterErrorContext,
|
|
68
|
+
response: Optional[httpx.Response],
|
|
69
|
+
error: Optional[Exception],
|
|
70
|
+
) -> Tuple[Optional[httpx.Response], Optional[Exception]]:
|
|
71
|
+
for hook in self.after_error_hooks:
|
|
72
|
+
result = hook.after_error(hook_ctx, response, error)
|
|
73
|
+
if isinstance(result, Exception):
|
|
74
|
+
raise result
|
|
75
|
+
response, error = result
|
|
76
|
+
return response, error
|