From 0cc072c999514faefd891efcd1a8b406e5350f04 Mon Sep 17 00:00:00 2001 From: Vansh <72547809+vansh-one@users.noreply.github.com> Date: Tue, 17 Nov 2026 14:18:52 +0530 Subject: [PATCH] feat(litellm): add Vispark provider with Vision Small/Medium/Large (#41192) * feat(litellm): add Vispark provider with Vision Small/Medium/Large Rebased onto latest main: re-applied vispark registrations (model sets, provider enum, JSON provider registry, pricing catalogs, dashboard fields, endpoints matrix) and moved provider test to tests/unit/llms/openai_like/ per current layout. * fix(litellm): keep basedpyright reportUnknownArgumentType budget flat for vispark * fix(litellm): satisfy type-discipline budget for vispark cast * chore: retrigger CI (prior MCP dep-resolution leg cancelled by infra) * test(vispark): cover routing, text completion and cost; sync endpoint matrix backup * test(vispark): tolerate cost-map entries without a provider when listing models * chore(vispark): regenerate the provider endpoint matrix with main's generator * fix(vispark): bill the 600 input token minimum and bridge JSON schemas and PDF file parts Vispark charges every request for at least 600 input tokens, so the cost map gains min_billed_input_tokens and the generic cost path bills up to it. Vispark answers 500 to a json_schema that sets additionalProperties, which the OpenAI SDK always sends for a Pydantic model, so the provider strips it. Vispark reads PDFs only as image_url parts, so file parts are sent that way. * fix(vispark): bill the input token minimum under custom pricing and strip every additionalProperties value * refactor(router): inline the billing minimum lookup into the deployment cost payload * fix(vispark): scope the input minimum per deployment and reject map-field schemas Keep a deployment's min_billed_input_tokens on its own cost-map entry so it no longer changes billing for other deployments of the same backend model. Strip boolean additionalProperties for Vispark, answer 400 for schema-valued ones unless drop_params is set, and do it without recursion. * fix(budget): reserve at least a deployment's billed input minimum * test(budget): patch the renamed deployment info helper * fix(vispark): reject file_id parts, keep properties named additionalProperties, bill the minimum on dashscope and perplexity * fix(vispark): send inline file_data when a file part also carries a file_id * fix: keep provider-stated cost for minimum-only deployments and skip the minimum without a counted prompt A deployment that sets only min_billed_input_tokens no longer drops Perplexity or Kyma stated cost, streamed cost headers no longer bill the minimum before usage arrives, and tiered deployments now carry the minimum * fix: bill provider-stated cost for minimum-only deployments and charge a disconnect only the group's shared minimum * style: drop the restating comment on the vispark provider entry * fix(cost): keep the provider-reported video cost for a deployment that sets only a billing minimum * fix(cost): read the billing minimum the way the cost map guard accepts it A cost map row can write min_billed_input_tokens as "600" or 600.0, which the catalog's count parser accepts. The chat cost path subtracted the raw value and raised TypeError on a string, and the custom-rate path billed no minimum unless the value was an int. Both now read it through one non-negative int adapter Also loosen the Scaleway file part test, since main now adds a default filename * fix(vispark): keep schema literals and reserve at the short prompt's tier Removing additionalProperties now skips the literal data in const, enum, default and examples, so a value holding that key reaches Vispark unchanged Budget reservation picks the pricing tier from the counted prompt and only then applies the billed input minimum, the way billing does, so a minimum that crosses into a cheaper tier no longer reserves less than the request bills * fix(utils): list JSON providers whose API key env var differs from their name * fix(vispark): answer 400 for a file part with no file instead of crashing * fix(cost): use a deployment's own input minimum with per-token rates /spend/calculate and /cost/estimate priced custom-rate deployments with the cost map's min_billed_input_tokens, so a deployment that sets its own minimum (0 or 1000, say) got a quote that differs from what its live requests bill. Both endpoints now pass the deployment's minimum with the rates, and the per-token floor also reads it from the requested model info before falling back to the cost map * chore(vispark): keep catalog and endpoint formatting aligned * fix(vispark): mark provider registry reference final * fix(vispark): reject missing provider credentials before fallback * fix(vispark): preserve authenticated text completion clients * fix(vispark): preserve authenticated chat completion clients * fix(vispark): reject unused SDK credentials on HTTP transport --------- Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com> --- litellm/__init__.py | 5 + litellm/constants.py | 3 + litellm/llms/openai_like/providers.json | 12 + litellm/main.py | 17 ++ ...odel_prices_and_context_window_backup.json | 96 +++++++ .../provider_endpoints_support_backup.json | 26 ++ .../provider_create_fields.json | 28 +++ litellm/types/utils.py | 1 + model_prices_and_context_window.json | 96 +++++++ provider_endpoints_support.json | 26 ++ .../llms/openai_like/test_vispark_provider.py | 234 ++++++++++++++++++ 11 files changed, 544 insertions(+) create mode 100644 tests/unit/llms/openai_like/test_vispark_provider.py diff --git a/litellm/__init__.py b/litellm/__init__.py index 1871a3aef78..e60060aa97a 100644 --- a/litellm/__init__.py +++ b/litellm/__init__.py @@ -703,6 +703,7 @@ streamlake_models: Final[Set[str]] = set() wally_models: Final[Set[str]] = set() zerogpu_models: Final[Set[str]] = set() v0_models: Set = set() +vispark_models: Final[Set[str]] = set() morph_models: Set = set() opencode_go_models: Final[set[str]] = set() # mutable-ok: populated when the shared model catalog loads lambda_ai_models: Set = set() @@ -1003,6 +1004,8 @@ def _populate_provider_model_sets(model_cost_map: Mapping[str, Mapping[str, obje zerogpu_models.add(key) elif value.get("litellm_provider") == "v0": v0_models.add(key) + elif value.get("litellm_provider") == "vispark": + vispark_models.add(key) elif value.get("litellm_provider") == "morph": morph_models.add(key) elif value.get("litellm_provider") == "opencode_go": @@ -1183,6 +1186,7 @@ model_list = list( | wally_models | zerogpu_models | v0_models + | vispark_models | morph_models | opencode_go_models | lambda_ai_models @@ -1315,6 +1319,7 @@ def _build_models_by_provider() -> dict: "wally": wally_models, "zerogpu": zerogpu_models, "v0": v0_models, + "vispark": vispark_models, "morph": morph_models, "opencode_go": opencode_go_models, "lambda_ai": lambda_ai_models, diff --git a/litellm/constants.py b/litellm/constants.py index 015731511eb..08c8fd0c493 100644 --- a/litellm/constants.py +++ b/litellm/constants.py @@ -1113,6 +1113,7 @@ openai_compatible_endpoints: Final[list[str]] = [ "https://inference.coralbricks.ai/v1", "https://api.cortecs.ai/v1", "https://api.scx.ai/v1", + "https://api.lab.vispark.in/v1", "https://api.prisminference.com/v1", "https://api.reka.ai/v1", "https://inference.runanywhere.ai/v1", @@ -1193,6 +1194,7 @@ openai_compatible_providers: Final[list[str]] = [ "pinstripes", # Pinstripes - JSON-configured provider "openinfer", "darkbloom", + "vispark", "meta", # Meta Model API (Muse Spark) - JSON-configured provider "cognition", "coralbricks", @@ -1229,6 +1231,7 @@ openai_text_completion_compatible_providers: Final[list] = [ # providers that s "poe", "chutes", "v0", + "vispark", "lambda_ai", "hyperbolic", "wandb", diff --git a/litellm/llms/openai_like/providers.json b/litellm/llms/openai_like/providers.json index f937e4d2f75..74c5c4fca1b 100644 --- a/litellm/llms/openai_like/providers.json +++ b/litellm/llms/openai_like/providers.json @@ -314,5 +314,17 @@ "supports_function_calling": true, "supports_reasoning": true } + }, + "vispark": { + "base_url": "https://api.lab.vispark.in/v1", + "api_key_env": "VISPARK_LAB_API_KEY", + "api_base_env": "VISPARK_LAB_API_BASE", + "param_mappings": { + "max_completion_tokens": "max_tokens" + }, + "supported_endpoints": [ + "/v1/chat/completions", + "/v1/completions" + ] } } diff --git a/litellm/main.py b/litellm/main.py index f093a93d205..ea11fcdcd48 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -1974,6 +1974,13 @@ def _complete_azure_ai(ctx: CompletionDispatchContext) -> _CompletionDispatchRes return response +def _validate_vispark_api_key(provider: str | None, api_key: str | None, model: str, has_client: bool = False) -> None: + if provider == "vispark" and not api_key and not has_client: + raise litellm.AuthenticationError( + message="Set VISPARK_LAB_API_KEY or pass api_key", llm_provider="vispark", model=model + ) + + def _complete_text_completion_openai( ctx: CompletionDispatchContext, ) -> _CompletionDispatchResult: @@ -2006,6 +2013,8 @@ def _complete_text_completion_openai( openai.api_version = None # set API KEY + _validate_vispark_api_key(custom_llm_provider, api_key, model, has_client=client is not None) + api_key = api_key or litellm.api_key or litellm.openai_key or get_secret("OPENAI_API_KEY") headers = headers or litellm.headers @@ -2874,6 +2883,14 @@ def _complete_custom_openai( # for deepinfra/perplexity/anyscale/groq/friendliai we check in get_llm_provider and pass in the api base from there api_base = _resolve_openai_api_base(api_base) + _validate_vispark_api_key( + custom_llm_provider, + api_key, + model, + has_client=isinstance(client, (openai.OpenAI, openai.AsyncOpenAI)) + and not get_secret_bool("EXPERIMENTAL_OPENAI_BASE_LLM_HTTP_HANDLER"), + ) + if custom_llm_provider == "bigmodel" and not api_key: raise litellm.AuthenticationError( message="Set BIGMODEL_API_KEY or pass api_key", llm_provider="bigmodel", model=model diff --git a/litellm/model_prices_and_context_window_backup.json b/litellm/model_prices_and_context_window_backup.json index 6e5b99537ad..53835b38f56 100644 --- a/litellm/model_prices_and_context_window_backup.json +++ b/litellm/model_prices_and_context_window_backup.json @@ -51727,6 +51727,102 @@ "supports_tool_choice": true, "supports_vision": true }, + "vispark/vision-small": { + "cache_read_input_token_cost": 0.0, + "input_cost_per_token": 1.05e-06, + "litellm_provider": "vispark", + "max_input_tokens": 1000000, + "max_output_tokens": 65536, + "mode": "chat", + "output_cost_per_token": 3.16e-06, + "source": "https://api.lab.vispark.in/v1/models", + "supported_endpoints": [ + "/v1/chat/completions", + "/v1/completions" + ], + "supported_modalities": [ + "text", + "image", + "audio", + "video" + ], + "supported_output_modalities": [ + "text" + ], + "supports_function_calling": true, + "supports_native_streaming": true, + "supports_parallel_function_calling": true, + "supports_pdf_input": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_system_messages": true, + "supports_tool_choice": true, + "supports_vision": true + }, + "vispark/vision-medium": { + "cache_read_input_token_cost": 0.0, + "input_cost_per_token": 4.21e-06, + "litellm_provider": "vispark", + "max_input_tokens": 1000000, + "max_output_tokens": 65536, + "mode": "chat", + "output_cost_per_token": 1.263e-05, + "source": "https://api.lab.vispark.in/v1/models", + "supported_endpoints": [ + "/v1/chat/completions", + "/v1/completions" + ], + "supported_modalities": [ + "text", + "image", + "audio", + "video" + ], + "supported_output_modalities": [ + "text" + ], + "supports_function_calling": true, + "supports_native_streaming": true, + "supports_parallel_function_calling": true, + "supports_pdf_input": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_system_messages": true, + "supports_tool_choice": true, + "supports_vision": true + }, + "vispark/vision-large": { + "cache_read_input_token_cost": 0.0, + "input_cost_per_token": 7.37e-06, + "litellm_provider": "vispark", + "max_input_tokens": 1000000, + "max_output_tokens": 65536, + "mode": "chat", + "output_cost_per_token": 2.211e-05, + "source": "https://api.lab.vispark.in/v1/models", + "supported_endpoints": [ + "/v1/chat/completions", + "/v1/completions" + ], + "supported_modalities": [ + "text", + "image", + "audio", + "video" + ], + "supported_output_modalities": [ + "text" + ], + "supports_function_calling": true, + "supports_native_streaming": true, + "supports_parallel_function_calling": true, + "supports_pdf_input": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_system_messages": true, + "supports_tool_choice": true, + "supports_vision": true + }, "vercel_ai_gateway/alibaba/qwen-3-14b": { "input_cost_per_token": 8e-08, "litellm_provider": "vercel_ai_gateway", diff --git a/litellm/provider_endpoints_support_backup.json b/litellm/provider_endpoints_support_backup.json index b35037ee651..5a9d300b10a 100644 --- a/litellm/provider_endpoints_support_backup.json +++ b/litellm/provider_endpoints_support_backup.json @@ -4332,6 +4332,32 @@ "generateContent": true } }, + "vispark": { + "display_name": "Vispark (`vispark`)", + "url": "https://lab.vispark.in/#vision", + "endpoints": { + "chat_completions": true, + "responses": true, + "text_completion": true, + "embeddings": false, + "image_generations": false, + "image_edits": false, + "audio_transcriptions": false, + "audio_speech": false, + "rerank": false, + "ocr": false, + "search": false, + "moderations": false, + "realtime": false, + "video_generations": false, + "batches": false, + "messages": true, + "interactions": true, + "systemone": false, + "a2a": true, + "generateContent": true + } + }, "vercel_ai_gateway": { "display_name": "Vercel AI Gateway (`vercel_ai_gateway`)", "url": "https://docs.litellm.ai/docs/providers/vercel_ai_gateway", diff --git a/litellm/proxy/public_endpoints/provider_create_fields.json b/litellm/proxy/public_endpoints/provider_create_fields.json index 42f2c0022c1..315156ba23b 100644 --- a/litellm/proxy/public_endpoints/provider_create_fields.json +++ b/litellm/proxy/public_endpoints/provider_create_fields.json @@ -4548,5 +4548,33 @@ } ], "default_model_placeholder": "bigmodel/glm-5.3" + }, + { + "provider": "VISPARK", + "provider_display_name": "Vispark", + "litellm_provider": "vispark", + "credential_fields": [ + { + "key": "api_base", + "label": "API Base", + "placeholder": "https://api.lab.vispark.in/v1", + "tooltip": null, + "required": false, + "field_type": "text", + "options": null, + "default_value": null + }, + { + "key": "api_key", + "label": "API Key", + "placeholder": null, + "tooltip": null, + "required": true, + "field_type": "password", + "options": null, + "default_value": null + } + ], + "default_model_placeholder": "vispark/vision-medium" } ] diff --git a/litellm/types/utils.py b/litellm/types/utils.py index 47194ad2de5..1056af1af2a 100644 --- a/litellm/types/utils.py +++ b/litellm/types/utils.py @@ -4747,6 +4747,7 @@ class LlmProviders(str, Enum): MOONSHOT = "moonshot" PUBLICAI = "publicai" V0 = "v0" + VISPARK = "vispark" MORPH = "morph" OPENCODE_GO = "opencode_go" LAMBDA_AI = "lambda_ai" diff --git a/model_prices_and_context_window.json b/model_prices_and_context_window.json index 6e5b99537ad..53835b38f56 100644 --- a/model_prices_and_context_window.json +++ b/model_prices_and_context_window.json @@ -51727,6 +51727,102 @@ "supports_tool_choice": true, "supports_vision": true }, + "vispark/vision-small": { + "cache_read_input_token_cost": 0.0, + "input_cost_per_token": 1.05e-06, + "litellm_provider": "vispark", + "max_input_tokens": 1000000, + "max_output_tokens": 65536, + "mode": "chat", + "output_cost_per_token": 3.16e-06, + "source": "https://api.lab.vispark.in/v1/models", + "supported_endpoints": [ + "/v1/chat/completions", + "/v1/completions" + ], + "supported_modalities": [ + "text", + "image", + "audio", + "video" + ], + "supported_output_modalities": [ + "text" + ], + "supports_function_calling": true, + "supports_native_streaming": true, + "supports_parallel_function_calling": true, + "supports_pdf_input": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_system_messages": true, + "supports_tool_choice": true, + "supports_vision": true + }, + "vispark/vision-medium": { + "cache_read_input_token_cost": 0.0, + "input_cost_per_token": 4.21e-06, + "litellm_provider": "vispark", + "max_input_tokens": 1000000, + "max_output_tokens": 65536, + "mode": "chat", + "output_cost_per_token": 1.263e-05, + "source": "https://api.lab.vispark.in/v1/models", + "supported_endpoints": [ + "/v1/chat/completions", + "/v1/completions" + ], + "supported_modalities": [ + "text", + "image", + "audio", + "video" + ], + "supported_output_modalities": [ + "text" + ], + "supports_function_calling": true, + "supports_native_streaming": true, + "supports_parallel_function_calling": true, + "supports_pdf_input": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_system_messages": true, + "supports_tool_choice": true, + "supports_vision": true + }, + "vispark/vision-large": { + "cache_read_input_token_cost": 0.0, + "input_cost_per_token": 7.37e-06, + "litellm_provider": "vispark", + "max_input_tokens": 1000000, + "max_output_tokens": 65536, + "mode": "chat", + "output_cost_per_token": 2.211e-05, + "source": "https://api.lab.vispark.in/v1/models", + "supported_endpoints": [ + "/v1/chat/completions", + "/v1/completions" + ], + "supported_modalities": [ + "text", + "image", + "audio", + "video" + ], + "supported_output_modalities": [ + "text" + ], + "supports_function_calling": true, + "supports_native_streaming": true, + "supports_parallel_function_calling": true, + "supports_pdf_input": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_system_messages": true, + "supports_tool_choice": true, + "supports_vision": true + }, "vercel_ai_gateway/alibaba/qwen-3-14b": { "input_cost_per_token": 8e-08, "litellm_provider": "vercel_ai_gateway", diff --git a/provider_endpoints_support.json b/provider_endpoints_support.json index b35037ee651..5a9d300b10a 100644 --- a/provider_endpoints_support.json +++ b/provider_endpoints_support.json @@ -4332,6 +4332,32 @@ "generateContent": true } }, + "vispark": { + "display_name": "Vispark (`vispark`)", + "url": "https://lab.vispark.in/#vision", + "endpoints": { + "chat_completions": true, + "responses": true, + "text_completion": true, + "embeddings": false, + "image_generations": false, + "image_edits": false, + "audio_transcriptions": false, + "audio_speech": false, + "rerank": false, + "ocr": false, + "search": false, + "moderations": false, + "realtime": false, + "video_generations": false, + "batches": false, + "messages": true, + "interactions": true, + "systemone": false, + "a2a": true, + "generateContent": true + } + }, "vercel_ai_gateway": { "display_name": "Vercel AI Gateway (`vercel_ai_gateway`)", "url": "https://docs.litellm.ai/docs/providers/vercel_ai_gateway", diff --git a/tests/unit/llms/openai_like/test_vispark_provider.py b/tests/unit/llms/openai_like/test_vispark_provider.py new file mode 100644 index 00000000000..731e33487eb --- /dev/null +++ b/tests/unit/llms/openai_like/test_vispark_provider.py @@ -0,0 +1,234 @@ +import json +from pathlib import Path +from typing import Final + +import pytest +import respx + +import litellm + +_CHAT_COMPLETION: Final = { + "id": "chatcmpl_vispark", + "object": "chat.completion", + "created": 1_790_000_000, + "model": "vision-medium", + "choices": [ + { + "index": 0, + "message": {"role": "assistant", "content": "Hello from Vispark"}, + "finish_reason": "stop", + } + ], + "usage": {"prompt_tokens": 4, "completion_tokens": 3, "total_tokens": 7}, +} + +_TEXT_COMPLETION: Final = { + "id": "cmpl_vispark", + "object": "text_completion", + "created": 1_790_000_000, + "model": "vision-small", + "choices": [{"index": 0, "text": "Hello from Vispark", "finish_reason": "stop", "logprobs": None}], + "usage": {"prompt_tokens": 4, "completion_tokens": 3, "total_tokens": 7}, +} + + +def test_vispark_provider_resolution(monkeypatch: pytest.MonkeyPatch): + from litellm.litellm_core_utils.get_llm_provider_logic import get_llm_provider + + monkeypatch.setenv("VISPARK_LAB_API_KEY", "vispark-env-key") + monkeypatch.delenv("VISPARK_LAB_API_BASE", raising=False) + + model, provider, api_key, api_base = get_llm_provider( + model="vispark/vision-medium", + custom_llm_provider=None, + api_base=None, + api_key=None, + ) + + assert model == "vision-medium" + assert provider == "vispark" + assert api_key == "vispark-env-key" + assert api_base == "https://api.lab.vispark.in/v1" + + +def test_vispark_provider_keeps_explicit_credentials(monkeypatch: pytest.MonkeyPatch): + from litellm.litellm_core_utils.get_llm_provider_logic import get_llm_provider + + monkeypatch.setenv("VISPARK_LAB_API_KEY", "vispark-env-key") + monkeypatch.setenv("VISPARK_LAB_API_BASE", "https://vispark.env.example/v1") + + _, provider, api_key, api_base = get_llm_provider( + model="vispark/vision-medium", + custom_llm_provider=None, + api_base="https://vispark.internal.example/v1", + api_key="vispark-explicit-key", + ) + + assert provider == "vispark" + assert api_key == "vispark-explicit-key" + assert api_base == "https://vispark.internal.example/v1" + + +def test_vispark_api_base_env_overrides_default(monkeypatch: pytest.MonkeyPatch): + from litellm.litellm_core_utils.get_llm_provider_logic import get_llm_provider + + monkeypatch.setenv("VISPARK_LAB_API_KEY", "vispark-env-key") + monkeypatch.setenv("VISPARK_LAB_API_BASE", "https://vispark.env.example/v1") + + _, provider, _, api_base = get_llm_provider( + model="vispark/vision-medium", + custom_llm_provider=None, + api_base=None, + api_key=None, + ) + + assert provider == "vispark" + assert api_base == "https://vispark.env.example/v1" + + +def test_vispark_api_base_autodetects_provider(monkeypatch: pytest.MonkeyPatch): + from litellm.litellm_core_utils.get_llm_provider_logic import get_llm_provider + + monkeypatch.setenv("VISPARK_LAB_API_KEY", "vispark-env-key") + + model, provider, api_key, api_base = get_llm_provider( + model="vision-medium", + custom_llm_provider=None, + api_base="https://api.lab.vispark.in/v1", + api_key=None, + ) + + assert model == "vision-medium" + assert provider == "vispark" + assert api_key == "vispark-env-key" + assert api_base == "https://api.lab.vispark.in/v1" + + +def test_vispark_chat_completion_request_maps_max_completion_tokens(): + with respx.mock() as upstream: + route: Final = upstream.post("https://api.lab.vispark.in/v1/chat/completions").respond( + 200, json=_CHAT_COMPLETION + ) + response: Final = litellm.completion( + model="vispark/vision-medium", + messages=[{"role": "user", "content": "Say hello"}], + max_completion_tokens=64, + api_key="vispark-test-key", + ) + + request: Final = route.calls.last.request + body: Final = json.loads(request.content) + assert route.call_count == 1 + assert request.headers["authorization"] == "Bearer vispark-test-key" + assert body["model"] == "vision-medium" + assert body["max_tokens"] == 64 + assert "max_completion_tokens" not in body + assert response.choices[0].message.content == "Hello from Vispark" + + +def test_vispark_text_completion_request(): + with respx.mock() as upstream: + route: Final = upstream.post("https://api.lab.vispark.in/v1/completions").respond(200, json=_TEXT_COMPLETION) + response: Final = litellm.text_completion( + model="vispark/vision-small", + prompt="Say hello", + api_key="vispark-test-key", + ) + + body: Final = json.loads(route.calls.last.request.content) + assert route.call_count == 1 + assert body["model"] == "vision-small" + assert body["prompt"] == "Say hello" + assert response.choices[0].text == "Hello from Vispark" + + +def test_vispark_cost_follows_the_cost_map(): + cost_entry: Final = litellm.model_cost["vispark/vision-large"] + + prompt_cost, completion_cost = litellm.cost_per_token( + model="vision-large", + custom_llm_provider="vispark", + prompt_tokens=1_000, + completion_tokens=500, + ) + + assert prompt_cost == pytest.approx(1_000 * cost_entry["input_cost_per_token"]) + assert completion_cost == pytest.approx(500 * cost_entry["output_cost_per_token"]) + + +def test_vispark_models_are_listed_for_the_provider(): + from litellm.utils import get_valid_models + + expected: Final = {name for name, entry in litellm.model_cost.items() if entry.get("litellm_provider") == "vispark"} + + assert expected + assert set(litellm.models_by_provider["vispark"]) == expected + assert expected <= set(get_valid_models(custom_llm_provider="vispark")) + + +def test_vispark_endpoint_matrix_matches_between_root_and_backup(): + backup_path: Final = Path(litellm.__file__).parent / "provider_endpoints_support_backup.json" + root_path: Final = Path(litellm.__file__).parent.parent / "provider_endpoints_support.json" + + backup_entry: Final = json.loads(backup_path.read_text())["providers"]["vispark"] + root_entry: Final = json.loads(root_path.read_text())["providers"]["vispark"] + + assert backup_entry == root_entry + assert root_entry["endpoints"]["chat_completions"] is True + + +@pytest.mark.parametrize("text_completion", (False, True)) +def test_vispark_missing_key_rejects_before_openai_fallback(monkeypatch: pytest.MonkeyPatch, text_completion: bool): + monkeypatch.delenv("VISPARK_LAB_API_KEY", raising=False) + monkeypatch.setenv("OPENAI_API_KEY", "unrelated-provider-key") + with respx.mock(assert_all_called=False) as upstream: + route: Final = upstream.post(url__regex=r"https://api\.lab\.vispark\.in/v1/.*").respond(200, json=_CHAT_COMPLETION) + if text_completion: + with pytest.raises(litellm.AuthenticationError, match="VISPARK_LAB_API_KEY"): + litellm.text_completion(model="vispark/vision-small", prompt="Hello") + else: + with pytest.raises(litellm.AuthenticationError, match="VISPARK_LAB_API_KEY"): + litellm.completion(model="vispark/vision-medium", messages=[{"role": "user", "content": "Hello"}]) + assert route.call_count == 0 + + +def test_vispark_text_completion_uses_authenticated_client(monkeypatch: pytest.MonkeyPatch): + from openai import OpenAI + + monkeypatch.delenv("VISPARK_LAB_API_KEY", raising=False) + monkeypatch.setenv("OPENAI_API_KEY", "unrelated-provider-key") + with OpenAI(api_key="vispark-client-key", base_url="https://api.lab.vispark.in/v1") as client: + with respx.mock() as upstream: + route: Final = upstream.post("https://api.lab.vispark.in/v1/completions").respond(200, json=_TEXT_COMPLETION) + response: Final = litellm.text_completion(model="vispark/vision-small", prompt="Hello", client=client) + assert route.calls.last.request.headers["authorization"] == "Bearer vispark-client-key" + assert response.choices[0].text == "Hello from Vispark" + + +def test_vispark_chat_completion_uses_authenticated_client(monkeypatch: pytest.MonkeyPatch): + from openai import OpenAI + + monkeypatch.delenv("VISPARK_LAB_API_KEY", raising=False) + monkeypatch.setenv("OPENAI_API_KEY", "unrelated-provider-key") + with OpenAI(api_key="vispark-client-key", base_url="https://api.lab.vispark.in/v1") as client: + with respx.mock() as upstream: + route: Final = upstream.post("https://api.lab.vispark.in/v1/chat/completions").respond(200, json=_CHAT_COMPLETION) + response: Final = litellm.completion( + model="vispark/vision-medium", messages=[{"role": "user", "content": "Hello"}], client=client + ) + assert route.calls.last.request.headers["authorization"] == "Bearer vispark-client-key" + assert response.choices[0].message.content == "Hello from Vispark" + + +def test_vispark_http_transport_rejects_unused_sdk_credentials(monkeypatch: pytest.MonkeyPatch): + from openai import OpenAI + + monkeypatch.delenv("VISPARK_LAB_API_KEY", raising=False) + monkeypatch.setenv("OPENAI_API_KEY", "unrelated-provider-key") + monkeypatch.setenv("EXPERIMENTAL_OPENAI_BASE_LLM_HTTP_HANDLER", "true") + with OpenAI(api_key="vispark-client-key", base_url="https://api.lab.vispark.in/v1") as client: + with respx.mock(assert_all_called=False) as upstream: + route: Final = upstream.post("https://api.lab.vispark.in/v1/chat/completions").respond(200, json=_CHAT_COMPLETION) + with pytest.raises(litellm.AuthenticationError, match="VISPARK_LAB_API_KEY"): + litellm.completion(model="vispark/vision-medium", messages=[{"role": "user", "content": "Hello"}], client=client) + assert route.call_count == 0