Python
Official Portkey Python SDK to help take your AI apps to production
The official Python SDK makes it easy to integrate Portkey into any Python application. Enjoy unified access to 250+ LLMs, advanced observability, routing, governance, and enterprise features with just a few lines of code.
Installation
Install the Portkey SDK from PyPI:
API Key Setup
- Create a Portkey API key in your dashboard.
- Store your API key securely as an environment variable:
Quickstart
Here’s a minimal example to get you started:
Authentication & Configuration
The SDK requires:
- Portkey API Key: Your Portkey API key (env var
PORTKEY_API_KEY
recommended) - Provider Authentication:
- Virtual Key: The Virtual Key of your chosen AI provider
- Config: The Config object or config slug for advanced routing
- Provider Slug + Auth Headers: Useful if you do not want to save your API keys to Portkey and make direct requests.
Async Usage
Portkey supports Async usage - just use AsyncPortkey
client instead of Portkey
with await
:
Using a Custom httpx Client
If you need to customize HTTP networking—for example, to disable SSL verification due to VPNs like Zscaler or to use custom proxies—you can pass your own httpx.Client
to the Portkey SDK.
Example: Disable SSL Verification
- You can use any
httpx.Client
options (e.g., for proxies, timeouts, custom headers). - For async usage, pass an
httpx.AsyncClient
toAsyncPortkey
. - See OpenAI Python SDK: Configuring the HTTP client for more examples and best practices.
Adding Trace ID or Metadata
You can choose to override the configuration in individual requests as well and send trace id or metadata along with each request.
Parameters
List of All Headers
View the complete list of headers that can be used with Portkey API requests, including authentication, configuration, and custom headers.
Here’s how you can use these headers with the Python SDK:
Troubleshooting & Support
- Having trouble? Email support or book a demo with our team.
- View the SDK on GitHub
- Report issues or request features