litellm/litellm/llms/openai_like
Mateo Wang 66c8f5b1a9
fix(llms): join api_base and route with one shared helper (#47535)
* fix(llms): sanitize trailing slashes in provider get_complete_url

* test(llms): move trailing-slash url tests into each provider's mapped unit file

* fix(llms): strip trailing slashes in Perplexity search and Ollama embedding URLs and run the Topaz tests in CI

* fix(llms): strip trailing slashes in search, DashScope, VolcEngine, GigaChat and Gradient AI URL builders

* fix(fastcrw): strip a trailing slash from api_base and expect one slash in the GigaChat tests

* fix(gigachat): strip a trailing slash from the pass-through api_base

* fix(llms): join api_base and route with one shared helper

A deployment api_base that ends in / made several get_complete_url builders emit a double slash, which Envoy and other gateways answer with 404. Add join_api_base in url_utils and use it in those builders, with a regression test that drives every get_complete_url under litellm/llms with a trailing-slash api_base

* refactor(llms): move per-provider api_base strips onto join_api_base

Use the shared join_api_base helper in the search, chat, embedding, image variation, GigaChat and VolcEngine URL builders instead of a per-provider rstrip and suffix check, so one helper owns the single-slash join

* ci: run the trailing-slash URL builder test in the llms shard

* test(integration): cover trailing-slash api_base across provider URL builders

Wire cells for chat, streaming, messages, responses, embeddings, search and pass-through with and without a trailing slash, an owned-proxy module for the flag-gated providers, env bases and a worker kill, two chaos bursts, and a Topaz SDK cell

* fix(llms): send every provider route through join_api_base

Every place under litellm/llms and litellm/main.py that joined a caller-settable api_base with a route by concatenation or f-string now goes through join_api_base, so an api_base with a trailing slash sends the same upstream path as one without it

Routes made only of a caller-supplied id, model, or passthrough path stay on a plain join over an already normalized base, because the helper's suffix dedup would turn a lookup by id into a list or create call

The sweep test now also reaches the factory-made JSON provider builders, and a new loopback test drives the public entrypoints with an api_base ending in /gw/ and checks that every request path keeps /gw/ with no double slash

* fix(llms): keep the route skip only where a builder already had it

join_api_base is a pure join again. The new ensure_api_route skips the route only when the URL path
already ends in it, and it is used only where the merge base or the trailing-slash test PR head
already skipped. Evals cancel, Predibase, and Azure AI threads append their route again, EdenAI
model listing and Gemini RAG ingestion join through join_api_base, and Azure AI Anthropic checks the
path so a host named anthropic-* builds /anthropic/v1/messages

* fix(azure_ai): read /anthropic from the URL path when building the count_tokens URL

* test(llms): run the Gemini RAG trailing-slash case through aingest

The sync ingest wrapper needs a current event loop, which an earlier async test in the same worker clears

* test(llms): cover the Azure realtime, Bedrock rerank, and oobabooga joins with a trailing-slash api_base

* test(integration): cover api_base joins across providers, query strings and named hosts

* feat(url_utils): add join_api_base and ensure_api_route helpers

* fix(url_utils): keep the scheme slashes when a stripped suffix was the whole host

join_api_base and ensure_api_route now keep the // of a base left with only a scheme,
so http://v1 with /v1 stripped joins to http://v1/files as before. The Azure v1 client
base_url goes through ensure_api_route so a base ending in /openai/v1 no longer doubles it

* fix(azure_ai): build the count_tokens URL from the messages URL

* fix(azure_ai): match anthropic as a whole path segment in the messages URL

* fix(llms): keep main's Azure AI anthropic match and non-string Bedrock endpoints

Azure AI chat and messages go back to cutting the base at the first
/anthropic, as on main, and count_tokens keeps its path check, so a
gateway path like /anthropic-proxy builds the same URLs as main. A base
that already ends in /v1/messages still gets count_tokens next to it.

A non-string aws_bedrock_runtime_endpoint (an httpx.URL VPC endpoint) is
used as its text again in AgentCore, Bedrock responses and the RAGFlow
base, instead of falling back to the default host.

get_volcengine_base_url and GigaChat's get_api_base strip a trailing
slash again for outside callers. The cache-hit wire test is skipped as a
known proxy bug, since one cache hit writes two cache_hit spend rows.

* fix(llms): drop a slash before the api_base query and tighten the route test checks

* test(llms): annotate new test locals as Final and flatten the builder generator

* fix(url_utils): keep a scheme-only api_base hostless so it fails before sending

---------

Co-authored-by: Raushankumar0720 <raushan.singh.cg@gmail.com>
2026-10-21 06:59:32 +00:00
..
chat fix(openai_like)!: surface provider response headers on non-streaming chat completions (#46928) 2026-10-16 17:25:50 -07:00
embedding chore(lint): strip inert type: ignore comments and zero LIT009, LIT010, LIT011 headroom 2026-08-05 02:37:24 -07:00
messages fix(llms): join api_base and route with one shared helper (#47535) 2026-10-21 06:59:32 +00:00
passthrough fix(vllm): write spend log rows for /vllm passthrough calls to router models (#45733) 2026-10-14 12:40:48 -07:00
responses fix(llms): join api_base and route with one shared helper (#47535) 2026-10-21 06:59:32 +00:00
common_utils.py fix(llms): join api_base and route with one shared helper (#47535) 2026-10-21 06:59:32 +00:00
dynamic_config.py fix(llms): join api_base and route with one shared helper (#47535) 2026-10-21 06:59:32 +00:00
json_loader.py feat(openinfer): add OpenInfer as an OpenAI-compatible provider (#41034) 2026-10-17 23:22:19 +00:00
model_info.py refactor: expose core private helpers under public names (#44871) 2026-10-07 00:05:27 +00:00
providers.json feat(openinfer): add OpenInfer as an OpenAI-compatible provider (#41034) 2026-10-17 23:22:19 +00:00
README.md feat(openinfer): add OpenInfer as an OpenAI-compatible provider (#41034) 2026-10-17 23:22:19 +00:00

JSON-Based OpenAI-Compatible Provider Configuration

This directory contains the new JSON-based configuration system for OpenAI-compatible providers.

Overview

Instead of creating a full Python module for simple OpenAI-compatible providers, you can now define them in a single JSON file.

Files

  • providers.json - Configuration file for all JSON-based providers
  • json_loader.py - Loads and parses the JSON configuration
  • dynamic_config.py - Generates Python config classes from JSON (chat + responses)
  • chat/ - OpenAI-like chat completion handlers
  • responses/ - OpenAI-like Responses API handlers

Adding a New Provider

For Simple OpenAI-Compatible Providers

Edit providers.json and add your provider:

{
  "your_provider": {
    "base_url": "https://api.yourprovider.com/v1",
    "api_key_env": "YOUR_PROVIDER_API_KEY"
  }
}

That's it! The provider will be automatically loaded and available.

Optional Configuration Fields

{
  "your_provider": {
    "base_url": "https://api.yourprovider.com/v1",
    "api_key_env": "YOUR_PROVIDER_API_KEY",
    
    // Optional: Override base_url via environment variable
    "api_base_env": "YOUR_PROVIDER_API_BASE",
    
    // Optional: Which base class to use (default: "openai_gpt")
    "base_class": "openai_gpt",  // or "openai_like"
    
    // Optional: Parameter name mappings
    "param_mappings": {
      "max_completion_tokens": "max_tokens"
    },
    
    // Optional: Parameter constraints
    "constraints": {
      "temperature_max": 1.0,
      "temperature_min": 0.0,
      "temperature_min_with_n_gt_1": 0.3,
      // /v1/messages providers only: keep Anthropic cache_control extensions
      // such as ttl instead of stripping them down to {"type": ...}
      "cache_control_ttl": true
    },
    
    // Optional: Special handling flags
    "special_handling": {
      "convert_content_list_to_string": true
    },

    // Optional: Capabilities every model of this provider has, used when a
    // model has no cost map row or its row leaves the field unset
    "provider_info": {
      "supports_function_calling": true
    }
  }
}

Example: PublicAI

The first JSON-configured provider:

{
  "publicai": {
    "base_url": "https://api.publicai.co/v1",
    "api_key_env": "PUBLICAI_API_KEY",
    "api_base_env": "PUBLICAI_API_BASE",
    "base_class": "openai_gpt",
    "param_mappings": {
      "max_completion_tokens": "max_tokens"
    },
    "special_handling": {
      "convert_content_list_to_string": true
    }
  }
}

Usage

import litellm

response = litellm.completion(
    model="publicai/swiss-ai/apertus-8b-instruct",
    messages=[{"role": "user", "content": "Hello"}],
)

Responses API Support

Providers that support the OpenAI Responses API (/v1/responses) can declare it via supported_endpoints:

{
  "your_provider": {
    "base_url": "https://api.yourprovider.com/v1",
    "api_key_env": "YOUR_PROVIDER_API_KEY",
    "supported_endpoints": ["/v1/chat/completions", "/v1/responses"]
  }
}

This enables litellm.responses(model="your_provider/model-name", ...) with zero Python code. The provider inherits all request/response handling from OpenAI's Responses API config.

If supported_endpoints is omitted, it defaults to [] (only chat completions, which is always enabled for JSON providers).

How It Works

  1. json_loader.py checks supported_endpoints for /v1/responses
  2. dynamic_config.py generates a responses config class (inherits from OpenAIResponsesAPIConfig)
  3. ProviderConfigManager.get_provider_responses_api_config() returns the generated config
  4. Request/response transformation is inherited from OpenAI — no custom code needed

Benefits

  • Simple: 2-5 lines of JSON vs 100+ lines of Python
  • Fast: Add a provider in 5 minutes
  • Safe: No Python code to mess up
  • Consistent: All providers follow the same pattern
  • Maintainable: Centralized configuration

When to Use Python Instead

Use a Python config class if you need:

  • Custom authentication (OAuth, rotating tokens, etc.)
  • Complex request/response transformations
  • Provider-specific streaming logic
  • Advanced tool calling transformations

For providers that are mostly OpenAI-compatible but need small overrides (e.g. preset model handling), you can inherit from OpenAIResponsesAPIConfig and override only what's needed — see litellm/llms/perplexity/responses/transformation.py for a minimal example (~40 lines).

Implementation Details

How It Works

  1. json_loader.py loads providers.json on import
  2. dynamic_config.py generates config classes on-demand
  3. Provider resolution checks JSON registry first
  4. ProviderConfigManager returns JSON-based configs

Integration Points

The JSON system is integrated at:

  • litellm/litellm_core_utils/get_llm_provider_logic.py - Provider resolution
  • litellm/utils.py - ProviderConfigManager (chat + responses)
  • litellm/responses/main.py - Responses API routing
  • litellm/constants.py - openai_compatible_providers list