Repository navigation
feat: add Y-API as a JSON-configured OpenAI-compatible provider #41559
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
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 |
|---|---|---|
| @@ -0,0 +1,247 @@ | ||
| """ | ||
| Tests for Y-API provider configuration and integration. | ||
|
|
||
| Y-API is a JSON-configured OpenAI-compatible relay. Its model ids keep the | ||
| upstream organization prefix, so they contain a slash of their own | ||
| (`y-api/deepseek/deepseek-v4-flash`) -- provider resolution must split on the | ||
| first slash only. | ||
| """ | ||
|
|
||
| import litellm | ||
|
|
||
| BASE_URL = "https://api.y-api.bestvirtualgoods.com/v1" | ||
|
|
||
|
|
||
| class TestYApiProviderConfig: | ||
| """Test Y-API provider configuration""" | ||
|
|
||
| def test_y_api_in_provider_list(self): | ||
| """Test that y-api is in the provider list""" | ||
| from litellm import LlmProviders | ||
|
|
||
| assert hasattr(LlmProviders, "Y_API") | ||
| assert LlmProviders.Y_API.value == "y-api" | ||
| assert "y-api" in litellm.provider_list | ||
|
|
||
| def test_y_api_json_config_exists(self): | ||
| """Test that y-api is configured in providers.json""" | ||
| from litellm.llms.openai_like.json_loader import JSONProviderRegistry | ||
|
|
||
| assert JSONProviderRegistry.exists("y-api") | ||
|
|
||
|
Comment on lines
+30
to
+31
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.
These comments restate configuration and vendor behavior, violating the repository rule that source comments must explain necessary complex logic or direct tools Context Used: CLAUDE.md (source) Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
| y_api = JSONProviderRegistry.get("y-api") | ||
| assert y_api is not None | ||
| assert y_api.base_url == BASE_URL | ||
|
Comment on lines
+24
to
+34
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. These assertions, along with the endpoint matrix checks, inspect static configuration instead of provider behavior, violating the repository's functional-testing requirement before merge Context Used: CLAUDE.md (source) |
||
| assert y_api.api_key_env == "YAPI_API_KEY" | ||
| assert y_api.api_base_env == "YAPI_API_BASE" | ||
| assert y_api.param_mappings == {"max_tokens": "max_completion_tokens"} | ||
|
|
||
| def test_y_api_supported_endpoints(self): | ||
| """Test the endpoints declared for y-api""" | ||
| from litellm.llms.openai_like.json_loader import JSONProviderRegistry | ||
|
|
||
| y_api = JSONProviderRegistry.get("y-api") | ||
| assert y_api is not None | ||
| assert "/v1/chat/completions" in y_api.supported_endpoints | ||
| assert "/v1/responses" in y_api.supported_endpoints | ||
| assert "/v1/messages" in y_api.supported_endpoints | ||
| assert JSONProviderRegistry.supports_responses_api("y-api") is True | ||
|
|
||
| def test_y_api_provider_resolution_with_nested_model_id(self): | ||
| """Test that provider resolution finds y-api and keeps the nested model id""" | ||
| from litellm.litellm_core_utils.get_llm_provider_logic import get_llm_provider | ||
|
|
||
| model, provider, api_key, api_base = get_llm_provider( | ||
| model="y-api/deepseek/deepseek-v4-flash", | ||
| custom_llm_provider=None, | ||
| api_base=None, | ||
| api_key=None, | ||
| ) | ||
|
|
||
| assert model == "deepseek/deepseek-v4-flash" | ||
| assert provider == "y-api" | ||
| assert api_base == BASE_URL | ||
|
|
||
| def test_y_api_api_base_override(self): | ||
| """Test that an explicit api_base / api_key overrides the default""" | ||
| from litellm.litellm_core_utils.get_llm_provider_logic import get_llm_provider | ||
|
|
||
| model, provider, api_key, api_base = get_llm_provider( | ||
| model="y-api/deepseek/deepseek-v4-flash", | ||
| custom_llm_provider=None, | ||
| api_base="https://custom.example.com/v1", | ||
| api_key="sk-test", | ||
| ) | ||
|
|
||
| assert provider == "y-api" | ||
| assert api_base == "https://custom.example.com/v1" | ||
| assert api_key == "sk-test" | ||
|
|
||
| def test_y_api_router_config(self): | ||
| """Test that y-api can be used in Router configuration""" | ||
| from litellm import Router | ||
|
|
||
| router = Router( | ||
| model_list=[ | ||
| { | ||
| "model_name": "y-api-chat", | ||
| "litellm_params": { | ||
| "model": "y-api/deepseek/deepseek-v4-flash", | ||
| "api_key": "test-key", | ||
| }, | ||
| } | ||
| ] | ||
| ) | ||
|
|
||
| assert len(router.model_list) == 1 | ||
| assert router.model_list[0]["model_name"] == "y-api-chat" | ||
|
|
||
| def test_y_api_supported_endpoints_matrix(self): | ||
| """The documented matrix lists y-api with the endpoints it actually serves.""" | ||
| import json | ||
| from pathlib import Path | ||
|
|
||
| import litellm as _litellm | ||
|
|
||
| matrix_path = ( | ||
| Path(_litellm.__file__).parent.parent / "provider_endpoints_support.json" | ||
| ) | ||
| matrix = json.loads(matrix_path.read_text()) | ||
|
|
||
| assert "y-api" in matrix["providers"] | ||
| endpoints = matrix["providers"]["y-api"]["endpoints"] | ||
| assert endpoints["chat_completions"] is True | ||
| assert endpoints["messages"] is True | ||
| assert endpoints["responses"] is True | ||
| # embeddings is advertised false: GET /v1/models returns no embedding | ||
| # model, and POST /v1/embeddings answers | ||
| # "No available channel for model ... under group y-api (distributor)". | ||
| assert endpoints["embeddings"] is False | ||
|
|
||
| def test_y_api_runtime_supported_endpoints_matrix(self): | ||
| """The runtime-served backup matrix (GET /public/supported_endpoints) lists y-api.""" | ||
| import json | ||
| from pathlib import Path | ||
|
|
||
| import litellm as _litellm | ||
|
|
||
| backup_path = ( | ||
| Path(_litellm.__file__).parent / "provider_endpoints_support_backup.json" | ||
| ) | ||
| matrix = json.loads(backup_path.read_text()) | ||
|
|
||
| assert "y-api" in matrix["providers"] | ||
| endpoints = matrix["providers"]["y-api"]["endpoints"] | ||
| assert endpoints["chat_completions"] is True | ||
| assert endpoints["messages"] is True | ||
| assert endpoints["responses"] is True | ||
|
|
||
|
|
||
| class TestYApiDashboardRegistration: | ||
| """The Add Model form must offer Y-API. | ||
|
|
||
| `test_every_backend_provider_is_listed_in_add_model_or_frozen_as_unlisted` | ||
| fails if a provider exists in `LlmProviders` without an entry here, so this | ||
| is the difference between "selectable in the UI" and "silently dropped into | ||
| the frozen unlisted set". | ||
| """ | ||
|
|
||
| @staticmethod | ||
| def _provider_create_fields(): | ||
| import json | ||
| from pathlib import Path | ||
|
|
||
| import litellm | ||
|
|
||
| path = ( | ||
| Path(litellm.__file__).parent | ||
| / "proxy" | ||
| / "public_endpoints" | ||
| / "provider_create_fields.json" | ||
| ) | ||
| with open(path) as f: | ||
| return json.load(f) | ||
|
|
||
| def test_y_api_is_selectable_in_the_add_model_form(self): | ||
| entries = [ | ||
| e | ||
| for e in self._provider_create_fields() | ||
| if e["litellm_provider"] == "y-api" | ||
| ] | ||
| assert ( | ||
| len(entries) == 1 | ||
| ), "y-api must appear exactly once in provider_create_fields.json" | ||
|
|
||
| entry = entries[0] | ||
| assert entry["provider"] == "Y_API" | ||
| assert entry["provider_display_name"] == "Y-API" | ||
| assert entry["default_model_placeholder"].startswith("y-api/") | ||
|
|
||
| fields = {f["key"]: f for f in entry["credential_fields"]} | ||
| assert fields["api_key"]["required"] is True | ||
| assert fields["api_key"]["field_type"] == "password" | ||
| assert fields["api_base"]["required"] is False | ||
| # The base URL is surfaced as the placeholder so admins can see the | ||
| # default without it being written into config. | ||
| assert fields["api_base"]["placeholder"] == BASE_URL | ||
|
|
||
| def test_y_api_is_returned_by_the_public_providers_fields_endpoint(self): | ||
| """The endpoint the Add Model dropdown actually reads from.""" | ||
| from fastapi import FastAPI | ||
| from fastapi.testclient import TestClient | ||
|
|
||
| from litellm.proxy.public_endpoints.public_endpoints import router | ||
|
|
||
| app_instance = FastAPI() | ||
| app_instance.include_router(router) | ||
| test_client = TestClient(app_instance) | ||
|
|
||
| response = test_client.get("/public/providers/fields") | ||
| assert response.status_code == 200 | ||
| providers = {p["litellm_provider"] for p in response.json()} | ||
| assert "y-api" in providers | ||
|
|
||
|
|
||
| class TestYApiTokenParamMapping: | ||
| """`max_tokens` has to reach this relay as `max_completion_tokens`. | ||
|
|
||
| The `openai/*` models y-api fronts reject the legacy name outright: | ||
|
|
||
| Unsupported parameter: 'max_tokens' is not supported with this model. | ||
| Use 'max_completion_tokens' instead. | ||
|
|
||
| LiteLLM's `openai_gpt` base class does not translate it for this provider -- | ||
| the model ids are not in the OpenAI cost map, so the caller's `max_tokens` | ||
| is forwarded verbatim. These tests drive the transformation the provider | ||
| actually applies to caller params, rather than reading the config, because | ||
| the config alone cannot show which parameter name leaves the process. | ||
| """ | ||
|
|
||
| @staticmethod | ||
| def _map(**params): | ||
| from litellm.llms.openai_like.dynamic_config import create_config_class | ||
| from litellm.llms.openai_like.json_loader import JSONProviderRegistry | ||
|
|
||
| provider = JSONProviderRegistry.get("y-api") | ||
| config = create_config_class(provider)() | ||
| return config.map_openai_params( | ||
| non_default_params=dict(params), | ||
| optional_params={}, | ||
| model="gpt-5.6-luna", | ||
| drop_params=False, | ||
| ) | ||
|
|
||
| def test_max_tokens_is_rewritten(self): | ||
| mapped = self._map(max_tokens=16) | ||
| assert mapped["max_completion_tokens"] == 16 | ||
| assert "max_tokens" not in mapped | ||
|
|
||
| def test_max_completion_tokens_passes_through(self): | ||
| mapped = self._map(max_completion_tokens=16) | ||
| assert mapped["max_completion_tokens"] == 16 | ||
| assert "max_tokens" not in mapped | ||
|
|
||
| def test_absent_cap_sends_neither(self): | ||
| mapped = self._map() | ||
| assert "max_tokens" not in mapped | ||
| assert "max_completion_tokens" not in mapped | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Low: OpenAI credential forwarded to Y-API
Adding
y-apihere also adds it toOPENAI_AUDIO_TRANSCRIPTION_PROVIDERSand the speech dispatch. An authenticated caller can submit an audio request for a directy-api/...model without a Y-API key;transcription()andspeech()then fall back toOPENAI_API_KEYwhile retaining Y-API's base URL, sending the deployment's OpenAI credential to that third party. Keep Y-API out of the generic audio-capable set, or gate these transports using its declaredsupported_endpointsand never use OpenAI credential fallbacks for another provider.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reproduced this against a loopback echo server (
base_urlpointed at127.0.0.1, so nothing left the machine). The finding is real — with one correction on where the fix has to land.Repro, as reported.
YAPI_API_KEYunset,OPENAI_API_KEYset:litellm.transcription("y-api/whisper-1", …)POST /v1/audio/transcriptions,Authorization: Bearer <OPENAI_API_KEY>litellm.speech("y-api/tts-1", …)POST /v1/audio/speech, same headerBoth resolved Y-API's own base URL from
providers.jsonand attached the OpenAI credential, so the mechanism you describe is correct.But deleting this line closes the audio surface, not the credential fallback. With
"y-api"removed fromopenai_compatible_providersand the environment unchanged:transcription()ValueError: Unmapped provider passed in.— no request leavesspeech()Unable to map the custom llm provider=y-api to a known provider=— no request leavescompletion("y-api/deepseek/…")POST /v1/chat/completions,Authorization: Bearer <OPENAI_API_KEY>responses("y-api/deepseek/…")POST /v1/responsesreaches the third-party base with noAuthorizationheader at allSo the fallback itself lives in the shared
openai_likecredential resolution, not in this list entry: a JSON provider with no configured key still sends whateverOPENAI_API_KEYholds to that provider's base on the chat path. Opting Y-API out of the audio set makes the flagged case go away; it does not make credential forwarding impossible.Scope. Of the 30 entries in
providers.json, 17 are inopenai_compatible_providers, and 16 of those declare no audio endpoint insupported_endpointsyet still land inOPENAI_AUDIO_TRANSCRIPTION_PROVIDERSthrough the same derivation —publicai,helicone,cognition,chutes,poe,nano-gpt,darkbloom,libertai,tensormesh,parasailamong them. Addingy-apito the list follows the existing convention rather than introducing a new one.Where I think a real fix belongs, and I would rather send it separately so this PR stays a plain provider addition:
OPENAI_AUDIO_TRANSCRIPTION_PROVIDERSfrom each JSON provider's declaredsupported_endpointsinstead of wholesale fromopenai_compatible_providers— that closes the audio surface for all 17 providers at once, and theproviders.jsondata for it is already there.OPENAI_API_KEYfallback when the resolved base URL belongs to a different provider — this is the credential-forwarding step proper, and without it the chat path above still reproduces.Say the word on which you want and I'll open it as its own PR. If you'd rather Y-API just not inherit the audio channel while (1) is pending, deleting line 1027 is a one-line change here — the four calls above show it costs nothing on the paths this provider declares (
/v1/chat/completions,/v1/completions,/v1/responses,/v1/messagesall still dispatched correctly).