How portkey-ai/portkey-python-sdk works
Main Components
• Client Application: User code that imports and calls the SDK (using constructs like Portkey and AsyncPortkey).
• API Gateway/SDK Core: The top-level interface that users interact with. This central component handles the unified API signature and orchestrates calls.
• API Resources Modules: Organized under portkey_ai/api_resources and its subdirectories. There are individual modules (e.g., chat, completions, embeddings, audio, batches, moderations, images, assistants, threads, vector stores) representing separate API endpoints.
• Instrumentation & Observability: Within api_resources/instrumentation, note the support for several observability tools (for logging, request tracing, and custom instrumentation patches for different providers). This is a stream of sub-components for integrating with vendor-specific instrumentation (e.g., openai, crewai, langgraph, litellm).
• Integration Wrappers: Additional integrations in directories like portkey_ai/langchain, portkey_ai/llamaindex, and portkey_ai/llms which provide custom callback handlers and adapters to work with popular AI orchestration frameworks.
• Configuration and Testing: The tests directory (with multiple config files and test cases) represents how various scenarios (single provider, load balancing, fallbacks, retries, etc.) are tested.
Depicting Relationships and Interactions
• Separate modules within the SDK Core:
– “Instrumentation & Observability” component that connects to the API Resources, emphasizing that each API call can be augmented with logging, request tracing, metadata, and feedback.
• Optionally, include a “Configuration & Utilities” section (drawing from the configuration files and test setups) that influences the behavior of the SDK.
Architectural Patterns or Design Principles
• Adapter Pattern: The SDK maps OpenAI calls to its own internal methods that add fallback, caching, and load balancing.
• API Gateway Pattern: The central Portkey interface serves as a gateway for routing client calls to various underlying provider-specific modules.
• Modularization: Clear separation between core API functionalities, instrumentation, external integrations, and test configurations.
• Observability: Show that every API interaction can optionally go through the instrumentation pipeline—making observability and logging a first-class concern.
Relevant Technologies and Frameworks
• Python as the base language.
• Asynchronous programming (AsyncPortkey) for improved concurrency.
• Integration with third-party libraries (LangChain, LlamaIndex) to support additional use cases.
• OpenAI as the primary reference provider, with additional abstraction to support “any provider” via a unified interface.
• Test frameworks and configuration management (pytest, Makefile, setup.cfg, pyproject.toml).
│
▼
│
│ ├── Chat Module
│ ├── Completions Module
│ ├── Embeddings Module
│ ├── Audio Module
│ └── … (Other endpoints)
│
│ ├── Logging
│ ├── Request Tracing
│ └── Custom Metadata / Feedback
│
├── LangChain Adapter
├── LlamaIndex Adapter
└── Others
│
▼