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.
from portkey_ai import Portkeyclient = Portkey( api_key="your_api_key_here", # Or use the env var PORTKEY_API_KEY provider="@your_virtual_key_here" # Or use config="cf-***")response = client.chat.completions.create( messages=[{"role": "user", "content": "Hello, world!"}], model="gpt-4o" # Example provider/model)
You can use either a Virtual Key or a Config object to select your AI provider. Find more info on different authentication mechanisms here.
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.
Disabling SSL certificate verification is insecure and should only be used for debugging or in trusted internal environments. Never use this in production.
Here’s how you can use these headers with the Python SDK:
Copy
Ask AI
portkey = Portkey( api_key="PORTKEY_API_KEY", provider="@VIRTUAL_KEY", # Add any other headers from the reference)# Or at runtimecompletion = portkey.with_options( trace_id="your_trace_id", metadata={"_user": "user_id"}, # Add any other headers as needed).chat.completions.create( messages=[{"role": "user", "content": "Hello!"}], model="gpt-4o")
Yes! Portkey’s Python SDK is OpenAI-compatible. You can also use any OpenAI-compatible library by pointing it to the Portkey API endpoint and using your Portkey API key.
How do I fix CERTIFICATE_VERIFY_FAILED or SSL errors?
If you are behind a VPN (like Zscaler) or a corporate proxy and see SSL/certificate errors, you can pass a custom httpx.Client to the Portkey SDK with SSL verification disabled. See the docs above for an example. Warning: Disabling SSL verification is insecure—only use this as a last resort or for debugging.