Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,50 @@ def with_nebius(
top_p=top_p,
)

@staticmethod
def with_api_route(
*,
model: str = "claude-sonnet-4-6",
api_key: str | None = None,
base_url: str = "https://global.api-route.com/v1",
client: openai.AsyncClient | None = None,
user: NotGivenOr[str] = NOT_GIVEN,
temperature: NotGivenOr[float] = NOT_GIVEN,
parallel_tool_calls: NotGivenOr[bool] = NOT_GIVEN,
tool_choice: ToolChoice = "auto",
reasoning_effort: NotGivenOr[ReasoningEffort] = NOT_GIVEN,
safety_identifier: NotGivenOr[str] = NOT_GIVEN,
prompt_cache_key: NotGivenOr[str] = NOT_GIVEN,
top_p: NotGivenOr[float] = NOT_GIVEN,
) -> LLM:
"""Create a new instance of API Route LLM.

api_key must be set to your API Route API key, either using the argument or by
setting the API_ROUTE_API_KEY environmental variable.
"""

api_key = api_key or os.environ.get("API_ROUTE_API_KEY")
if api_key is None:
raise ValueError(
"API Route API key is required, either as argument or set API_ROUTE_API_KEY environmental variable" # noqa: E501
)

return LLM(
model=model,
api_key=api_key,
base_url=base_url,
client=client,
user=user,
temperature=temperature,
parallel_tool_calls=parallel_tool_calls,
tool_choice=tool_choice,
reasoning_effort=reasoning_effort,
safety_identifier=safety_identifier,
prompt_cache_key=prompt_cache_key,
top_p=top_p,
_strict_tool_schema=False,
)

@staticmethod
def with_letta(
*,
Expand Down
38 changes: 38 additions & 0 deletions tests/test_openai_api_route.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from __future__ import annotations

import pytest

from livekit.plugins import openai

pytestmark = pytest.mark.unit


async def test_api_route_defaults(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("API_ROUTE_API_KEY", "test-key")

llm = openai.LLM.with_api_route()
try:
assert llm.model == "claude-sonnet-4-6"
assert llm.provider == "global.api-route.com"
finally:
await llm.aclose()


async def test_api_route_explicit_configuration() -> None:
llm = openai.LLM.with_api_route(
model="deepseek-v4-pro",
api_key="explicit-key",
base_url="https://example.com/v1",
)
try:
assert llm.model == "deepseek-v4-pro"
assert llm.provider == "example.com"
finally:
await llm.aclose()


def test_api_route_requires_api_key(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv("API_ROUTE_API_KEY", raising=False)

with pytest.raises(ValueError, match="API Route API key is required"):
openai.LLM.with_api_route()
Loading