Repository navigation
feat(openai_like): add Responses API support to JSON providers #21398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
0c27f20
926a0df
7bac87e
23e4f00
e223b29
e0927d1
90a77c6
a2781f0
636b474
6bb2905
ff97b05
5e32ace
da76e17
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -166,3 +166,63 @@ def custom_llm_provider(self) -> Optional[str]: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return provider.slug | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return JSONProviderConfig | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _responses_config_cache: dict = {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def create_responses_config_class(provider: SimpleProviderConfig): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Generate a Responses API config class dynamically from JSON configuration. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Parallel to create_config_class() but for /v1/responses endpoints. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Classes are cached per provider slug to avoid regeneration on every request. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if provider.slug in _responses_config_cache: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return _responses_config_cache[provider.slug] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from litellm.llms.openai_like.responses.transformation import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| OpenAILikeResponsesConfig, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from litellm.types.router import GenericLiteLLMParams | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class JSONProviderResponsesConfig(OpenAILikeResponsesConfig): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @property | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def custom_llm_provider(self): # type: ignore[override] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return provider.slug | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def validate_environment( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| headers: dict, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model: str, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| litellm_params: Optional[GenericLiteLLMParams], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> dict: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| litellm_params = litellm_params or GenericLiteLLMParams() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| api_key = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| litellm_params.api_key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| or get_secret_str(provider.api_key_env) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if api_key: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Chesars marked this conversation as resolved.
Resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| headers["Authorization"] = f"Bearer {api_key}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return headers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Chesars marked this conversation as resolved.
Resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def get_complete_url( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| api_base: Optional[str], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| litellm_params: dict, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> str: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if not api_base: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if provider.api_base_env: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| api_base = get_secret_str(provider.api_base_env) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if not api_base: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| api_base = provider.base_url | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if api_base is None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise ValueError( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f"api_base is required for provider {provider.slug}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| api_base = api_base.rstrip("/") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return f"{api_base}/responses" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+208
to
+225
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The generated URL appends only If a provider sets Consider adding a validation check to enforce the convention and provide a clear error message:
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _responses_config_cache[provider.slug] = JSONProviderResponsesConfig | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return JSONProviderResponsesConfig | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from litellm.llms.openai_like.responses.transformation import ( | ||
| OpenAILikeResponsesConfig, | ||
| ) | ||
|
|
||
| __all__ = ["OpenAILikeResponsesConfig"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| """ | ||
| OpenAI-like Responses API transformation. | ||
|
|
||
| Base class for JSON-declared providers that support the /v1/responses endpoint. | ||
| Inherits everything from OpenAIResponsesAPIConfig; subclasses only override | ||
| provider-specific resolution (slug, API key env var, base URL). | ||
| """ | ||
|
|
||
| from typing import Optional, Union | ||
|
|
||
| from litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfig | ||
| from litellm.secret_managers.main import get_secret_str | ||
| from litellm.types.router import GenericLiteLLMParams | ||
| from litellm.types.utils import LlmProviders | ||
|
|
||
|
|
||
| class OpenAILikeResponsesConfig(OpenAIResponsesAPIConfig): | ||
| """ | ||
| Responses API config for OpenAI-compatible providers declared via JSON. | ||
|
|
||
| Concrete per-provider classes are generated dynamically in dynamic_config.py. | ||
| This base provides the three overridable hooks that the dynamic generator | ||
| fills in: custom_llm_provider, validate_environment, get_complete_url. | ||
| """ | ||
|
|
||
| @property | ||
| def custom_llm_provider(self) -> Union[str, LlmProviders]: # type: ignore[override] | ||
| return "openai_like" | ||
|
|
||
| def validate_environment( | ||
| self, | ||
| headers: dict, | ||
| model: str, | ||
| litellm_params: Optional[GenericLiteLLMParams], | ||
| ) -> dict: | ||
| litellm_params = litellm_params or GenericLiteLLMParams() | ||
| api_key = litellm_params.api_key or get_secret_str("OPENAI_LIKE_API_KEY") | ||
| if api_key: | ||
| headers["Authorization"] = f"Bearer {api_key}" | ||
| return headers | ||
|
|
||
| def get_complete_url( | ||
| self, | ||
| api_base: Optional[str], | ||
| litellm_params: dict, | ||
| ) -> str: | ||
| api_base = api_base or get_secret_str("OPENAI_LIKE_API_BASE") | ||
| if not api_base: | ||
| raise ValueError("api_base is required for openai_like provider") | ||
| api_base = api_base.rstrip("/") | ||
| return f"{api_base}/responses" |
Uh oh!
There was an error while loading. Please reload this page.