Skip to content

refactor: introduce LLM provider adapter ABC with Portkey gateway support#3703

Open
crivetimihai wants to merge 1 commit intomainfrom
llm-gateway-rust
Open

refactor: introduce LLM provider adapter ABC with Portkey gateway support#3703
crivetimihai wants to merge 1 commit intomainfrom
llm-gateway-rust

Conversation

@crivetimihai
Copy link
Copy Markdown
Member

@crivetimihai crivetimihai commented Mar 17, 2026

Summary

  • Introduces an Abstract Base Class (LLMProviderAdapter) for LLM provider HTTP proxy adapters, consolidating provider-specific wire-format logic that was previously scattered across ~6 if/elif chains in llm_proxy_service.py
  • Adds concrete adapter implementations for OpenAI, Azure OpenAI, Anthropic, Ollama (native + OpenAI-compat), and Portkey
  • Adds a registry/factory (get_adapter, get_portkey_adapter) that maps LLMProviderType to adapter instances
  • Adds Portkey external LLM gateway runtime support (original PR scope)
  • Reduces llm_proxy_service.py from ~987 lines to ~456 lines by extracting provider-specific logic into self-contained adapter classes

Architecture

                      ┌─────────────────────┐
                      │  LLMProviderAdapter  │  (ABC)
                      │─────────────────────│
                      │  build_request()     │
                      │  transform_response()│
                      │  transform_stream()  │
                      │  uses_sse_framing    │
                      └──────────┬──────────┘
           ┌──────────┬─────────┼─────────┬──────────┐
           │          │         │         │          │
      OpenAI    Anthropic    Azure    Ollama    Portkey
      Adapter    Adapter    Adapter   Adapter   Adapter

Adapter classes

Adapter Extends Provider types
OpenAIAdapter ABC openai, openai_compatible, cohere, mistral, groq, together
AzureOpenAIAdapter OpenAI azure_openai
AnthropicAdapter ABC anthropic
OllamaNativeAdapter ABC ollama (native /api/chat)
OllamaOpenAICompatAdapter OpenAI ollama (/v1 endpoint)
PortkeyAdapter OpenAI portkey (+ gateway routing)

Key design decisions

  • Stateless singletons: Adapters are reused across requests (no per-request state)
  • Injected get_api_key: Adapters receive API key decryption as a callable, keeping them decoupled from LLMProxyService internals
  • Gateway routing stays separate: should_route_provider_via_llm_gateway() remains in llm_provider_service.py — it's an orthogonal routing decision, not a wire-format concern
  • LangChain path untouched: mcp_client_chat_service.py is a different concern (creating SDK objects, not HTTP requests)

New files

mcpgateway/services/llm_adapters/
    __init__.py, base.py, registry.py,
    openai_adapter.py, azure_openai_adapter.py,
    anthropic_adapter.py, ollama_adapter.py, portkey_adapter.py

tests/unit/mcpgateway/services/llm_adapters/
    test_openai_adapter.py, test_azure_openai_adapter.py,
    test_anthropic_adapter.py, test_ollama_adapter.py,
    test_portkey_adapter.py, test_registry.py

Modified files

  • mcpgateway/services/llm_proxy_service.py — replaced if/elif chains with adapter dispatch, removed extracted private methods
  • tests/unit/mcpgateway/services/test_llm_proxy_service.py — updated tests to call adapters directly instead of deleted private methods

Test plan

  • 66 new adapter unit tests pass (tests/unit/mcpgateway/services/llm_adapters/)
  • 76 existing proxy service tests pass (behavioral no-ops verified at each phase)
  • Full unit suite passes
  • Formatting clean (make autoflake isort black)

🤖 Generated with Claude Code

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
@crivetimihai crivetimihai self-assigned this Mar 17, 2026
@crivetimihai crivetimihai added experimental Experimental features, test proposed MCP Specification changes rust Rust programming COULD P3: Nice-to-have features with minimal impact if left out; included if time permits labels Mar 17, 2026
@crivetimihai crivetimihai added this to the Release 1.1.0 milestone Mar 20, 2026
@jonpspri jonpspri changed the title feat: add Portkey external LLM gateway runtime refactor: introduce LLM provider adapter ABC with Portkey gateway support Apr 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

COULD P3: Nice-to-have features with minimal impact if left out; included if time permits experimental Experimental features, test proposed MCP Specification changes rust Rust programming

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants