diff --git a/.github/scripts/smoke_test_native_wheel.py b/.github/scripts/smoke_test_native_wheel.py index 577bb32fcf0..af4054ea92e 100644 --- a/.github/scripts/smoke_test_native_wheel.py +++ b/.github/scripts/smoke_test_native_wheel.py @@ -7,6 +7,8 @@ import zipfile from pathlib import Path from typing import Final +COST_MAP: Final = Path(__file__).resolve().parents[2] / "model_prices_and_context_window.json" + CHILD_SCRIPT: Final = """ from importlib.util import module_from_spec, spec_from_file_location from pathlib import Path @@ -34,6 +36,9 @@ else: after = module.gil_stats() if not isinstance(after.get("releases"), int): raise AssertionError(f"native module unusable after panic: {after!r}") + +if module.bundled_model_cost_map() != Path(sys.argv[2]).read_bytes(): + raise AssertionError("the wheel embeds a different model_prices_and_context_window.json than this checkout") """ @@ -55,7 +60,9 @@ def main() -> int: native_path: Final = Path(temporary_directory) / Path(native_members[0].filename).name native_path.write_bytes(archive.read(native_members[0])) - result: Final = subprocess.run((sys.executable, "-c", CHILD_SCRIPT, str(native_path)), check=False) + result: Final = subprocess.run( + (sys.executable, "-c", CHILD_SCRIPT, str(native_path), str(COST_MAP)), check=False + ) if result.returncode != 0: sys.stderr.write(f"native wheel smoke test exited with status {result.returncode}\n") diff --git a/.github/workflows/cost-map-guard.yml b/.github/workflows/cost-map-guard.yml index 6519b65e71b..fb86237e504 100644 --- a/.github/workflows/cost-map-guard.yml +++ b/.github/workflows/cost-map-guard.yml @@ -46,3 +46,24 @@ jobs: HEAD_REF: ${{ github.event.pull_request.head.ref }} run: | uv run --no-project python ci_cd/cost_map_guard.py --base "$MERGE_BASE" --head "$HEAD_SHA" --head-ref "$HEAD_REF" + - name: Check whether the pull request changes the cost map + id: changed + env: + MERGE_BASE: ${{ steps.revisions.outputs.merge_base }} + HEAD_SHA: ${{ github.event.pull_request.head.sha }} + run: | + if git diff --quiet "$MERGE_BASE" "$HEAD_SHA" -- model_prices_and_context_window.json; then + echo "cost_map=false" >> "$GITHUB_OUTPUT" + else + echo "cost_map=true" >> "$GITHUB_OUTPUT" + fi + - if: steps.changed.outputs.cost_map == 'true' + run: rustup toolchain install --no-self-update + - name: Load the cost map through model-catalog + if: steps.changed.outputs.cost_map == 'true' + env: + HEAD_SHA: ${{ github.event.pull_request.head.sha }} + run: | + git show "${HEAD_SHA}:model_prices_and_context_window.json" > "$RUNNER_TEMP/model_prices_and_context_window.json" + cargo run --locked --quiet --manifest-path litellm-rust/Cargo.toml -p litellm-model-catalog --bin check-registry \ + -- "$RUNNER_TEMP/model_prices_and_context_window.json" diff --git a/.github/workflows/test-linting.yml b/.github/workflows/test-linting.yml index 89ca60ad9d1..bd7343ea222 100644 --- a/.github/workflows/test-linting.yml +++ b/.github/workflows/test-linting.yml @@ -376,10 +376,10 @@ jobs: exit 0 fi files="$(git diff --name-only "$base" HEAD)" - if grep -Eq '^(litellm/(proxy|types)/|ui/litellm-dashboard/(src/lib/http/schema\.d\.ts|scripts/gen-api-types\.mjs|package(-lock)?\.json)$|\.github/workflows/test-linting\.yml$)' <<< "$files"; then + if grep -Eq '^(litellm/|enterprise/|(pyproject\.toml|uv\.lock)$|ui/litellm-dashboard/(src/lib/http/schema\.d\.ts|scripts/gen-api-types\.mjs|package(-lock)?\.json)$|\.github/workflows/test-linting\.yml$)' <<< "$files"; then echo "relevant=true" >> "$GITHUB_OUTPUT" else - echo "No proxy, types or generator changes in this pull request, nothing to verify." + echo "No litellm, enterprise, dependency or generator changes in this pull request, nothing to verify." echo "relevant=false" >> "$GITHUB_OUTPUT" fi diff --git a/.github/workflows/test-terraform-provider.yml b/.github/workflows/test-terraform-provider.yml index ff9db13bd25..6aeb66ed37c 100644 --- a/.github/workflows/test-terraform-provider.yml +++ b/.github/workflows/test-terraform-provider.yml @@ -12,6 +12,7 @@ on: paths: - "terraform/provider/**" - "litellm/proxy/**" + - "enterprise/**" - ".github/workflows/test-terraform-provider.yml" permissions: diff --git a/.github/workflows/test-unit.yml b/.github/workflows/test-unit.yml index e7865817048..509271cbf54 100644 --- a/.github/workflows/test-unit.yml +++ b/.github/workflows/test-unit.yml @@ -322,6 +322,7 @@ jobs: tests/unit/llms/openai_like tests/unit/llms/opencode tests/unit/llms/opensandbox + tests/unit/llms/opensearch tests/unit/llms/openrouter tests/unit/llms/ovhcloud tests/unit/llms/parallel_ai diff --git a/backend/Dockerfile b/backend/Dockerfile index 1c22f914100..aad59ba17c6 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -24,6 +24,7 @@ ENV UV_PYTHON_DOWNLOADS=0 COPY pyproject.toml README.md rust-toolchain.toml ./ COPY .cargo ./.cargo COPY litellm-rust ./litellm-rust +COPY model_prices_and_context_window.json ./ RUN --mount=type=cache,target=/root/.cache/uv \ mkdir -p litellm/rust_bridge /wheels && \ diff --git a/codecov.yaml b/codecov.yaml index 5b30e78a8fb..521088868bd 100644 --- a/codecov.yaml +++ b/codecov.yaml @@ -15,9 +15,10 @@ ignore: flag_management: default_rules: carryforward: true - # Dead flags no CI job uploads anymore: their carried-forward sessions were - # measured against old revisions, and the stale line maps mark comment lines - # of since-edited files as missed, sinking patch coverage on unrelated PRs. + # Dead flags no CI job uploads anymore, and flags whose job is path-gated on + # PRs: their carried-forward sessions were measured against old revisions, and + # the stale line maps mark comment lines of since-edited files as missed, + # sinking patch coverage on unrelated PRs. individual_flags: - name: proxy-mgmt-behavior carryforward: false @@ -99,6 +100,10 @@ flag_management: carryforward: false - name: proxy-db-endpoints-and-responses carryforward: false + - name: roi-postgres + carryforward: false + - name: redis-compat + carryforward: false component_management: individual_components: diff --git a/gateway/Dockerfile b/gateway/Dockerfile index 6dc8bab45ce..9d228627543 100644 --- a/gateway/Dockerfile +++ b/gateway/Dockerfile @@ -40,6 +40,7 @@ ENV UV_PYTHON_DOWNLOADS=0 COPY pyproject.toml README.md rust-toolchain.toml ./ COPY .cargo ./.cargo COPY litellm-rust ./litellm-rust +COPY model_prices_and_context_window.json ./ RUN --mount=type=cache,target=/root/.cache/uv \ mkdir -p litellm/rust_bridge /wheels && \ diff --git a/helm/litellm-helm/README.md b/helm/litellm-helm/README.md index e059d45e037..a3488d6114c 100644 --- a/helm/litellm-helm/README.md +++ b/helm/litellm-helm/README.md @@ -259,3 +259,9 @@ itself. `lensWorker.mode` selects `disabled`, `bundled` or `external`. Bundled mode generates separate service and identity-signing credentials. External mode requires `lensWorker.gateway.secretName` and `lensWorker.serviceTokenSecret.name`; their keys default to `gateway-secret` and `service-token`. Provision distinct values matching the external Lens deployment Follow the [Lens Helm connection guide](https://github.com/BerriAI/lens/blob/main/helm/lens/README.md#connect-a-gateway) for complete values, verification, GitOps credential requirements and existing-data migration. Source-chart installation requires a built Lens image until the first signed Lens release is published + +## Shutdown accounting + +`GRACEFUL_SHUTDOWN_TIMEOUT` defaults to 30 seconds. Pending success callbacks share the time remaining since SIGTERM or the first `/health/drain` call. Callback flushing reserves the existing 10-second teardown headroom for cancellation, tracked accounting tasks and persistence. Exhausting this budget can discard callbacks, which is logged + +Set `terminationGracePeriodSeconds` to at least `GRACEFUL_SHUTDOWN_TIMEOUT + 10`, allowing additional time for preStop hooks and database writes. This is not a hard shutdown bound or a guarantee of persistence. Database retries retain their own limits, and cancellation-resistant callbacks can remain alive. The existing preStop hook can also be followed by Uvicorn's separate connection drain, as described in `values.yaml` diff --git a/litellm-proxy-extras/litellm_proxy_extras/migration_data_checks.py b/litellm-proxy-extras/litellm_proxy_extras/migration_data_checks.py index e22639daaef..75c5bcf4e1e 100644 --- a/litellm-proxy-extras/litellm_proxy_extras/migration_data_checks.py +++ b/litellm-proxy-extras/litellm_proxy_extras/migration_data_checks.py @@ -14,11 +14,9 @@ either, so the migration keeps today's behavior for it. """ import os -import re -from collections.abc import Callable, Iterator +from collections.abc import Callable from dataclasses import dataclass from functools import reduce -from itertools import accumulate from pathlib import Path from types import MappingProxyType from typing import TYPE_CHECKING, Final, Literal @@ -43,7 +41,7 @@ MIGRATION_DATA_CHECK_TIMEOUT_ENV_VAR: Final = "LITELLM_MIGRATION_DATA_CHECK_TIME DEFAULT_MIGRATION_DATA_CHECK_TIMEOUT_SECONDS: Final = 600.0 REFUSAL: Final = "Existing rows would make a pending schema change fail:" -Kind = Literal["too_long", "duplicates", "nulls", "any_row", "orphans", "check_violations"] +Kind = Literal["too_long"] @dataclass(frozen=True, slots=True) @@ -54,9 +52,6 @@ class Check: statement: str source: str index: str | None = None - referenced_table: str | None = None - referenced_columns: tuple[str, ...] = () - expression: str | None = None @dataclass(frozen=True, slots=True) @@ -100,33 +95,6 @@ class ColumnType: ColumnTypeLookup = Callable[[str, str], ColumnType | None] -_IDENT: Final = r'"?(\w+)"?' -_QUALIFIED: Final = r'(?:"?\w+"?\s*\.\s*)?"?(\w+)"?' -_CREATE_INDEX: Final = re.compile( - rf"^\s*CREATE\s+(UNIQUE\s+)?INDEX\s+(?:CONCURRENTLY\s+)?(?:IF\s+NOT\s+EXISTS\s+)?{_IDENT}\s+ON\s+(?:ONLY\s+)?" - rf"{_QUALIFIED}\s*(?:USING\s+(\w+)\s*)?\((.*?)\)", - re.IGNORECASE | re.DOTALL, -) -_ALTER_TABLE: Final = re.compile( - rf"^\s*ALTER\s+TABLE\s+(?:IF\s+EXISTS\s+)?(?:ONLY\s+)?{_QUALIFIED}\s+(.*)$", re.IGNORECASE | re.DOTALL -) -_SET_NOT_NULL: Final = re.compile(rf"^ALTER\s+(?:COLUMN\s+)?{_IDENT}\s+SET\s+NOT\s+NULL$", re.IGNORECASE) -_ADD_COLUMN: Final = re.compile( - rf"^ADD\s+(?:COLUMN\s+)?(?:IF\s+NOT\s+EXISTS\s+)?(?!(?:CONSTRAINT|FOREIGN|UNIQUE|CHECK|PRIMARY|EXCLUDE)\b){_IDENT}\s+(.*)$", - re.IGNORECASE | re.DOTALL, -) -_CONSTRAINT: Final = rf"^ADD\s+(?:CONSTRAINT\s+{_IDENT}\s+)?" -_ADD_FOREIGN_KEY: Final = re.compile( - rf"{_CONSTRAINT}FOREIGN\s+KEY\s*\(([^)]*)\)\s*REFERENCES\s+{_QUALIFIED}\s*\(([^)]*)\)(.*)$", - re.IGNORECASE | re.DOTALL, -) -_ADD_UNIQUE: Final = re.compile(rf"{_CONSTRAINT}UNIQUE\s*\(([^)]*)\)", re.IGNORECASE | re.DOTALL) -_ADD_CHECK: Final = re.compile(rf"{_CONSTRAINT}CHECK\s*\((.*)\)\s*(NOT\s+VALID)?\s*$", re.IGNORECASE | re.DOTALL) -_PLAIN_COLUMN: Final = re.compile(rf"^{_IDENT}(?:\s+(?:ASC|DESC|NULLS\s+(?:FIRST|LAST)))*$", re.IGNORECASE) -_COMMENT: Final = re.compile(r"--[^\n]*") -_NOT_NULL: Final = re.compile(r"\bNOT\s+NULL\b", re.IGNORECASE) -_HAS_DEFAULT: Final = re.compile(r"\bDEFAULT\b|\bGENERATED\b", re.IGNORECASE) - def migration_data_check_timeout_seconds() -> float: """Seconds one count query may run before the preflight gives up on verifying it.""" @@ -145,117 +113,6 @@ def migration_data_check_timeout_seconds() -> float: return DEFAULT_MIGRATION_DATA_CHECK_TIMEOUT_SECONDS -def statements(migration_sql: str) -> tuple[str, ...]: - """The statements of one migration file, comments stripped, empty ones dropped.""" - parts: Final = _COMMENT.sub("", migration_sql).split(";") - return tuple(part.strip() for part in parts if part.strip()) - - -def _split_top_level(text: str) -> tuple[str, ...]: - """Split on the commas outside parentheses.""" - depth: Final = accumulate((1 if char == "(" else -1 if char == ")" else 0) for char in text) - cuts: Final = tuple( - position for position, (char, level) in enumerate(zip(text, depth, strict=True)) if char == "," and level == 0 - ) - starts: Final = (0, *(cut + 1 for cut in cuts)) - stops: Final = (*cuts, len(text)) - pieces: Final = tuple(text[start:stop].strip() for start, stop in zip(starts, stops, strict=True)) - return tuple(piece for piece in pieces if piece) - - -def _plain_columns(column_list: str) -> tuple[str, ...] | None: - """The column names of an index or constraint column list, None when one entry is an - expression the derivation cannot turn into a count.""" - matches: Final = tuple(_PLAIN_COLUMN.match(entry) for entry in _split_top_level(column_list)) - if any(match is None for match in matches): - return None - return tuple(match.group(1) for match in matches if match is not None) - - -def _index_checks(match: "re.Match[str]", statement_text: str, source: str) -> Iterator[Check]: - unique, name, table, method, column_list = match.groups() - columns: Final = _plain_columns(column_list) - if columns is None: - return - label: Final = f'CREATE {"UNIQUE " if unique else ""}INDEX "{name}"' - if method is None or method.lower() == "btree": - yield Check("too_long", table, columns, label, source, index=name) - if unique: - yield Check("duplicates", table, columns, label, source, index=name) - - -def _alter_table_checks(table: str, action: str, source: str) -> Iterator[Check]: - if (set_not_null := _SET_NOT_NULL.match(action)) is not None: - yield Check("nulls", table, (set_not_null.group(1),), f'ALTER TABLE "{table}" {action}', source) - return - if (add_column := _ADD_COLUMN.match(action)) is not None: - column, body = add_column.groups() - if _NOT_NULL.search(body) is not None and _HAS_DEFAULT.search(body) is None: - yield Check( - "any_row", table, (column,), f'ALTER TABLE "{table}" ADD COLUMN "{column}" ... NOT NULL', source - ) - return - if (foreign_key := _ADD_FOREIGN_KEY.match(action)) is not None: - constraint, columns, referenced_table, referenced_columns, tail = foreign_key.groups() - own: Final = _plain_columns(columns) - theirs: Final = _plain_columns(referenced_columns) - if own is None or theirs is None or len(own) != len(theirs) or "NOT VALID" in tail.upper(): - return - yield Check( - "orphans", - table, - own, - _constraint_label(table, constraint, "FOREIGN KEY"), - source, - referenced_table=referenced_table, - referenced_columns=theirs, - ) - return - if (unique := _ADD_UNIQUE.match(action)) is not None: - constraint, columns = unique.groups() - unique_columns: Final = _plain_columns(columns) - if unique_columns is not None: - yield Check("duplicates", table, unique_columns, _constraint_label(table, constraint, "UNIQUE"), source) - return - if (check := _ADD_CHECK.match(action)) is not None: - constraint, expression, not_valid = check.groups() - if not_valid is None: - yield Check( - "check_violations", - table, - (), - _constraint_label(table, constraint, "CHECK"), - source, - expression=expression, - ) - - -def _constraint_label(table: str, constraint: str | None, kind: str) -> str: - named: Final = f'CONSTRAINT "{constraint}" ' if constraint else "" - return f'ALTER TABLE "{table}" ADD {named}{kind}' - - -def checks_for_statement(statement_text: str, source: str) -> tuple[Check, ...]: - """The count queries one SQL statement needs before it can be applied, empty for a - statement that cannot fail on existing rows or that the derivation cannot read.""" - if (index := _CREATE_INDEX.match(statement_text)) is not None: - return tuple(_index_checks(index, statement_text, source)) - if (alter := _ALTER_TABLE.match(statement_text)) is not None: - table, actions = alter.groups() - return tuple( - check for action in _split_top_level(actions) for check in _alter_table_checks(table, action, source) - ) # comprehension-ok: flattening the per-action checks of one statement - return () - - -def checks_for_migration(name: str, migration_sql: str) -> tuple[Check, ...]: - return tuple( - check - for statement_text in statements(migration_sql) - for check in checks_for_statement(statement_text, f"migration {name}") - ) # comprehension-ok: flattening the per-statement checks of one migration - - def checks_for_index(table: str, name: str, columns: tuple[str, ...]) -> tuple[Check, ...]: """The entry-size check a request-log index needs before `CREATE INDEX CONCURRENTLY`.""" return (Check("too_long", table, columns, f'CREATE INDEX "{name}"', "request-log index", index=name),) @@ -424,42 +281,7 @@ def _entry_size_query(check: Check, schema: str, types: tuple[ColumnType, ...], def _count_query(check: Check, schema: str, types: tuple[ColumnType, ...], limit_bytes: int) -> "sql.Composed": - from psycopg import sql - - table: Final = sql.Identifier(schema, check.table) - columns: Final = sql.SQL(", ").join(sql.Identifier(column) for column in check.columns) - match check.kind: - case "too_long": - return _entry_size_query(check, schema, types, limit_bytes) - case "duplicates": - not_null: Final = sql.SQL(" AND ").join( - sql.SQL("{} IS NOT NULL").format(sql.Identifier(column)) for column in check.columns - ) - return sql.SQL( - "SELECT count(*) FROM (SELECT 1 FROM {t} WHERE {nn} GROUP BY {cols} HAVING count(*) > 1) AS d" - ).format(t=table, nn=not_null, cols=columns) - case "nulls": - return sql.SQL("SELECT count(*) FROM {t} WHERE {col} IS NULL").format( - t=table, col=sql.Identifier(check.columns[0]) - ) - case "any_row": - return sql.SQL("SELECT count(*) FROM {t}").format(t=table) - case "orphans": - pairs: Final = sql.SQL(" AND ").join( - sql.SQL("parent.{} = child.{}").format(sql.Identifier(theirs), sql.Identifier(own)) - for own, theirs in zip(check.columns, check.referenced_columns, strict=True) - ) - present: Final = sql.SQL(" AND ").join( - sql.SQL("child.{} IS NOT NULL").format(sql.Identifier(column)) for column in check.columns - ) - return sql.SQL( - "SELECT count(*) FROM {t} AS child WHERE {present} AND NOT EXISTS (SELECT 1 FROM {ref} AS parent WHERE {pairs})" - ).format(t=table, present=present, ref=sql.Identifier(schema, check.referenced_table or ""), pairs=pairs) - case "check_violations": - return sql.SQL("SELECT count(*) FROM {t} WHERE NOT ({expr})").format( - t=table, - expr=sql.SQL(check.expression or "true"), # pyright: ignore[reportArgumentType] # copied from our own migration file - ) + return _entry_size_query(check, schema, types, limit_bytes) def _applies( @@ -474,15 +296,7 @@ def _applies( return False if check.index is not None and _index_is_valid(connection, schema, check.index): return False - if check.kind == "any_row": - return types is None - if types is None: - return False - if check.kind == "too_long": - return any(column.can_overflow_btree for column in types) - if check.kind == "orphans": - return check.referenced_table is not None and _table_exists(connection, schema, check.referenced_table) - return True + return types is not None and any(column.can_overflow_btree for column in types) def _too_long_detail(check: Check, types: tuple[ColumnType, ...], values: tuple[object, ...], limit_bytes: int) -> str: @@ -515,21 +329,7 @@ def _run_check( rows: Final = _int(values[0], 0) if values else 0 if rows == 0: return None - match check.kind: - case "too_long": - return BlockedChange(check, rows, _too_long_detail(check, types, values, limit_bytes)) - case "duplicates": - return BlockedChange(check, rows, "duplicated value groups") - case "nulls": - return BlockedChange(check, rows, "NULL") - case "any_row": - return BlockedChange( - check, rows, "rows, and a NOT NULL column with no DEFAULT cannot be added to a table with rows" - ) - case "orphans": - return BlockedChange(check, rows, f'with no matching row in "{check.referenced_table}"') - case "check_violations": - return BlockedChange(check, rows, f"failing CHECK ({check.expression})") + return BlockedChange(check, rows, _too_long_detail(check, types, values, limit_bytes)) def find_blockers( @@ -571,19 +371,7 @@ def find_blockers( def _remedy(check: Check) -> str: joined: Final = ", ".join(check.columns) - match check.kind: - case "too_long": - return f'Shorten or hash the long "{check.table}" ({joined}) values so every row\'s index entry fits.' - case "duplicates": - return f'Remove or merge the duplicate "{check.table}" ({joined}) values.' - case "nulls": - return f'Fill the NULL "{check.table}"."{joined}" values, or give the column a default first.' - case "any_row": - return "Give the new column a DEFAULT in the migration, or add it nullable and fill it afterwards." - case "orphans": - return f'Delete or repoint the "{check.table}" ({joined}) values with no "{check.referenced_table}" row.' - case "check_violations": - return f'Fix the "{check.table}" rows that fail the expression before adding the constraint.' + return f'Shorten or hash the long "{check.table}" ({joined}) values so every row\'s index entry fits.' def _describe(finding: Finding) -> str: @@ -607,30 +395,4 @@ def _describe(finding: Finding) -> str: def report(findings: tuple[Finding, ...]) -> str: """The operator-facing refusal: one block per finding, then what to do next.""" lines: Final = "\n".join(_describe(finding) for finding in findings) - return ( - f"{REFUSAL}\n{lines}\n" - "No schema change was made. Fix the rows and start again, or run `litellm --check_migrations_against_existing_data` with the new image to check again." - ) - - -def pending_change_checks( - connection: "psycopg.Connection[tuple[object, ...]]", - schema: str, - migrations_dir: Path | None, - indexes: tuple[tuple[str, str, tuple[str, ...]], ...], -) -> tuple[Check, ...]: - """Every check the pending migrations and the request-log indexes need. `migrations_dir` - is None when the schema is pushed rather than migrated, so only the indexes are checked.""" - migration_checks: Final = ( - tuple( - check - for name, migration_sql in pending_migrations(connection, schema, migrations_dir) - for check in checks_for_migration(name, migration_sql) - ) # comprehension-ok: flattening the per-migration checks - if migrations_dir is not None - else () - ) - index_checks: Final = tuple( - check for table, name, columns in indexes for check in checks_for_index(table, name, columns) - ) # comprehension-ok: flattening the per-index checks - return migration_checks + index_checks + return f"{REFUSAL}\n{lines}\nNo index was built. Fix the rows and the next index build picks it up." diff --git a/litellm-proxy-extras/litellm_proxy_extras/migration_dry_run.py b/litellm-proxy-extras/litellm_proxy_extras/migration_dry_run.py new file mode 100644 index 00000000000..c24742ce145 --- /dev/null +++ b/litellm-proxy-extras/litellm_proxy_extras/migration_dry_run.py @@ -0,0 +1,184 @@ +"""`litellm --check_migrations_against_existing_data`: run the pending migrations for real, each +inside its own transaction that is always rolled back, so Postgres itself says whether this +database's rows and schema take them. Each migration holds the locks the real upgrade will, +and releases them before the next one runs.""" + +from __future__ import annotations + +import re +import time +from collections.abc import Callable +from dataclasses import dataclass +from functools import reduce +from pathlib import Path +from typing import TYPE_CHECKING, Final + +from litellm_proxy_extras.migration_data_checks import pending_migrations + +if TYPE_CHECKING: + import psycopg + +_CONCURRENTLY: Final = re.compile( + r"\b((?:CREATE\s+(?:UNIQUE\s+)?INDEX|DROP\s+INDEX|REINDEX\s+\w+)\s+)CONCURRENTLY\s+", re.IGNORECASE +) +UNDEFINED_SQLSTATES: Final = frozenset({"42703", "42P01", "42704", "42883"}) + +Migration = tuple[str, str] + + +@dataclass(frozen=True, slots=True) +class Applied: + name: str + seconds: float + replayed: tuple[str, ...] = () + + +@dataclass(frozen=True, slots=True) +class Rejected: + name: str + error: str + seconds: float + + +@dataclass(frozen=True, slots=True) +class Baseline: + """A database with tables but no `_prisma_migrations`: the boot baselines it instead of replaying + history, so the dry run asks the same question the boot does. `refusal` is None when the boot + will baseline, otherwise its reason (a custom migration folder, or the schema diff).""" + + refusal: str | None + + @property + def matches(self) -> bool: + return self.refusal is None + + +@dataclass(frozen=True, slots=True) +class DryRun: + applied: tuple[Applied, ...] + rejected: Rejected | None + baseline: Baseline | None = None + + @property + def ok(self) -> bool: + return self.rejected is None and (self.baseline is None or self.baseline.matches) + + +def transactional(migration_sql: str) -> str: + """The migration as it can run inside a transaction: `CONCURRENTLY` index builds become plain ones.""" + return _CONCURRENTLY.sub(r"\1", migration_sql) + + +def _attempt( + connection: psycopg.Connection[tuple[object, ...]], + schema: str, + chain: tuple[Migration, ...], + clock: Callable[[], float], +) -> tuple[float, psycopg.Error | None]: + import psycopg + from psycopg import sql + + started: Final = clock() + try: + with connection.transaction(force_rollback=True): + connection.execute(sql.SQL("SET LOCAL search_path TO {}").format(sql.Identifier(schema))) + for _, migration_sql in chain: + connection.execute(transactional(migration_sql)) + except psycopg.Error as exc: + return clock() - started, exc + return clock() - started, None + + +def _run( + connection: psycopg.Connection[tuple[object, ...]], + schema: str, + pending: tuple[Migration, ...], + position: int, + done: DryRun, + clock: Callable[[], float], +) -> DryRun: + """One migration in its own rolled-back transaction. A migration that fails because something + an earlier pending migration creates does not exist yet is rerun with those replayed in front.""" + if done.rejected is not None: + return done + name: Final = pending[position][0] + seconds, error = _attempt(connection, schema, (pending[position],), clock) + if error is None: + return DryRun(done.applied + (Applied(name, seconds),), None, done.baseline) + if error.sqlstate not in UNDEFINED_SQLSTATES or position == 0: + return DryRun(done.applied, Rejected(name, str(error).strip(), seconds), done.baseline) + earlier: Final = pending[:position] + chained_seconds, chained_error = _attempt(connection, schema, earlier + (pending[position],), clock) + if chained_error is None: + replayed: Final = tuple(earlier_name for earlier_name, _ in earlier) + return DryRun(done.applied + (Applied(name, chained_seconds, replayed),), None, done.baseline) + return DryRun(done.applied, Rejected(name, str(chained_error).strip(), chained_seconds), done.baseline) + + +def _has_ledger(connection: psycopg.Connection[tuple[object, ...]], schema: str) -> bool: + from psycopg import sql + + row: Final = connection.execute( + "SELECT to_regclass(%s)", (sql.Identifier(schema, "_prisma_migrations").as_string(connection),) + ).fetchone() + return row is not None and row[0] is not None + + +def _has_litellm_tables(connection: psycopg.Connection[tuple[object, ...]], schema: str) -> bool: + row: Final = connection.execute( + "SELECT 1 FROM information_schema.tables WHERE table_schema = %s AND table_name LIKE 'LiteLLM\\_%%' LIMIT 1", + (schema,), + ).fetchone() + return row is not None + + +def dry_run( + connection: psycopg.Connection[tuple[object, ...]], + schema: str, + migrations_dir: Path, + baseline_check: Callable[[], str | None] | None = None, + clock: Callable[[], float] = time.monotonic, +) -> DryRun: + """Apply every pending migration in order, each in its own transaction that is rolled back + before the next one starts. The first migration Postgres rejects ends the run with its error. + `baseline_check` (None when the boot would baseline, otherwise why it refuses) stands in for + the replay on a database that has tables but no migration history, which the boot baselines.""" + if baseline_check is not None and not _has_ledger(connection, schema) and _has_litellm_tables(connection, schema): + return DryRun((), None, Baseline(baseline_check())) + pending: Final = pending_migrations(connection, schema, migrations_dir) + return reduce( + lambda done, position: _run(connection, schema, pending, position, done, clock), + range(len(pending)), + DryRun((), None), + ) + + +def _timing(applied: Applied) -> str: + replay: Final = f" (depends on {', '.join(applied.replayed)}, replayed in front of it)" if applied.replayed else "" + return f"\n {applied.name}: {applied.seconds:.1f} s{replay}" + + +def report(result: DryRun) -> str: + """What the operator reads: the per-migration time the real upgrade will hold its locks, + or the migration Postgres rejected with Postgres's own words. Nothing was applied either way.""" + if result.baseline is not None: + if result.baseline.matches: + return ( + "No migration history and the schema matches this build, " + "the proxy baselines it at boot, nothing to dry-run." + ) + return f"No migration history and the proxy cannot baseline it at boot: {result.baseline.refusal}" + timings: Final = "".join(_timing(applied) for applied in result.applied) + if result.rejected is None: + count: Final = len(result.applied) + if count == 0: + return "No pending migrations, this database is on this version's schema." + return ( + f"{count} pending migration(s) applied one at a time in a dry run, each rolled back before the next " + f"so its locks are released, nothing changed:{timings}" + ) + return ( + f"Migration {result.rejected.name} fails on this database after {result.rejected.seconds:.1f} s, " + f"nothing was applied.\n{result.rejected.error}" + + (f"\nApplied before it in the dry run:{timings}" if timings else "") + ) diff --git a/litellm-proxy-extras/litellm_proxy_extras/migration_recovery.py b/litellm-proxy-extras/litellm_proxy_extras/migration_recovery.py index 5a55b35b255..1c16eb5e6b7 100644 --- a/litellm-proxy-extras/litellm_proxy_extras/migration_recovery.py +++ b/litellm-proxy-extras/litellm_proxy_extras/migration_recovery.py @@ -86,6 +86,18 @@ def migration_files(directory: Path) -> tuple[tuple[str, str], ...]: ) +def has_packaged_history(migrations_dir: Path) -> bool: + """Whether `migrations_dir` carries exactly the migration files and schema.prisma this package ships, + the only history the boot baselines an existing database onto.""" + packaged_dir: Final = Path(__file__).parent + migrations: Final = migration_files(migrations_dir) + return ( + bool(migrations) + and migrations == migration_files(packaged_dir) + and (migrations_dir / "schema.prisma").read_bytes() == (packaged_dir / "schema.prisma").read_bytes() + ) + + def baseline_current_schema( coordinator: MigrationCoordinator, schema: str, @@ -95,14 +107,9 @@ def baseline_current_schema( ) -> None: from psycopg import sql - packaged_dir: Final = Path(__file__).parent - migrations: Final = migration_files(migrations_dir) - if ( - not migrations - or migrations != migration_files(packaged_dir) - or (migrations_dir / "schema.prisma").read_bytes() != (packaged_dir / "schema.prisma").read_bytes() - ): + if not has_packaged_history(migrations_dir): raise RuntimeError("Cannot automatically baseline an existing database with custom migration history.") + migrations: Final = migration_files(migrations_dir) coordinator.acquire_prisma_lock() existing: Final = coordinator.connection.execute( diff --git a/litellm-proxy-extras/litellm_proxy_extras/utils.py b/litellm-proxy-extras/litellm_proxy_extras/utils.py index 528bf7def20..8b19576d86e 100644 --- a/litellm-proxy-extras/litellm_proxy_extras/utils.py +++ b/litellm-proxy-extras/litellm_proxy_extras/utils.py @@ -11,18 +11,13 @@ from collections.abc import Callable from contextlib import AbstractContextManager, nullcontext from dataclasses import dataclass, replace from pathlib import Path -from typing import TYPE_CHECKING, Final, Optional, Protocol, Union +from typing import TYPE_CHECKING, Final, Protocol from litellm_proxy_extras import prisma_toolchain from litellm_proxy_extras._logging import logger from litellm_proxy_extras._redaction import _redact_credentials -from litellm_proxy_extras.migration_data_checks import ( - Finding, - find_blockers, - migration_data_check_timeout_seconds, - pending_change_checks, - report, -) +from litellm_proxy_extras.migration_dry_run import dry_run +from litellm_proxy_extras.migration_dry_run import report as dry_run_report from litellm_proxy_extras.migration_lock import held_migration_lock from litellm_proxy_extras.prisma_toolchain import ( PRISMA_COMMAND_TIMEOUT_ENV_VAR, @@ -36,7 +31,6 @@ from litellm_proxy_extras.replica_identity import ( apply_replica_identity_full, ) from litellm_proxy_extras.request_log_indexes import ( - REQUEST_LOG_INDEXES, ensure_request_log_indexes, filter_request_log_index_diff, ) @@ -52,7 +46,7 @@ class LensCheckConnect(Protocol): ) -> "psycopg.Connection[tuple[object, ...]]": ... -def str_to_bool(value: Optional[str]) -> bool: +def str_to_bool(value: str | None) -> bool: if value is None: return False return value.lower() in ("true", "1", "t", "y", "yes") @@ -169,7 +163,7 @@ def _without_sql_comments(statement: str) -> str: ).strip() -def _without_spend_logs_pk_clauses(statement: str) -> Optional[str]: +def _without_spend_logs_pk_clauses(statement: str) -> str | None: prefix_match = _SPEND_LOGS_ALTER_RE.match(statement) if not prefix_match: return statement @@ -217,7 +211,7 @@ def _max_migration_timestamp(names) -> int: return max(_migration_timestamp(n) for n in names) -def _redacted_command(command: object) -> Union[str, tuple[str, ...], list[str]]: +def _redacted_command(command: object) -> str | tuple[str, ...] | list[str]: if isinstance(command, tuple): return tuple(_redact_credentials(str(argument)) for argument in command) if isinstance(command, list): @@ -404,7 +398,7 @@ class ProxyExtrasDBManager: return tuple(row) if row is not None else () @staticmethod - def _failed_migration_logs(migration_name: str, started_at: str) -> Optional[str]: + def _failed_migration_logs(migration_name: str, started_at: str) -> str | None: row: Final = ProxyExtrasDBManager._read_migration_ledger( "SELECT logs FROM {} WHERE migration_name = %s AND started_at = %s::timestamptz " "AND finished_at IS NULL AND rolled_back_at IS NULL", @@ -721,10 +715,10 @@ class ProxyExtrasDBManager: return row is not None @staticmethod - def _prisma_schema_param(url: str) -> Optional[str]: + def _prisma_schema_param(url: str) -> str | None: """The `schema` query param Prisma uses to pick its target schema, or None when the URL does not set one.""" - from urllib.parse import urlparse, parse_qsl + from urllib.parse import parse_qsl, urlparse return next( (v for k, v in parse_qsl(urlparse(url).query) if k == "schema"), @@ -1424,7 +1418,6 @@ class ProxyExtrasDBManager: bool: True if setup was successful, False otherwise """ ensure_prisma_toolchain(prisma_command=_get_prisma_command(), prisma_env=_get_prisma_env()) - ProxyExtrasDBManager.raise_if_rows_block_pending_changes(use_migrate=use_migrate) migrated = ProxyExtrasDBManager._run_migrations(use_migrate=use_migrate, use_v2_resolver=use_v2_resolver) if not migrated: return False @@ -1433,46 +1426,69 @@ class ProxyExtrasDBManager: return True @staticmethod - def migration_data_check_findings(use_migrate: bool = True) -> tuple[Finding, ...]: - """What in the existing rows would make a pending migration or request-log index - fail, read with count queries only: no write, no table lock. Empty when nothing - blocks, when no database is configured, or when it cannot be reached, so the - migration step that follows reports the connection problem the way it does today.""" + def _baseline_refusal(prisma_dir: Path) -> str | None: + """Why `baseline_current_schema` would refuse this database at boot, None when it would baseline: + the migrations folder must be the packaged one, then `prisma migrate diff` must find the live + database and this build's schema.prisma identical.""" + from litellm_proxy_extras.migration_lock import migration_environment + from litellm_proxy_extras.migration_recovery import has_packaged_history + + if not has_packaged_history(prisma_dir): + return ( + f"{prisma_dir} is a custom migration history (LITELLM_MIGRATION_DIR), it differs from the " + "migrations this build ships, and the proxy only baselines onto the packaged history." + ) + try: + prisma_toolchain.run_prisma( + ( + _get_prisma_command(), + "migrate", + "diff", + "--from-schema-datasource", + str(prisma_dir / "schema.prisma"), + "--to-schema-datamodel", + str(prisma_dir / "schema.prisma"), + "--exit-code", + ), + timeout=prisma_toolchain.prisma_command_timeout(), + env=migration_environment(_get_prisma_env()), + ) + except subprocess.CalledProcessError as exc: + diff: Final = str(exc.stdout or exc.stderr or exc).strip() + return f"the schema differs from this build. prisma migrate diff:\n{diff}" + except subprocess.TimeoutExpired as exc: + return f"prisma migrate diff did not finish within {exc.timeout} s" + return None + + @staticmethod + def migration_dry_run_summary(use_migrate: bool = True) -> tuple[bool, str]: + """`--check_migrations_against_existing_data`: the pending migrations applied for real on + DIRECT_URL or DATABASE_URL inside one transaction that is rolled back, so Postgres reports + what the upgrade would hit. (True, the per-migration timings) when they all apply, + (False, Postgres's own error) when one does not.""" import psycopg + if not use_migrate: + return (True, "The schema is pushed rather than migrated, there is nothing to dry-run.") database_url: Final = os.environ.get("DATABASE_URL") if not database_url: - return () + return (False, "DATABASE_URL is not set.") direct_url: Final = ProxyExtrasDBManager._strip_prisma_query_params( os.environ.get("DIRECT_URL") or database_url ) schema: Final = ProxyExtrasDBManager._prisma_schema_param(database_url) or "public" - migrations_dir: Final = Path(ProxyExtrasDBManager._get_prisma_dir()) / "migrations" if use_migrate else None - indexes: Final = tuple((index.table, index.name, index.columns) for index in REQUEST_LOG_INDEXES) + prisma_dir: Final = Path(ProxyExtrasDBManager._get_prisma_dir()) try: with psycopg.connect(direct_url, connect_timeout=10, autocommit=True) as connection: - checks: Final = pending_change_checks(connection, schema, migrations_dir, indexes) - return find_blockers(connection, schema, checks, migration_data_check_timeout_seconds()) + result: Final = dry_run( + connection, + schema, + prisma_dir / "migrations", + baseline_check=lambda: ProxyExtrasDBManager._baseline_refusal(prisma_dir), + ) except psycopg.OperationalError as exc: - logger.warning( - "Could not reach the database for the pre-migration row check: %s", _redact_credentials(str(exc)) - ) - return () - - @staticmethod - def migration_data_check_report(use_migrate: bool = True) -> Optional[str]: - """The operator-facing refusal for `migration_data_check_findings`, None when nothing blocks.""" - findings: Final = ProxyExtrasDBManager.migration_data_check_findings(use_migrate=use_migrate) - return report(findings) if findings else None - - @staticmethod - def raise_if_rows_block_pending_changes(use_migrate: bool) -> None: - """Refuse to run migrations that the existing rows would make fail, before any is - applied. The RuntimeError carries the report and reaches the operator through the - proxy's "Database migration cannot proceed" exit.""" - refusal: Final = ProxyExtrasDBManager.migration_data_check_report(use_migrate=use_migrate) - if refusal is not None: - raise RuntimeError(refusal) + return (False, f"Could not reach the database: {_redact_credentials(str(exc))}") + return (result.ok, dry_run_report(result)) @staticmethod def build_request_log_indexes(build: Callable[[str, str], bool] = ensure_request_log_indexes) -> bool: @@ -1724,5 +1740,4 @@ class ProxyExtrasDBManager: time.sleep(random.randrange(5, 15)) finally: os.chdir(original_dir) - pass return False diff --git a/litellm-rust/Cargo.lock b/litellm-rust/Cargo.lock index f7d7e5f4dab..e959f77917b 100644 --- a/litellm-rust/Cargo.lock +++ b/litellm-rust/Cargo.lock @@ -4345,6 +4345,7 @@ dependencies = [ "litellm-inference-transcription", "litellm-llms", "litellm-llms-types", + "litellm-model-catalog", "litellm-router-types", "litellm-secrets", "litellm-secrets-aws", diff --git a/litellm-rust/crates/core-utils/src/exception_mapping_utils/openai.rs b/litellm-rust/crates/core-utils/src/exception_mapping_utils/openai.rs index d45078c415e..1273fd4d7f1 100644 --- a/litellm-rust/crates/core-utils/src/exception_mapping_utils/openai.rs +++ b/litellm-rust/crates/core-utils/src/exception_mapping_utils/openai.rs @@ -53,6 +53,7 @@ pub(super) const RULES: &[Rule] = &[ |mapping| { mapping.error_str.contains("invalid_request_error") && !mapping.error_str.contains("Incorrect API key provided") + && !matches!(mapping.status, Some(401 | 404)) }, PublicError::BadRequest, ), @@ -132,6 +133,23 @@ mod tests { assert_eq!(classified(Some(400), text), Some(expected)); } + #[rstest::rstest] + #[case::unauthorized(Some(401), None)] + #[case::not_found(Some(404), None)] + #[case::forbidden(Some(403), Some(PublicError::BadRequest))] + #[case::rate_limited(Some(429), Some(PublicError::BadRequest))] + #[case::server_error(Some(500), Some(PublicError::BadRequest))] + #[case::no_status(None, Some(PublicError::BadRequest))] + fn invalid_request_errors_leave_401_and_404_to_the_status_mapping( + #[case] status: Option, + #[case] expected: Option, + ) { + assert_eq!( + classified(status, "invalid_request_error bad field"), + expected + ); + } + #[rstest::rstest] #[case::rate_limit_before_context_window( "rate limit and This model's maximum context length is 10", diff --git a/litellm-rust/crates/model-catalog/AGENTS.md b/litellm-rust/crates/model-catalog/AGENTS.md index fc4b28d4ef0..ff24fb81143 100644 --- a/litellm-rust/crates/model-catalog/AGENTS.md +++ b/litellm-rust/crates/model-catalog/AGENTS.md @@ -25,3 +25,5 @@ A new `N`, image size or resolution in an existing key shape parses with no Rust ## Tests `BUNDLED_REGISTRY` embeds the root `model_prices_and_context_window.json`, so the tests over it fail a JSON change with a bad value or a key no type reads + +`Catalog::problems` is the one definition of a mergeable cost map. The bundled test asserts it is empty, and the cost map guard workflow runs it on a pull request's JSON through `cargo run --bin check-registry ` diff --git a/litellm-rust/crates/model-catalog/Cargo.toml b/litellm-rust/crates/model-catalog/Cargo.toml index 45d16515dcd..63b9ad14768 100644 --- a/litellm-rust/crates/model-catalog/Cargo.toml +++ b/litellm-rust/crates/model-catalog/Cargo.toml @@ -10,6 +10,7 @@ schema = ["dep:schemars", "litellm-cost/schema"] [dependencies] litellm-cost = { workspace = true, features = ["serde"] } +litellm-error.workspace = true indexmap = { version = "2.14.0", features = ["serde"] } jiff = { version = "0.2.37", default-features = false, features = ["std", "tzdb-bundle-always"] } @@ -24,5 +25,4 @@ time.workspace = true [dev-dependencies] jsonschema = { version = "0.55.1", default-features = false } -litellm-error.workspace = true rstest.workspace = true diff --git a/litellm-rust/crates/model-catalog/src/bin/check-registry.rs b/litellm-rust/crates/model-catalog/src/bin/check-registry.rs new file mode 100644 index 00000000000..6fdc7621a0a --- /dev/null +++ b/litellm-rust/crates/model-catalog/src/bin/check-registry.rs @@ -0,0 +1,26 @@ +use std::{env, fs, path::PathBuf, process::ExitCode}; + +use litellm_model_catalog::Catalog; + +fn main() -> ExitCode { + let Some(path) = env::args_os().nth(1).map(PathBuf::from) else { + eprintln!("usage: check-registry "); + return ExitCode::from(2); + }; + let body = match fs::read(&path) { + Ok(body) => body, + Err(error) => { + eprintln!("{}: {error}", path.display()); + return ExitCode::FAILURE; + } + }; + let problems = Catalog::problems(&body); + for problem in &problems { + eprintln!("{}", litellm_error::Report::new(problem)); + } + if problems.is_empty() { + ExitCode::SUCCESS + } else { + ExitCode::FAILURE + } +} diff --git a/litellm-rust/crates/model-catalog/src/catalog.rs b/litellm-rust/crates/model-catalog/src/catalog.rs index ac5f4ffcf25..98af785b17e 100644 --- a/litellm-rust/crates/model-catalog/src/catalog.rs +++ b/litellm-rust/crates/model-catalog/src/catalog.rs @@ -5,7 +5,7 @@ use indexmap::IndexMap; use serde::Deserialize; use serde_json::Value; -use crate::error::{Error, Loaded, Rejects, Repr}; +use crate::error::{Error, Loaded, Rejects, Repr, pointer_child, pointer_for_key}; use crate::fallback::{FallbackGeneralizations, FallbackRule}; use crate::model::Model; use crate::store::{Rows, Store}; @@ -211,6 +211,36 @@ impl Catalog { }) } + pub fn problems(body: &[u8]) -> Vec { + let Loaded { + value: catalog, + errors, + } = match Self::parse(body) { + Ok(loaded) => loaded, + Err(fatal) => return vec![fatal], + }; + let unread = catalog.rows().entries.iter().flat_map(|(model, entry)| { + EntryWire::read(entry.fields()) + .unknown + .into_iter() + .map(move |(key, _)| { + Error::new( + pointer_child(&pointer_for_key(model), &key), + Repr::UnreadField, + ) + }) + }); + let no_fallback_rules = catalog + .fallback_rules() + .is_none_or(<[_]>::is_empty) + .then(|| Error::from_key(FALLBACK_GENERALIZATIONS, Repr::NoFallbackRules)); + errors + .into_iter() + .chain(unread) + .chain(no_fallback_rules) + .collect() + } + pub fn row_errors(model: &str, fields: &Fields) -> Vec { let (_, _, errors) = read_row(fields); errors @@ -404,3 +434,85 @@ fn unknown_timezone(wire: &EntryWire) -> Option { ) }) } + +#[cfg(test)] +mod tests { + use rstest::rstest; + + use super::*; + + const RULES: &str = r#""fallback_generalizations":{"rules":[{"name":"family","pattern":"^new-","model_info":{"mode":"chat"}}]}"#; + + #[rstest] + #[case::clean(format!(r#"{{{RULES},"m":{{"litellm_provider":"test"}}}}"#), vec![])] + #[case::not_json("{".to_owned(), vec![("", "json")])] + #[case::row_error(format!(r#"{{{RULES},"m":{{}}}}"#), vec![("/m/litellm_provider", "missing_provider")])] + #[case::unread_key( + format!(r#"{{{RULES},"m":{{"litellm_provider":"test","typo_cost":1}}}}"#), + vec![("/m/typo_cost", "unread_field")] + )] + #[case::empty_model_key( + format!(r#"{{{RULES},"":{{"litellm_provider":"test","future_field":true}}}}"#), + vec![("//future_field", "unread_field")] + )] + #[case::unread_key_on_invalid_row( + format!(r#"{{{RULES},"m":{{"typo_cost":1}}}}"#), + vec![("/m/litellm_provider", "missing_provider"), ("/m/typo_cost", "unread_field")] + )] + #[case::escaped_unread_key_on_invalid_row( + format!(r#"{{{RULES},"open/router~v1":{{"typo/cost~":1}}}}"#), + vec![("/open~1router~0v1/litellm_provider", "missing_provider"), ("/open~1router~0v1/typo~1cost~0", "unread_field")] + )] + #[case::no_rules( + r#"{"m":{"litellm_provider":"test"}}"#.to_owned(), + vec![("/fallback_generalizations", "no_fallback_rules")] + )] + #[case::empty_rules( + r#"{"fallback_generalizations":{"rules":[]},"m":{"litellm_provider":"test"}}"#.to_owned(), + vec![("/fallback_generalizations", "no_fallback_rules")] + )] + fn problems_list_what_a_cost_map_change_must_fix( + #[case] body: String, + #[case] expected: Vec<(&str, &str)>, + ) { + let problems: Vec<_> = Catalog::problems(body.as_bytes()) + .iter() + .map(|problem| (problem.path().to_owned(), <&str>::from(problem.kind()))) + .collect(); + let expected: Vec<_> = expected + .into_iter() + .map(|(path, kind)| (path.to_owned(), kind)) + .collect(); + assert_eq!(problems, expected); + } + + #[rstest] + fn runtime_parsing_preserves_custom_fields_and_recovers_valid_rows() { + let body = br#"{ + "good":{"litellm_provider":"test","custom_metadata":true}, + "bad":{"litellm_provider":"test","input_cost_per_token":"wrong"} + }"#; + let loaded = Catalog::parse(body).unwrap(); + assert_eq!(loaded.errors.len(), 1); + assert_eq!(loaded.errors[0].path(), "/bad/input_cost_per_token"); + let (catalog, rejected) = loaded.value.without_rejected(&loaded.errors); + assert_eq!(rejected.len(), 1); + assert!(catalog.lookup("bad").is_none()); + let good = catalog.lookup("good").unwrap(); + assert!(good.entry.model().is_some()); + assert_eq!( + good.entry.field("custom_metadata"), + Some(&Value::Bool(true)) + ); + assert!( + Catalog::problems(body) + .iter() + .any(|problem| problem.kind() == crate::ErrorKind::UnreadField) + ); + assert!( + Catalog::problems(body) + .iter() + .any(|problem| problem.kind() == crate::ErrorKind::NoFallbackRules) + ); + } +} diff --git a/litellm-rust/crates/model-catalog/src/error.rs b/litellm-rust/crates/model-catalog/src/error.rs index 425ae47687f..89e27c05f46 100644 --- a/litellm-rust/crates/model-catalog/src/error.rs +++ b/litellm-rust/crates/model-catalog/src/error.rs @@ -109,6 +109,12 @@ pub(crate) enum Repr { #[source] cause: Option, }, + #[error("not a field litellm reads")] + #[strum_discriminants(strum(serialize = "unread_field"))] + UnreadField, + #[error("has no rules")] + #[strum_discriminants(strum(serialize = "no_fallback_rules"))] + NoFallbackRules, } /// What a problem takes out of the served map @@ -148,7 +154,7 @@ impl Error { | Repr::NoOffPeakHours | Repr::EmptyList | Repr::InvalidDate { .. } => Rejects::Row, - Repr::MissingProvider => Rejects::Nothing, + Repr::MissingProvider | Repr::UnreadField | Repr::NoFallbackRules => Rejects::Nothing, } } } @@ -334,6 +340,8 @@ mod tests { #[case::no_off_peak_hours(Repr::NoOffPeakHours, Rejects::Row)] #[case::empty_list(Repr::EmptyList, Rejects::Row)] #[case::invalid_date(Repr::InvalidDate { cause: None }, Rejects::Row)] + #[case::unread_field(Repr::UnreadField, Rejects::Nothing)] + #[case::no_fallback_rules(Repr::NoFallbackRules, Rejects::Nothing)] fn policy_classifies_each_error(#[case] error: Repr, #[case] expected: Rejects) { assert_eq!(Error::from(error).rejects(), expected); } diff --git a/litellm-rust/crates/model-catalog/tests/catalog.rs b/litellm-rust/crates/model-catalog/tests/catalog.rs index 30c070cfb52..31f907689d3 100644 --- a/litellm-rust/crates/model-catalog/tests/catalog.rs +++ b/litellm-rust/crates/model-catalog/tests/catalog.rs @@ -366,18 +366,14 @@ fn written_is_the_document_in_order_including_sections_and_bad_entries() { } #[rstest] -fn the_bundled_registry_parses_with_no_problems_and_every_key_understood() { - let loaded = Catalog::parse(BUNDLED_REGISTRY).unwrap(); - let catalog = loaded.value; - assert!(loaded.errors.is_empty(), "{:#?}", loaded.errors); - let unrecognized: Vec<_> = catalog.unrecognized().collect(); - assert!(unrecognized.is_empty(), "{unrecognized:#?}"); - assert!( - catalog - .fallback_rules() - .is_some_and(|rules| !rules.is_empty()) - ); +fn the_bundled_registry_has_no_problems() { + let problems: Vec<_> = Catalog::problems(BUNDLED_REGISTRY) + .iter() + .map(report) + .collect(); + assert!(problems.is_empty(), "{problems:#?}"); const REFERENCE_MODEL_COUNT: usize = 4303; + let catalog = Catalog::parse(BUNDLED_REGISTRY).unwrap().value; assert!(catalog.model_count() as f64 >= REFERENCE_MODEL_COUNT as f64 * 0.9); } diff --git a/litellm-rust/crates/python-bridge/AGENTS.md b/litellm-rust/crates/python-bridge/AGENTS.md index d90fe073b20..26858b2afe2 100644 --- a/litellm-rust/crates/python-bridge/AGENTS.md +++ b/litellm-rust/crates/python-bridge/AGENTS.md @@ -106,6 +106,18 @@ GIL handling to `litellm-host-python`. ## Data Handling +- Reuse large immutable bundled data and parsed catalogs across calls, including in + replacement APIs. Share Rust storage and cache repeated Python projections instead + of reparsing, serializing or copying the full payload on every access +- Cache owned Python objects with `PyOnceLock>`, then bind and clone references + while attached. Do not use blocking `OnceLock::get_or_init` while attached + ([PyO3 initialization](https://pyo3.rs/v0.29.2/faq.html), + [reference ownership](https://pyo3.rs/v0.29.2/types.html)) +- Never reuse Python objects across interpreters or interpreter reinitialization. + Once-only caches are for immutable data. Reloadable/custom data needs versioned + entries or invalidation, and caller-mutable Python containers must not be shared +- Test unchanged contents and allocation reuse, not just equal values. Keep this + coverage when replacing a bundled-data accessor with a catalog API - OCR payloads can contain personal data and large base64 images. Do not log payloads or provider responses. - Avoid copying large payloads more than needed. The current JSON round-trip is diff --git a/litellm-rust/crates/python-bridge/Cargo.toml b/litellm-rust/crates/python-bridge/Cargo.toml index 9d69cd8c14b..a4e1c57ab1e 100644 --- a/litellm-rust/crates/python-bridge/Cargo.toml +++ b/litellm-rust/crates/python-bridge/Cargo.toml @@ -49,6 +49,7 @@ litellm-secrets-types.workspace = true litellm-llms-types.workspace = true litellm-router-types.workspace = true litellm-host-python.workspace = true +litellm-model-catalog.workspace = true litellm-token-counter = { path = "../token-counter", default-features = false } pyo3.workspace = true prost.workspace = true diff --git a/litellm-rust/crates/python-bridge/src/lib.rs b/litellm-rust/crates/python-bridge/src/lib.rs index 12827b76ff3..683e90d02a2 100644 --- a/litellm-rust/crates/python-bridge/src/lib.rs +++ b/litellm-rust/crates/python-bridge/src/lib.rs @@ -8,6 +8,7 @@ mod execution; mod http; mod logger; mod marshal; +mod model_registry; mod preflight; mod python_settings; mod routes; @@ -30,6 +31,8 @@ mod _native { #[pymodule_export] use crate::logger::NativeDiagnosticProcessor; #[pymodule_export] + use crate::model_registry::bundled_model_cost_map; + #[pymodule_export] use crate::routes::audio_transcription::{atranscription, transcription}; #[pymodule_export] use crate::routes::chat_completions::{acompletion, completion}; @@ -110,6 +113,7 @@ mod tests { "NativeClickHouseSpendConfig", "NativeClickHouseSpendStorage", "trace_encode_error", + "bundled_model_cost_map", "TokenCounter", "Tokenizer", "gil_stats", diff --git a/litellm-rust/crates/python-bridge/src/model_registry.rs b/litellm-rust/crates/python-bridge/src/model_registry.rs new file mode 100644 index 00000000000..b7b7cd1fbb3 --- /dev/null +++ b/litellm-rust/crates/python-bridge/src/model_registry.rs @@ -0,0 +1,30 @@ +use pyo3::prelude::*; +use pyo3::sync::PyOnceLock; +use pyo3::types::PyBytes; + +#[pyfunction] +pub fn bundled_model_cost_map(py: Python<'_>) -> Bound<'_, PyBytes> { + static BYTES: PyOnceLock> = PyOnceLock::new(); + BYTES + .get_or_init(py, || { + PyBytes::new(py, litellm_model_catalog::BUNDLED_REGISTRY).unbind() + }) + .bind(py) + .clone() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[rstest::rstest] + fn repeated_calls_reuse_the_bundled_registry_bytes() { + Python::initialize(); + let first = Python::attach(|py| bundled_model_cost_map(py).unbind()); + Python::attach(|py| { + let second = bundled_model_cost_map(py); + assert!(first.bind(py).is(&second)); + assert_eq!(second.as_bytes(), litellm_model_catalog::BUNDLED_REGISTRY); + }); + } +} diff --git a/litellm/__init__.py b/litellm/__init__.py index 5e8f4246cef..a1bda4aa9f7 100644 --- a/litellm/__init__.py +++ b/litellm/__init__.py @@ -685,6 +685,7 @@ moonshot_models: Set = set() publicai_models: Set = set() darkbloom_models: Set = set() openinfer_models: Final[Set[str]] = set() +kyma_models: Final[set[str]] = set() # mutable-ok: populated when the shared model catalog loads streamlake_models: Final[Set[str]] = set() zerogpu_models: Final[Set[str]] = set() v0_models: Set = set() @@ -981,6 +982,8 @@ def _populate_provider_model_sets(model_cost_map: Mapping[str, Mapping[str, obje darkbloom_models.add(key) elif value.get("litellm_provider") == "openinfer": openinfer_models.add(key) + elif value.get("litellm_provider") == "kyma": + kyma_models.add(key) elif value.get("litellm_provider") == "streamlake": streamlake_models.add(key) elif value.get("litellm_provider") == "zerogpu": @@ -1159,6 +1162,7 @@ model_list = list( | publicai_models | darkbloom_models | openinfer_models + | kyma_models | streamlake_models | zerogpu_models | v0_models @@ -1290,6 +1294,7 @@ def _build_models_by_provider() -> dict: "publicai": publicai_models, "darkbloom": darkbloom_models, "openinfer": openinfer_models, + "kyma": kyma_models, "streamlake": streamlake_models, "zerogpu": zerogpu_models, "v0": v0_models, diff --git a/litellm/_internal_context.py b/litellm/_internal_context.py index 244efc8047b..c2243b90fbf 100644 --- a/litellm/_internal_context.py +++ b/litellm/_internal_context.py @@ -36,6 +36,8 @@ REDIS_FAMILIES_METADATA_KEY: Final = "families" _service_caller: Final[ContextVar[str | None]] = ContextVar("service_caller", default=None) +_mirrored_call: Final[ContextVar[bool]] = ContextVar("mirrored_call", default=False) + @contextmanager def internal_sub_call() -> Generator[None]: @@ -123,6 +125,30 @@ def current_service_caller() -> str | None: return _service_caller.get() +@contextmanager +def mirrored_call() -> Generator[None]: + """A silent_model mirror of a request: every litellm call inside, its retries and fallbacks included.""" + token: Final = _mirrored_call.set(True) + try: + yield + finally: + _restore(_mirrored_call, token) + + +def in_mirrored_call() -> bool: + return _mirrored_call.get() + + +@contextmanager +def outside_mirrored_call() -> Generator[None]: + """Logging a mirrored call: the calls its callbacks start are not mirrors themselves.""" + token: Final = _mirrored_call.set(False) + try: + yield + finally: + _restore(_mirrored_call, token) + + @contextmanager def pinned_billing_time(moment: datetime) -> Generator[None]: """Price every rate lookup inside this block at ``moment`` rather than at each one's own clock read.""" diff --git a/litellm/anthropic_beta_gated_fields.py b/litellm/anthropic_beta_gated_fields.py new file mode 100644 index 00000000000..fc36699bc93 --- /dev/null +++ b/litellm/anthropic_beta_gated_fields.py @@ -0,0 +1,325 @@ +"""Body fields that an ``anthropic-beta`` value gates. + +``anthropic_beta_headers_config.json`` says which beta each provider leg +carries. When a leg drops a beta, the request fields that beta unlocks +would make the provider 400. Under ``drop_params`` the same request degrades on +that leg by dropping those fields too; without it the request goes out as sent +and the provider answers its own 400, like any unsupported param. Every helper +here is copy-on-write: the caller's containers are returned untouched when +nothing is stripped, and a shallow copy is returned otherwise, so a fallback +leg still sends the full request. +""" + +from collections.abc import Mapping, Sequence, Set +from dataclasses import dataclass +from types import MappingProxyType +from typing import Final + +from pydantic import TypeAdapter, ValidationError + +from litellm._logging import verbose_logger +from litellm.litellm_core_utils.core_helpers import drops_unsupported_params +from litellm.types.llms.anthropic import ( + ANTHROPIC_BETA_HEADER_VALUES, + ANTHROPIC_MID_CONVERSATION_OUTPUT_CONFIG_BETA_HEADER, + ANTHROPIC_MID_CONVERSATION_TOOL_CHANGES_BETA_HEADER, + ANTHROPIC_THINKING_BINDING_CONTROLS_BETA_HEADER, + ANTHROPIC_THINKING_DISPLAY_UPDATES_BETA_HEADER, +) + +ANTHROPIC_MID_CONVERSATION_SYSTEM_CLEAR_AT_BETA_HEADER: Final = "mid-conversation-system-clear-at-2026-08-21" +ANTHROPIC_INLINE_TOOLS_BETA_HEADER: Final = "inline-tools-2026-09-15" + +THINKING_BLOCK_BINDING: Final = "thinking.block_binding" +THINKING_DISPLAY_UPDATES: Final = "thinking.display=updates" +SYSTEM_CLEAR_AT: Final = "messages[].system.clear_at" +SYSTEM_OUTPUT_CONFIG: Final = "messages[].system.output_config" +SYSTEM_TOOL_CHANGES: Final = "messages[].system.content[].tool_addition|tool_removal" +SYSTEM_MESSAGE_EMPTIED: Final = "messages[].system (emptied by the strip)" + +_TOOL_CHANGE_BLOCK_TYPES: Final = frozenset({"tool_addition", "tool_removal"}) + +BETA_GATED_FIELDS: Final[Mapping[str, tuple[str, ...]]] = MappingProxyType( + { + ANTHROPIC_THINKING_BINDING_CONTROLS_BETA_HEADER: (THINKING_BLOCK_BINDING,), + ANTHROPIC_THINKING_DISPLAY_UPDATES_BETA_HEADER: (THINKING_DISPLAY_UPDATES,), + ANTHROPIC_MID_CONVERSATION_SYSTEM_CLEAR_AT_BETA_HEADER: (SYSTEM_CLEAR_AT,), + ANTHROPIC_MID_CONVERSATION_OUTPUT_CONFIG_BETA_HEADER: (SYSTEM_OUTPUT_CONFIG,), + ANTHROPIC_BETA_HEADER_VALUES.PER_TURN_CONTROL_2026_07_01.value: (SYSTEM_OUTPUT_CONFIG,), + ANTHROPIC_MID_CONVERSATION_TOOL_CHANGES_BETA_HEADER: (SYSTEM_TOOL_CHANGES,), + ANTHROPIC_INLINE_TOOLS_BETA_HEADER: (SYSTEM_TOOL_CHANGES,), + } +) + + +def _betas_unlocking(field: str) -> frozenset[str]: + return frozenset(beta for beta, fields in BETA_GATED_FIELDS.items() if field in fields) + + +_FIELD_BETAS: Final[Mapping[str, frozenset[str]]] = MappingProxyType( + { + field: _betas_unlocking(field) for fields in BETA_GATED_FIELDS.values() for field in fields + } # comprehension-ok: flattening the beta-to-fields map into a field-to-betas index +) + + +def field_is_gated(field: str, active_betas: Set[str]) -> bool: + """True when no active beta unlocks ``field``, so the field must be stripped.""" + return _FIELD_BETAS[field].isdisjoint(active_betas) + + +_MAPPING_ADAPTER: Final = TypeAdapter(Mapping[str, object]) +_SEQUENCE_ADAPTER: Final = TypeAdapter(tuple[object, ...]) +_HEADERS_ADAPTER: Final = TypeAdapter(Mapping[str, str]) + + +def _as_mapping(value: object) -> Mapping[str, object] | None: + try: + return _MAPPING_ADAPTER.validate_python(value, strict=True) + except ValidationError: + return None + + +def _as_sequence(value: object) -> tuple[object, ...] | None: + try: + return _SEQUENCE_ADAPTER.validate_python(value) + except ValidationError: + return None + + +def _is_tool_change_block(block: object) -> bool: + mapping: Final = _as_mapping(block) + block_type: Final = mapping.get("type") if mapping is not None else None + return isinstance(block_type, str) and block_type in _TOOL_CHANGE_BLOCK_TYPES + + +def _header_beta_values(headers: Mapping[str, str]) -> frozenset[str]: + joined: Final = ",".join(value for key, value in headers.items() if key.lower() == "anthropic-beta") + return frozenset(piece.strip() for piece in joined.split(",") if piece.strip()) + + +def _validated_headers(headers: object) -> Mapping[str, str]: + try: + return _HEADERS_ADAPTER.validate_python(headers) + except ValidationError: + return MappingProxyType({}) + + +def active_beta_values(headers: object, body_betas: object = ()) -> frozenset[str]: + """The ``anthropic-beta`` values a request will send: the comma-joined header (key matched + case-insensitively) plus the body's ``anthropic_beta`` list on legs that carry betas in the body.""" + body_values: Final = _as_sequence(body_betas) or () + body_betas_as_strings: Final = frozenset(beta for beta in body_values if isinstance(beta, str)) + return _header_beta_values(_validated_headers(headers)) | body_betas_as_strings + + +@dataclass(frozen=True, slots=True) +class StrippedThinking: + thinking: object + stripped: tuple[str, ...] + + +@dataclass(frozen=True, slots=True) +class StrippedMessages: + messages: Sequence[object] + stripped: tuple[str, ...] + + +@dataclass(frozen=True, slots=True) +class StrippedRequest: + request: Mapping[str, object] + stripped: tuple[str, ...] + + +def strip_beta_gated_thinking(thinking: object, active_betas: Set[str], *, drop_params: object) -> StrippedThinking: + """Drop ``thinking`` sub-fields whose beta is absent. Returns the same object when nothing is gated.""" + mapping: Final = _as_mapping(thinking) + if mapping is None or not drops_unsupported_params(drop_params): + return StrippedThinking(thinking=thinking, stripped=()) + binding_gated: Final = "block_binding" in mapping and field_is_gated(THINKING_BLOCK_BINDING, active_betas) + display_gated: Final = mapping.get("display") == "updates" and field_is_gated( + THINKING_DISPLAY_UPDATES, active_betas + ) + if not binding_gated and not display_gated: + return StrippedThinking(thinking=thinking, stripped=()) + gated_keys: Final = frozenset( + {*(("block_binding",) if binding_gated else ()), *(("display",) if display_gated else ())} + ) + stripped: Final = ( + *((THINKING_BLOCK_BINDING,) if binding_gated else ()), + *((THINKING_DISPLAY_UPDATES,) if display_gated else ()), + ) + return StrippedThinking( + thinking={key: value for key, value in mapping.items() if key not in gated_keys}, + stripped=stripped, + ) + + +def _strip_beta_gated_system_message(message: Mapping[str, object], active_betas: Set[str]) -> StrippedMessages: + """One mid-conversation ``role: system`` message. ``messages`` holds the rewritten message, or nothing + when the strip left it with neither content nor ``output_config``, since such a system turn is itself a 400.""" + clear_at_gated: Final = "clear_at" in message and field_is_gated(SYSTEM_CLEAR_AT, active_betas) + output_config_gated: Final = "output_config" in message and field_is_gated(SYSTEM_OUTPUT_CONFIG, active_betas) + content: Final = message.get("content") + blocks: Final = _as_sequence(content) if not isinstance(content, str) else None + tool_changes_gated: Final = ( + blocks is not None + and any(_is_tool_change_block(block) for block in blocks) + and field_is_gated(SYSTEM_TOOL_CHANGES, active_betas) + ) + if not clear_at_gated and not output_config_gated and not tool_changes_gated: + return StrippedMessages(messages=(message,), stripped=()) + kept_content: Final[object] = ( + [block for block in blocks if not _is_tool_change_block(block)] + if tool_changes_gated and blocks is not None + else content + ) + gated_keys: Final = frozenset( + {*(("clear_at",) if clear_at_gated else ()), *(("output_config",) if output_config_gated else ())} + ) + rewritten: Final = { + key: (kept_content if key == "content" else value) for key, value in message.items() if key not in gated_keys + } + stripped: Final = ( + *((SYSTEM_CLEAR_AT,) if clear_at_gated else ()), + *((SYSTEM_OUTPUT_CONFIG,) if output_config_gated else ()), + *((SYSTEM_TOOL_CHANGES,) if tool_changes_gated else ()), + ) + if kept_content in (None, "", []) and "output_config" not in rewritten: + return StrippedMessages(messages=(), stripped=(*stripped, SYSTEM_MESSAGE_EMPTIED)) + return StrippedMessages(messages=(rewritten,), stripped=stripped) + + +def _strip_beta_gated_message(message: object, active_betas: Set[str]) -> StrippedMessages: + mapping: Final = _as_mapping(message) + if mapping is None or mapping.get("role") != "system": + return StrippedMessages(messages=(message,), stripped=()) + return _strip_beta_gated_system_message(mapping, active_betas) + + +def _ends_on_assistant(messages: Sequence[object]) -> bool: + last: Final = _as_mapping(messages[-1]) if messages else None + return last is not None and last.get("role") == "assistant" + + +def strip_beta_gated_system_messages( + messages: Sequence[object], active_betas: Set[str], *, drop_params: object +) -> StrippedMessages: + """Drop the gated fields of every mid-conversation ``role: system`` message. Returns the same + sequence when nothing is gated, or when dropping an emptied system turn would leave the conversation + ending on an assistant turn, which the model would read as a prefill to continue.""" + if not drops_unsupported_params(drop_params): + return StrippedMessages(messages=messages, stripped=()) + per_message: Final = tuple(_strip_beta_gated_message(message, active_betas) for message in messages) + stripped: Final = tuple( + name for result in per_message for name in result.stripped + ) # comprehension-ok: flattening per-message strip reports + if not stripped: + return StrippedMessages(messages=messages, stripped=()) + kept: Final = [ + message for result in per_message for message in result.messages + ] # comprehension-ok: flattening per-message results + if _ends_on_assistant(kept) and not _ends_on_assistant(messages): + return StrippedMessages(messages=messages, stripped=()) + return StrippedMessages(messages=kept, stripped=stripped) + + +def strip_beta_gated_request_fields( + request: Mapping[str, object], active_betas: Set[str], *, drop_params: object +) -> StrippedRequest: + """Apply both strips to an Anthropic ``/v1/messages`` shaped body. Returns the same mapping when + nothing is gated, otherwise a shallow copy with ``thinking`` and ``messages`` replaced.""" + thinking: Final = strip_beta_gated_thinking(request.get("thinking"), active_betas, drop_params=drop_params) + messages: Final = request.get("messages") + message_sequence: Final = _as_sequence(messages) if not isinstance(messages, str) else None + system_messages: Final = ( + strip_beta_gated_system_messages(message_sequence, active_betas, drop_params=drop_params) + if message_sequence is not None + else StrippedMessages(messages=(), stripped=()) + ) + stripped: Final = (*thinking.stripped, *system_messages.stripped) + if not stripped: + return StrippedRequest(request=request, stripped=()) + rewritten: Final = { + **request, + **({"thinking": thinking.thinking} if thinking.stripped else {}), + **({"messages": system_messages.messages} if system_messages.stripped else {}), + } + return StrippedRequest(request=rewritten, stripped=stripped) + + +@dataclass(frozen=True, slots=True) +class StrippedNativeRequest: + thinking: object + thinking_stripped: bool + messages: Sequence[object] + messages_stripped: bool + stripped: tuple[str, ...] + + +def unstripped_native_request(optional_params: object, messages: object) -> StrippedNativeRequest: + """The identity result for legs that strip later, once their final ``anthropic_beta`` list exists.""" + params: Final = _as_mapping(optional_params) or MappingProxyType({}) + message_sequence: Final = _as_sequence(messages) if not isinstance(messages, str) else None + return StrippedNativeRequest( + thinking=params.get("thinking"), + thinking_stripped=False, + messages=message_sequence if message_sequence is not None else (), + messages_stripped=False, + stripped=(), + ) + + +def strip_beta_gated_native_request( + headers: object, optional_params: object, messages: object, *, body_betas: object = (), drop_params: object +) -> StrippedNativeRequest: + """The ``/v1/messages`` seam, fed the final (already filtered) headers, the final body ``anthropic_beta`` + list on legs that carry betas in the body, plus the body parts it may rewrite.""" + if not drops_unsupported_params(drop_params): + return unstripped_native_request(optional_params, messages) + active_betas: Final = active_beta_values(headers, body_betas) + params: Final = _as_mapping(optional_params) or MappingProxyType({}) + thinking: Final = strip_beta_gated_thinking(params.get("thinking"), active_betas, drop_params=drop_params) + message_sequence: Final = _as_sequence(messages) if not isinstance(messages, str) else None + system_messages: Final = ( + strip_beta_gated_system_messages(message_sequence, active_betas, drop_params=drop_params) + if message_sequence is not None + else StrippedMessages(messages=(), stripped=()) + ) + return StrippedNativeRequest( + thinking=thinking.thinking, + thinking_stripped=bool(thinking.stripped), + messages=system_messages.messages, + messages_stripped=bool(system_messages.stripped), + stripped=(*thinking.stripped, *system_messages.stripped), + ) + + +def log_beta_gated_strips(stripped: Sequence[str], model: str | None, provider: str) -> None: + if not stripped: + return + described: Final = ", ".join( + f"{field} (unlocked by anthropic-beta {' or '.join(sorted(_FIELD_BETAS[field]))})" + if field in _FIELD_BETAS + else field + for field in sorted(set(stripped)) + ) + verbose_logger.warning( + "Dropped %s for model=%s provider=%s because drop_params is on and the provider would reject them on " + "this request, either since this provider leg does not send that anthropic-beta (see " + "anthropic_beta_headers_config.json) or since the model turns mid-conversation system messages into user " + "messages.", + described, + model, + provider, + ) + + +def strip_beta_gated_request_body( + request: Mapping[str, object], headers: object, *, drop_params: object, model: str | None, provider: str +) -> Mapping[str, object]: + gated: Final = strip_beta_gated_request_fields( + request, active_beta_values(headers, request.get("anthropic_beta") or ()), drop_params=drop_params + ) + log_beta_gated_strips(gated.stripped, model=model, provider=provider) + return gated.request diff --git a/litellm/anthropic_beta_headers_config.json b/litellm/anthropic_beta_headers_config.json index d68de6c9d4d..cee5ce15469 100644 --- a/litellm/anthropic_beta_headers_config.json +++ b/litellm/anthropic_beta_headers_config.json @@ -248,6 +248,7 @@ "text_editor_20241022": null, "text_editor_20250124": null, "thinking-binding-controls-2026-08-01": "thinking-binding-controls-2026-08-01", + "thinking-display-updates-2026-08-18": "thinking-display-updates-2026-08-18", "token-efficient-tools-2025-02-19": "token-efficient-tools-2025-02-19", "web-fetch-2025-09-10": null, "web-search-2025-03-05": "web-search-2025-03-05" diff --git a/litellm/anthropic_beta_headers_manager.py b/litellm/anthropic_beta_headers_manager.py index fec1d8e4465..07730a0fbf8 100644 --- a/litellm/anthropic_beta_headers_manager.py +++ b/litellm/anthropic_beta_headers_manager.py @@ -241,6 +241,13 @@ def get_provider_name(provider: str) -> str: return aliases.get(provider, provider) +def has_beta_headers_config(provider: str) -> bool: + config: Final = _bundled_beta_headers_config() + aliases: Final = config.get("provider_aliases", {}) + canonical_provider: Final = (aliases.get(provider) if isinstance(aliases, Mapping) else None) or provider + return canonical_provider in config + + def filter_and_transform_beta_headers( beta_headers: list[str], provider: str, @@ -397,7 +404,7 @@ def update_request_with_filtered_beta( headers: dict, request_data: dict, provider: str, -) -> tuple[dict, dict]: +) -> tuple[dict[str, str], dict[str, object]]: """ Update both headers and request body beta fields based on provider support. Modifies both dicts in place and returns them. diff --git a/litellm/caching/caching.py b/litellm/caching/caching.py index 5f5b8210096..1ced13445de 100644 --- a/litellm/caching/caching.py +++ b/litellm/caching/caching.py @@ -455,17 +455,33 @@ class Cache: 3. Else use the `model` passed in kwargs, plus `model_id` when set (e.g. a SageMaker inference component) """ litellm_params: Final[dict] = kwargs.get("litellm_params", {}) or {} - metadata_sources: Final[tuple[dict, ...]] = ( - kwargs.get("metadata") or {}, - kwargs.get("litellm_metadata") or {}, - litellm_params.get("metadata") or {}, - litellm_params.get("litellm_metadata") or {}, + metadata_sources: Final = cast( # cast-ok: each source is the untyped request's metadata dict or empty + "tuple[dict[str, object], dict[str, object], dict[str, object], dict[str, object]]", + ( + kwargs.get("metadata") or {}, + kwargs.get("litellm_metadata") or {}, + litellm_params.get("metadata") or {}, + litellm_params.get("litellm_metadata") or {}, + ), ) - model_group: Final[str | None] = next( - (source["model_group"] for source in metadata_sources if source.get("model_group")), None + metadata, litellm_metadata, params_metadata, params_litellm_metadata = metadata_sources + call_model_info: Final[object] = kwargs.get("model_info") + routed_source: Final = next( + ( + source + for source in (litellm_metadata, metadata, params_litellm_metadata, params_metadata) + if call_model_info and source.get("model_group") and source.get("model_info") == call_model_info + ), + None, ) + model_group: Final = ( + str(routed_source["model_group"]) + if routed_source is not None + else next((str(source["model_group"]) for source in metadata_sources if source.get("model_group")), None) + ) + caching_group_sources: Final = (routed_source,) if routed_source is not None else metadata_sources caching_group: Final = next( - (group for source in metadata_sources if (group := self._get_caching_group(source, model_group))), None + (group for source in caching_group_sources if (group := self._get_caching_group(source, model_group))), None ) model_id_suffix: Final = ( f" model_id: {kwargs['model_id']}" if "model_id" in kwargs and kwargs["model_id"] is not None else "" diff --git a/litellm/caching/qdrant_semantic_cache.py b/litellm/caching/qdrant_semantic_cache.py index 924506dfa45..560a5ba06b8 100644 --- a/litellm/caching/qdrant_semantic_cache.py +++ b/litellm/caching/qdrant_semantic_cache.py @@ -13,7 +13,7 @@ import asyncio import json import os from types import MappingProxyType -from typing import TYPE_CHECKING, Any, Final, Protocol, cast +from typing import TYPE_CHECKING, Any, Final, Protocol from pydantic import TypeAdapter @@ -292,10 +292,7 @@ class QdrantSemanticCache(BaseCache): prompt: Final = get_semantic_cache_prompt_from_messages(messages) # create an embedding for prompt - embedding_response: Final = cast( - EmbeddingResponse, - self._get_embedding(prompt, metadata=kwargs.get("metadata")), - ) + embedding_response: Final = self._get_embedding(prompt, metadata=kwargs.get("metadata")) # get the embedding embedding: Final = _EMBEDDING_VECTOR.validate_python(embedding_response["data"][0]["embedding"]) @@ -331,10 +328,7 @@ class QdrantSemanticCache(BaseCache): prompt: Final = get_semantic_cache_prompt_from_messages(messages) # convert to embedding - embedding_response: Final = cast( - EmbeddingResponse, - self._get_embedding(prompt, metadata=kwargs.get("metadata")), - ) + embedding_response: Final = self._get_embedding(prompt, metadata=kwargs.get("metadata")) # get the embedding embedding: Final = _EMBEDDING_VECTOR.validate_python(embedding_response["data"][0]["embedding"]) diff --git a/litellm/caching/redis_semantic_cache.py b/litellm/caching/redis_semantic_cache.py index b892e4c3a1d..7268511fe7e 100644 --- a/litellm/caching/redis_semantic_cache.py +++ b/litellm/caching/redis_semantic_cache.py @@ -14,7 +14,9 @@ import asyncio import json import os from collections.abc import Callable, Mapping -from typing import TYPE_CHECKING, Any, Final, cast +from typing import TYPE_CHECKING, Final, cast + +from pydantic import ConfigDict, TypeAdapter import litellm from litellm._logging import print_verbose, verbose_logger @@ -38,6 +40,11 @@ if TYPE_CHECKING: from litellm.router import Router +_CACHE_RESPONSE_ADAPTER: Final[TypeAdapter[str | bytes | None]] = TypeAdapter( + str | bytes | None, config=ConfigDict(strict=True, hide_input_in_errors=True) +) + + class RedisSemanticCache(BaseCache): """ Redis-backed semantic cache for LLM responses. @@ -380,7 +387,7 @@ class RedisSemanticCache(BaseCache): ) return embedding_response["data"][0]["embedding"] - def _get_cache_logic(self, cached_response: Any) -> object: + def _get_cache_logic(self, cached_response: str | bytes | None) -> object: """ Process the cached response to prepare it for use. @@ -391,24 +398,23 @@ class RedisSemanticCache(BaseCache): The processed cache response, or None if input was None """ if cached_response is None: - return cached_response + return None # Convert bytes to string if needed - if isinstance(cached_response, bytes): - cached_response = cached_response.decode("utf-8") + response_text: Final = ( + cached_response.decode("utf-8") if isinstance(cached_response, bytes) else cached_response + ) # Convert string representation to Python object try: - cached_response = json.loads(cached_response) + return json.loads(response_text) except json.JSONDecodeError: try: - cached_response = ast.literal_eval(cached_response) + return ast.literal_eval(response_text) except (ValueError, SyntaxError) as e: print_verbose(f"Error parsing cached response: {e}") return None - return cached_response - def set_cache(self, key: str, value: object, **kwargs) -> None: """ Store a value in the semantic cache. @@ -494,7 +500,7 @@ class RedisSemanticCache(BaseCache): similarity: Final = 1 - vector_distance cached_prompt: Final = cache_hit["prompt"] - cached_response: Final = cache_hit["response"] + cached_response: Final = _CACHE_RESPONSE_ADAPTER.validate_python(cache_hit["response"]) # update kwargs["metadata"] with similarity, don't rewrite the original metadata kwargs.setdefault("metadata", {})["semantic-similarity"] = similarity @@ -645,7 +651,7 @@ class RedisSemanticCache(BaseCache): similarity: Final = 1 - vector_distance cached_prompt: Final = cache_hit["prompt"] - cached_response: Final = cache_hit["response"] + cached_response: Final = _CACHE_RESPONSE_ADAPTER.validate_python(cache_hit["response"]) # update kwargs["metadata"] with similarity, don't rewrite the original metadata kwargs.setdefault("metadata", {})["semantic-similarity"] = similarity diff --git a/litellm/cost_calculator.py b/litellm/cost_calculator.py index b5dc0ce13de..a4a9a5bee39 100644 --- a/litellm/cost_calculator.py +++ b/litellm/cost_calculator.py @@ -22,7 +22,7 @@ from litellm.constants import ( OPENAI_LIVE_TERMINAL_RESPONSE_EVENT_TYPES, REALTIME_METERED_USAGE_KEY, ) -from litellm.litellm_core_utils.core_helpers import as_str_mapping +from litellm.litellm_core_utils.core_helpers import as_str_mapping, billed_usage_cost from litellm.litellm_core_utils.cost_discount import resolve_cost_discount from litellm.litellm_core_utils.decisions_pricing import decisions_pricing_fields, is_decisions_call_type from litellm.litellm_core_utils.hidden_params import HIDDEN_PARAMS_ATTR @@ -50,6 +50,7 @@ from litellm.litellm_core_utils.llm_cost_calc.utils import ( parse_prompt_tokens_details, select_cost_metric_for_model, ) +from litellm.litellm_core_utils.llm_response_utils.response_metadata import response_ms_since_logging_start from litellm.llms.anthropic.cost_calculation import ( cost_per_token as anthropic_cost_per_token, ) @@ -1396,9 +1397,9 @@ def _apply_cost_margin( def _store_cost_breakdown_in_logging_obj( litellm_logging_obj: LitellmLoggingObject | None, - prompt_tokens_cost_usd_dollar: float, - completion_tokens_cost_usd_dollar: float, - cost_for_built_in_tools_cost_usd_dollar: float, + prompt_tokens_cost_usd_dollar: float | None, + completion_tokens_cost_usd_dollar: float | None, + cost_for_built_in_tools_cost_usd_dollar: float | None, total_cost_usd_dollar: float, additional_costs: dict | None = None, original_cost: float | None = None, @@ -1500,9 +1501,45 @@ def _response_time_ms_for_cost( return float(stamped) if total_time: return total_time - if litellm_logging_obj is not None: - return litellm_logging_obj.get_response_ms() - return 0.0 + if litellm_logging_obj is None: + return 0.0 + reported_ms: Final = response_ms_since_logging_start(litellm_logging_obj) + if reported_ms is not None and litellm_logging_obj.stream is not True: + return reported_ms + return litellm_logging_obj.get_response_ms() + + +def _adjusted_provider_billed_cost( + reported_cost: float, + custom_llm_provider: str | None, + model: str | None, + region_name: str | None, + litellm_logging_obj: LitellmLoggingObject | None, + service_tier: str | None, + data_residency: str | None, +) -> float: + discounted_cost, discount_percent, discount_amount = _apply_cost_discount( + base_cost=reported_cost, custom_llm_provider=custom_llm_provider, model=model, region_name=region_name + ) + adjusted_cost, margin_percent, margin_fixed_amount, margin_total_amount = _apply_cost_margin( + base_cost=discounted_cost, custom_llm_provider=custom_llm_provider + ) + _store_cost_breakdown_in_logging_obj( + litellm_logging_obj=litellm_logging_obj, + prompt_tokens_cost_usd_dollar=None, + completion_tokens_cost_usd_dollar=None, + cost_for_built_in_tools_cost_usd_dollar=None, + total_cost_usd_dollar=adjusted_cost, + original_cost=reported_cost, + discount_percent=discount_percent, + discount_amount=discount_amount, + margin_percent=margin_percent, + margin_fixed_amount=margin_fixed_amount, + margin_total_amount=margin_total_amount, + service_tier=service_tier, + data_residency=data_residency, + ) + return adjusted_cost def completion_cost( @@ -1616,6 +1653,9 @@ def completion_cost( ) explicit_pricing: Final = custom_pricing is True or base_model is not None + caller_set_prices: Final = ( + explicit_pricing or custom_cost_per_token is not None or custom_cost_per_second is not None + ) custom_priced_request_model: Final = model if custom_pricing is True else None configured_cost_per_query: Final = ( _configured_search_cost_per_query(litellm_logging_obj) if custom_pricing else None @@ -1666,6 +1706,7 @@ def completion_cost( raw_candidates if custom_pricing is True else _priced_candidates_before_unpriced_rules(raw_candidates) ) + priced_response: Final[object] = completion_response for idx, (candidate_model, candidate_pricing_provider) in enumerate(potential_model_names): model = candidate_model try: @@ -1984,6 +2025,19 @@ def completion_cost( total_time=total_time, ) model_for_discount = custom_priced_request_model or model + if ( + not caller_set_prices + and (reported_cost := billed_usage_cost(priced_response, custom_llm_provider)) is not None + ): + return _adjusted_provider_billed_cost( + reported_cost=reported_cost, + custom_llm_provider=custom_llm_provider, + model=model_for_discount, + region_name=region_name if isinstance(region_name, str) else None, + litellm_logging_obj=litellm_logging_obj, + service_tier=service_tier, + data_residency=data_residency, + ) # Calculate cost based on prompt_tokens, completion_tokens if ( "togethercomputer" in model or "together_ai" in model or custom_llm_provider == "together_ai" diff --git a/litellm/exceptions.py b/litellm/exceptions.py index cd5139f5f79..28211b3dab2 100644 --- a/litellm/exceptions.py +++ b/litellm/exceptions.py @@ -139,6 +139,7 @@ class AuthenticationError(openai.AuthenticationError): litellm_debug_info: str | None = None, max_retries: int | None = None, num_retries: int | None = None, + body: Mapping[str, object] | None = None, ): self.status_code = 401 self.message = f"litellm.AuthenticationError: {message}" @@ -152,7 +153,7 @@ class AuthenticationError(openai.AuthenticationError): request=httpx.Request(method="GET", url="https://litellm.ai"), # mock request object ) super().__init__( - self.message, response=self.response, body=None + self.message, response=self.response, body=body ) # Call the base class constructor with the parameters it needs def __str__(self): @@ -189,6 +190,7 @@ class NotFoundError(openai.NotFoundError): litellm_debug_info: str | None = None, max_retries: int | None = None, num_retries: int | None = None, + body: Mapping[str, object] | None = None, ): self.status_code = 404 self.message = f"litellm.NotFoundError: {message}" @@ -202,7 +204,7 @@ class NotFoundError(openai.NotFoundError): request=httpx.Request(method="GET", url="https://litellm.ai"), # mock request object ) super().__init__( - self.message, response=self.response, body=None + self.message, response=self.response, body=body ) # Call the base class constructor with the parameters it needs def __str__(self): @@ -321,6 +323,17 @@ class UnsupportedEndpointError(BadRequestError): pass +class EndpointNotSupportedByModelError(BadRequestError): + def __init__(self, message: str, model: str, llm_provider: str = "") -> None: + super().__init__( # pyright: ignore[reportUnknownMemberType] # inherited public exception constructor is untyped + message=message, model=model, llm_provider=llm_provider + ) + self.message = message + self.type = "invalid_request_error" + self.param = "model" + self.code = "400" + + class UnprocessableEntityError(openai.UnprocessableEntityError): def __init__( self, diff --git a/litellm/images/main.py b/litellm/images/main.py index 6e77cbb7046..4aad5210028 100644 --- a/litellm/images/main.py +++ b/litellm/images/main.py @@ -68,6 +68,11 @@ from litellm.utils import ( ) _PROVIDER_ROUTING_HINTS: Final = TypeAdapter(ProviderRoutingHints) +_PROVIDERS_RESOLVING_THEIR_OWN_IMAGE_BASE: Final = ( + LlmProviders.DASHSCOPE, + LlmProviders.QWENCLOUD, + LlmProviders.QWEN_AI_PLATFORM, +) # Cache for ImageEditRequestUtils to avoid repeated __getattr__ calls _ImageEditRequestUtils_cache: type["ImageEditRequestUtils"] | None = None @@ -271,6 +276,7 @@ def image_generation( """ try: args: Final = locals() + requested_api_base: Final = api_base aimg_generation: Final = kwargs.get("aimg_generation", False) litellm_call_id: Final = kwargs.get("litellm_call_id", None) logger_fn: Final = kwargs.get("logger_fn", None) @@ -468,7 +474,11 @@ def image_generation( raise ValueError(f"image generation config is not supported for {custom_llm_provider}") # Resolve api_base from litellm.api_base if not explicitly provided - _api_base: Final = api_base or litellm.api_base + _api_base: Final = ( + requested_api_base + if custom_llm_provider in _PROVIDERS_RESOLVING_THEIR_OWN_IMAGE_BASE + else api_base or litellm.api_base + ) litellm_params_dict["api_base"] = _api_base return llm_http_handler.image_generation_handler( @@ -641,6 +651,8 @@ def image_generation( timeout=timeout, client=custom_client, ) + elif custom_llm_provider == LlmProviders.KYMA.value: + raise LiteLLMUnknownProvider(model=model, custom_llm_provider=custom_llm_provider) return model_response except Exception as e: diff --git a/litellm/integrations/custom_logger.py b/litellm/integrations/custom_logger.py index a4de1be1842..0579eb15baf 100644 --- a/litellm/integrations/custom_logger.py +++ b/litellm/integrations/custom_logger.py @@ -513,7 +513,7 @@ class CustomLogger: # https://docs.litellm.ai/docs/observability/custom_callbac data: dict, user_api_key_dict: UserAPIKeyAuth, call_type: CallTypesLiteral, - ) -> Any: + ) -> object: pass async def async_post_call_streaming_hook( diff --git a/litellm/integrations/datadog/datadog_cost_management.py b/litellm/integrations/datadog/datadog_cost_management.py index 1a81fa019dc..d4b75fc0bce 100644 --- a/litellm/integrations/datadog/datadog_cost_management.py +++ b/litellm/integrations/datadog/datadog_cost_management.py @@ -205,11 +205,12 @@ class DatadogCostManagementLogger(CustomBatchLogger): # see _RESERVED_TAG_KEYS for the rationale. if self.cost_tag_keys: allow: Final = set(self.cost_tag_keys) + client_allow: Final = allow - {"is_silent_experiment"} for rt in log.get("request_tags") or []: if not isinstance(rt, str) or ":" not in rt: continue k, _, v = rt.partition(":") - if k in allow and v: + if k in client_allow and v: self._set_custom_tag(tags, k, v) for k, v in metadata.items(): if k in allow and v is not None and not isinstance(v, (dict, list)): @@ -218,7 +219,7 @@ class DatadogCostManagementLogger(CustomBatchLogger): nested = metadata.get(nested_key) if isinstance(nested, dict): for k, v in nested.items(): - if k in allow and v is not None and not isinstance(v, (dict, list)): + if k in client_allow and v is not None and not isinstance(v, (dict, list)): self._set_custom_tag(tags, k, str(v)) return tags diff --git a/litellm/integrations/langfuse/langfuse.py b/litellm/integrations/langfuse/langfuse.py index b8c6e91d0d7..bd68ffe8cd6 100644 --- a/litellm/integrations/langfuse/langfuse.py +++ b/litellm/integrations/langfuse/langfuse.py @@ -833,6 +833,12 @@ class LangFuseLogger: ("vertex_location", vertex_location, bool(vertex_location)), ("aws_region_name", aws_region_name, bool(aws_region_name)), ("cache_hit", kwargs.get("cache_hit") or False, "cache_hit" in kwargs), + ( + "is_silent_experiment", + "is_silent_experiment" in allowlisted_metadata + and allowlisted_metadata["is_silent_experiment"] is True, + "is_silent_experiment" in allowlisted_metadata, + ), ) enrichments: Final[Mapping[str, object]] = { key: value for key, value, include in candidate_enrichments if include diff --git a/litellm/integrations/opentelemetry_utils/base_otel_llm_obs_attributes.py b/litellm/integrations/opentelemetry_utils/base_otel_llm_obs_attributes.py index 32cf93b7937..97a970c8966 100644 --- a/litellm/integrations/opentelemetry_utils/base_otel_llm_obs_attributes.py +++ b/litellm/integrations/opentelemetry_utils/base_otel_llm_obs_attributes.py @@ -15,7 +15,7 @@ class BaseLLMObsOTELAttributes(ABC): pass -def cast_as_primitive_value_type(value) -> str | bool | int | float: +def cast_as_primitive_value_type(value: object) -> str | bool | int | float: """ Converts a value to an OTEL-supported primitive for Arize/Phoenix observability. """ diff --git a/litellm/integrations/prometheus.py b/litellm/integrations/prometheus.py index d15956c0e95..e62b99f8b0f 100644 --- a/litellm/integrations/prometheus.py +++ b/litellm/integrations/prometheus.py @@ -5148,6 +5148,12 @@ def _get_combined_custom_metadata_from_standard_logging_payload( **(requester_metadata if isinstance(requester_metadata, dict) else {}), **(user_api_key_auth_metadata if isinstance(user_api_key_auth_metadata, dict) else {}), **(spend_logs_metadata if isinstance(spend_logs_metadata, dict) else {}), + "is_silent_experiment": ( + "true" + if "is_silent_experiment" in standard_logging_metadata + and standard_logging_metadata["is_silent_experiment"] is True + else None + ), } diff --git a/litellm/integrations/websearch_interception/handler.py b/litellm/integrations/websearch_interception/handler.py index 6e1ba3eea08..c25fcfe3e70 100644 --- a/litellm/integrations/websearch_interception/handler.py +++ b/litellm/integrations/websearch_interception/handler.py @@ -37,6 +37,7 @@ from litellm.integrations.websearch_interception.transformation import ( from litellm.litellm_core_utils.agentic_loop_settings import ( validated_max_agentic_loops, ) +from litellm.litellm_core_utils.core_helpers import qualify_provider_stripped_model from litellm.llms.base_llm.search.transformation import SearchResponse from litellm.types.integrations.custom_logger import ( CHAT_COMPLETION_AGENTIC_SURFACE, @@ -114,6 +115,18 @@ class _WebSearchSettingsView(TypedDict): websearch_interception_params: WebSearchInterceptionConfig +def _caller_model(model: str, logging_obj: "LiteLLMLoggingObj | None", kwargs: Mapping[str, object]) -> str: + agentic_view: Final[_AgenticLoopParamsView] = { + "agentic_loop_params": logging_obj.model_call_details.get("agentic_loop_params", {}) if logging_obj else {} + } + recorded_model: Final = agentic_view["agentic_loop_params"].get("model") + custom_llm_provider: Final = kwargs.get("custom_llm_provider") + return qualify_provider_stripped_model( + recorded_model if isinstance(recorded_model, str) and recorded_model else model, + custom_llm_provider if isinstance(custom_llm_provider, str) else "", + ) + + class _SearchToolLitellmParams(TypedDict, total=False): search_provider: ReadOnly[str | None] @@ -1155,6 +1168,7 @@ class WebSearchInterceptionLogger(CustomLogger): tool_calls=tool_calls, optional_params=optional_params, kwargs=kwargs, + logging_obj=logging_obj, response_format=response_format, ) return AgenticLoopPlan( @@ -1181,6 +1195,7 @@ class WebSearchInterceptionLogger(CustomLogger): tool_calls=tool_calls, optional_params=optional_params, kwargs=kwargs, + logging_obj=logging_obj, ) return AgenticLoopPlan( run_agentic_loop=True, @@ -1195,6 +1210,7 @@ class WebSearchInterceptionLogger(CustomLogger): tool_calls: list[dict], optional_params: dict, kwargs: dict, + logging_obj: "LiteLLMLoggingObj | None", ) -> AgenticLoopRequestPatch: """Execute litellm.asearch() and build a Responses API rerun patch.""" search_tasks: Final = [ @@ -1254,9 +1270,7 @@ class WebSearchInterceptionLogger(CustomLogger): } } - full_model_name = model - if "/" not in model and isinstance(kwargs.get("custom_llm_provider"), str): - full_model_name = f"{kwargs['custom_llm_provider']}/{model}" + full_model_name: Final = _caller_model(model, logging_obj, kwargs) verbose_logger.debug( "WebSearchInterception: Built responses request patch model=%s input_items=%d searches=%d", @@ -1814,6 +1828,7 @@ class WebSearchInterceptionLogger(CustomLogger): tool_calls=tool_calls, optional_params=dict[str, object](optional_params), kwargs=dict[str, object](kwargs), + logging_obj=logging_obj, response_format=response_format, ) if request_patch.messages is None: @@ -1836,7 +1851,8 @@ class WebSearchInterceptionLogger(CustomLogger): messages: list[dict], tool_calls: list[dict], optional_params: dict, - kwargs: dict, + kwargs: dict[str, object], + logging_obj: "LiteLLMLoggingObj | None", response_format: str = "openai", ) -> AgenticLoopRequestPatch: """Execute litellm.search() and build chat-completion rerun patch.""" @@ -1922,11 +1938,7 @@ class WebSearchInterceptionLogger(CustomLogger): k: v for k, v in kwargs.items() if not k.startswith("_websearch_interception") and k not in internal_params } - full_model_name = model - if "custom_llm_provider" in kwargs: - custom_llm_provider: Final = kwargs["custom_llm_provider"] - if not model.startswith(custom_llm_provider) and "/" not in model: - full_model_name = f"{custom_llm_provider}/{model}" + full_model_name: Final = _caller_model(model, logging_obj, kwargs) verbose_logger.debug( "WebSearchInterception: Built chat completion request patch model=%s messages=%d", diff --git a/litellm/interactions/litellm_responses_transformation/handler.py b/litellm/interactions/litellm_responses_transformation/handler.py index d84f0b5ffbd..f95b5611831 100644 --- a/litellm/interactions/litellm_responses_transformation/handler.py +++ b/litellm/interactions/litellm_responses_transformation/handler.py @@ -103,6 +103,7 @@ class LiteLLMResponsesInteractionsHandler: return LiteLLMResponsesInteractionsConfig.transform_responses_response_to_interactions_response( responses_response=responses_api_response, model=model, + custom_llm_provider=custom_llm_provider, ) async def async_interactions_api_handler( @@ -138,4 +139,5 @@ class LiteLLMResponsesInteractionsHandler: return LiteLLMResponsesInteractionsConfig.transform_responses_response_to_interactions_response( responses_response=responses_api_response, model=model, + custom_llm_provider=responses_request.get("custom_llm_provider"), ) diff --git a/litellm/interactions/litellm_responses_transformation/transformation.py b/litellm/interactions/litellm_responses_transformation/transformation.py index ade33831cdc..2e9b62dcfe2 100644 --- a/litellm/interactions/litellm_responses_transformation/transformation.py +++ b/litellm/interactions/litellm_responses_transformation/transformation.py @@ -12,6 +12,7 @@ from typing import Final, cast from pydantic import BaseModel +from litellm.litellm_core_utils.core_helpers import billed_usage_cost from litellm.types.interactions import ( InteractionInput, InteractionsAPIOptionalRequestParams, @@ -190,6 +191,7 @@ class LiteLLMResponsesInteractionsConfig: def transform_responses_response_to_interactions_response( responses_response: ResponsesAPIResponse, model: str | None = None, + custom_llm_provider: str | None = None, ) -> InteractionsAPIResponse: """ Transform a Responses API response to an Interactions API response. @@ -266,9 +268,11 @@ class LiteLLMResponsesInteractionsConfig: # (total_input_tokens, total_output_tokens) usage: Final[object] = getattr(responses_response, "usage", None) if usage: + reported_cost: Final = billed_usage_cost(responses_response, custom_llm_provider) interactions_response_dict["usage"] = { "total_input_tokens": getattr(usage, "input_tokens", 0), "total_output_tokens": getattr(usage, "output_tokens", 0), + **({} if reported_cost is None else {"cost": reported_cost}), } # Add updated (same as created for now) diff --git a/litellm/litellm_core_utils/chat_completion_agentic_loop.py b/litellm/litellm_core_utils/chat_completion_agentic_loop.py index 628486f377f..2ba47cbb400 100644 --- a/litellm/litellm_core_utils/chat_completion_agentic_loop.py +++ b/litellm/litellm_core_utils/chat_completion_agentic_loop.py @@ -15,6 +15,7 @@ from litellm.litellm_core_utils.agentic_loop_settings import ( DEFAULT_MAX_AGENTIC_LOOPS, validated_max_agentic_loops, ) +from litellm.litellm_core_utils.core_helpers import qualify_agentic_followup_model from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObject from litellm.llms.base_llm.base_model_iterator import MockResponseIterator from litellm.types.integrations.custom_logger import ( @@ -182,9 +183,7 @@ async def _execute_chat_completion_agentic_plan( if patch.messages is None: raise ValueError("Agentic loop plan missing patched messages") - full_model_name = patch.model or model - if "/" not in full_model_name: - full_model_name = f"{custom_llm_provider}/{full_model_name}" + full_model_name: Final = qualify_agentic_followup_model(patch.model, model, custom_llm_provider) optional_params_for_followup: Final = {**optional_params, **patch.optional_params} if patch.tools is not None: diff --git a/litellm/litellm_core_utils/core_helpers.py b/litellm/litellm_core_utils/core_helpers.py index 55025508e40..d940baf8b56 100644 --- a/litellm/litellm_core_utils/core_helpers.py +++ b/litellm/litellm_core_utils/core_helpers.py @@ -2,6 +2,7 @@ ## Helper utilities import copy import logging +import math import re from collections.abc import Collection, Iterable, Iterator, Mapping from types import MappingProxyType @@ -34,6 +35,49 @@ def is_codex_user_agent(user_agent: str) -> bool: return bool(_CODEX_CLIENT_PREFIX_RE.match(user_agent)) +def qualify_provider_stripped_model(model: str, custom_llm_provider: str) -> str: + """Put the provider prefix back on a provider-stripped model.""" + if not custom_llm_provider or model.startswith(f"{custom_llm_provider}/"): + return model + if _already_routes_to(model, custom_llm_provider): + return model + return f"{custom_llm_provider}/{model}" + + +def _already_routes_to(model: str, custom_llm_provider: str) -> bool: + from litellm.litellm_core_utils.get_llm_provider_logic import inferred_provider + + return _leads_with_provider(model) and inferred_provider(model) == custom_llm_provider + + +def _leads_with_provider(model: str) -> bool: + leading_segment, separator, _ = model.partition("/") + return bool(separator) and names_a_provider(leading_segment) + + +def names_a_provider(candidate: str) -> bool: + """Whether a model name's leading segment is a provider rather than part of the model.""" + import litellm + from litellm.litellm_core_utils.get_llm_provider_logic import is_registered_custom_provider + from litellm.llms.openai_like.json_loader import JSONProviderRegistry + + provider_names: Final = {getattr(provider, "value", provider) for provider in litellm.provider_list} + return ( + candidate in provider_names + or JSONProviderRegistry.exists(candidate) + or is_registered_custom_provider(candidate) + ) + + +def qualify_agentic_followup_model(patch_model: str | None, model: str, custom_llm_provider: str) -> str: + """Resolve the model an agentic follow-up re-dispatches, keeping a hook's own provider.""" + if not patch_model or patch_model == model: + return qualify_provider_stripped_model(model, custom_llm_provider) + if _leads_with_provider(patch_model): + return patch_model + return qualify_provider_stripped_model(patch_model, custom_llm_provider) + + def safe_divide_seconds(seconds: float, denominator: float, default: float | None = None) -> float | None: """ Safely divide seconds by denominator, handling zero division. @@ -973,6 +1017,16 @@ def chunk_usage(chunk: object) -> object: return getattr(chunk, "usage", None) +USAGE_COST_BILLED_PROVIDERS: Final = frozenset({"kyma"}) + + +def billed_usage_cost(response: object, custom_llm_provider: str | None) -> float | None: + if custom_llm_provider not in USAGE_COST_BILLED_PROVIDERS: + return None + cost: Final = provider_reported_usage_cost(_usage_field(response, "usage"), custom_llm_provider) + return cost if cost is not None and math.isfinite(cost) and cost >= 0 else None + + _MESSAGES_REPORTED_COST_PROVIDERS: Final = frozenset({"opencode_go"}) diff --git a/litellm/litellm_core_utils/exception_mapping_utils.py b/litellm/litellm_core_utils/exception_mapping_utils.py index 703550dd2b5..64a95eb5f28 100644 --- a/litellm/litellm_core_utils/exception_mapping_utils.py +++ b/litellm/litellm_core_utils/exception_mapping_utils.py @@ -413,6 +413,25 @@ def _map_openai_exception( body=getattr(original_exception, "body", None), ) elif "invalid_request_error" in error_str and "Incorrect API key provided" not in error_str: + invalid_request_status: Final = getattr(original_exception, "status_code", None) + if invalid_request_status == 401: + raise AuthenticationError( + message=f"{exception_provider} - {message}", + llm_provider=custom_llm_provider, + model=model, + response=response, + litellm_debug_info=extra_information, + body=getattr(original_exception, "body", None), + ) + if invalid_request_status == 404: + raise NotFoundError( + message=f"{exception_provider} - {message}", + model=model, + llm_provider=custom_llm_provider, + response=response, + litellm_debug_info=extra_information, + body=getattr(original_exception, "body", None), + ) raise BadRequestError( message=f"{exception_provider} - {message}", llm_provider=custom_llm_provider, diff --git a/litellm/litellm_core_utils/health_check_helpers.py b/litellm/litellm_core_utils/health_check_helpers.py index b19bcd941fa..940730eeb9f 100644 --- a/litellm/litellm_core_utils/health_check_helpers.py +++ b/litellm/litellm_core_utils/health_check_helpers.py @@ -31,7 +31,13 @@ EXTRACTION_HEALTH_CHECK_FIELDS: Final[Mapping[str, Mapping[str, str]]] = Mapping ) -def native_health_check_mode(model: str, custom_llm_provider: str | None) -> Literal["anthropic_messages"] | None: +def native_health_check_mode( + model: str, custom_llm_provider: str | None +) -> Literal["anthropic_messages", "realtime"] | None: + if custom_llm_provider == "soniox": + from litellm.llms.soniox.common_utils import soniox_health_check_mode + + return soniox_health_check_mode(model) if custom_llm_provider != "bedrock_mantle": return None from litellm.llms.bedrock_mantle.common_utils import mantle_health_check_mode diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py index 7f4b069ded6..c0e988412a1 100644 --- a/litellm/litellm_core_utils/litellm_logging.py +++ b/litellm/litellm_core_utils/litellm_logging.py @@ -23,7 +23,7 @@ from httpx import Response, Timeout from pydantic import BaseModel, JsonValue, TypeAdapter import litellm -from litellm._internal_context import post_response_phase +from litellm._internal_context import in_mirrored_call, outside_mirrored_call, post_response_phase from litellm._logging import ( is_debugging_on, redact_string, @@ -162,9 +162,10 @@ from litellm.types.llms.openai import ( from litellm.types.mcp import MCPPostCallResponseObject from litellm.types.prompts.init_prompts import PromptSpec from litellm.types.rerank import RerankResponse -from litellm.types.router import RouterRateLimitError +from litellm.types.router import RouterRateLimitError, SilentExperimentFlag from litellm.types.utils import ( DEPLOYMENT_SCOPED_PRICING_FIELDS, + AgenticLoopParams, CachingDetails, CallTypes, CostBreakdown, @@ -314,7 +315,7 @@ else: _in_memory_loggers: Final[list[CustomLogger]] = [] _STANDARD_LOGGING_METADATA_RESOLVED_KEYS: Final[frozenset[str]] = frozenset( - ("used_client_oauth_token", "usage_object", KUBERNETES_POD_ROUTING_KEY) + ("used_client_oauth_token", "usage_object", KUBERNETES_POD_ROUTING_KEY, "is_silent_experiment") ) _STANDARD_LOGGING_METADATA_KEYS: Final[frozenset[str]] = ( frozenset(StandardLoggingMetadata.__annotations__.keys()) - _STANDARD_LOGGING_METADATA_RESOLVED_KEYS @@ -808,6 +809,7 @@ class Logging(LiteLLMLoggingBaseClass): litellm_params = scrub_sensitive_keys_in_metadata(litellm_params) self.litellm_params = litellm_params + self.is_silent_experiment: Final = in_mirrored_call() # Initialize cost breakdown field self.cost_breakdown: CostBreakdown | None = None @@ -1158,6 +1160,9 @@ class Logging(LiteLLMLoggingBaseClass): if "custom_llm_provider" in self.model_call_details: self.custom_llm_provider = self.model_call_details["custom_llm_provider"] + def record_agentic_loop_params(self, params: AgenticLoopParams) -> None: + self.model_call_details["agentic_loop_params"] = params + def record_applied_timeout(self, timeout: float | Timeout) -> None: self.applied_timeout = timeout self.litellm_params["timeout"] = timeout @@ -1937,10 +1942,10 @@ class Logging(LiteLLMLoggingBaseClass): def set_cost_breakdown( self, - input_cost: float, - output_cost: float, + input_cost: float | None, + output_cost: float | None, total_cost: float, - cost_for_built_in_tools_cost_usd_dollar: float, + cost_for_built_in_tools_cost_usd_dollar: float | None, additional_costs: dict | None = None, original_cost: float | None = None, discount_percent: float | None = None, @@ -1979,14 +1984,17 @@ class Logging(LiteLLMLoggingBaseClass): self.billed_token_rates = billed_token_rates self.cost_breakdown = CostBreakdown( - input_cost=input_cost, - output_cost=output_cost, total_cost=total_cost, - tool_usage_cost=cost_for_built_in_tools_cost_usd_dollar, service_tier=service_tier, data_residency=data_residency, vertex_location=vertex_location, ) + if input_cost is not None: + self.cost_breakdown["input_cost"] = input_cost + if output_cost is not None: + self.cost_breakdown["output_cost"] = output_cost + if cost_for_built_in_tools_cost_usd_dollar is not None: + self.cost_breakdown["tool_usage_cost"] = cost_for_built_in_tools_cost_usd_dollar if cache_read_cost is not None and cache_read_cost > 0: self.cost_breakdown["cache_read_cost"] = cache_read_cost if cache_creation_cost is not None and cache_creation_cost > 0: @@ -3249,7 +3257,7 @@ class Logging(LiteLLMLoggingBaseClass): """Restores trace_id/session_id contextvars once this attempt's own success logging (including any nested calls its callbacks trigger) is fully done.""" try: - with post_response_phase(): + with post_response_phase(), outside_mirrored_call(): return self._success_handler_body( result=result, start_time=start_time, @@ -3749,7 +3757,7 @@ class Logging(LiteLLMLoggingBaseClass): """Restores trace_id/session_id contextvars once this attempt's own success logging (including any nested calls its callbacks trigger) is fully done.""" try: - with post_response_phase(): + with post_response_phase(), outside_mirrored_call(): return await self._async_success_handler_body( result=result, start_time=start_time, end_time=end_time, cache_hit=cache_hit, **kwargs ) @@ -4266,13 +4274,14 @@ class Logging(LiteLLMLoggingBaseClass): ``for_async_request`` marks the sync run of a failure whose async run also logs it, so a CustomLogger that gets ``async_log_failure_event`` from that async run skips its sync hook.""" try: - return self._failure_handler_body( - exception=exception, - traceback_exception=traceback_exception, - start_time=start_time, - end_time=end_time, - for_async_request=for_async_request, - ) + with outside_mirrored_call(): + return self._failure_handler_body( + exception=exception, + traceback_exception=traceback_exception, + start_time=start_time, + end_time=end_time, + for_async_request=for_async_request, + ) finally: self._restore_correlation_context() @@ -4465,12 +4474,13 @@ class Logging(LiteLLMLoggingBaseClass): """Restores trace_id/session_id contextvars once this attempt's own failure logging (including any nested calls its callbacks trigger) is fully done.""" try: - return await self._async_failure_handler_body( - exception=exception, - traceback_exception=traceback_exception, - start_time=start_time, - end_time=end_time, - ) + with outside_mirrored_call(): + return await self._async_failure_handler_body( + exception=exception, + traceback_exception=traceback_exception, + start_time=start_time, + end_time=end_time, + ) finally: self._restore_correlation_context() @@ -6559,6 +6569,7 @@ class StandardLoggingPayloadSetup: start_time: dt_object | None = None, response_id: str | None = None, custom_llm_provider: str | None = None, + is_silent_experiment: bool = False, ) -> StandardLoggingMetadata: """ Clean and filter the metadata dictionary to include only the specified keys in StandardLoggingMetadata. @@ -6632,6 +6643,7 @@ class StandardLoggingPayloadSetup: proxy_stamped_used_client_oauth_token(metadata, litellm_params), custom_llm_provider, ), + **(SilentExperimentFlag(is_silent_experiment=True) if is_silent_experiment else SilentExperimentFlag()), ) if isinstance(metadata, dict): for key in metadata.keys() & _STANDARD_LOGGING_METADATA_KEYS: @@ -7432,6 +7444,7 @@ def get_standard_logging_object_payload( start_time=start_time, response_id=id, custom_llm_provider=selected_provider if isinstance(selected_provider, str) else None, + is_silent_experiment=logging_obj.is_silent_experiment is True, ) _request_body: Final = proxy_server_request.get("body", {}) end_user_id: Final = clean_metadata["user_api_key_end_user_id"] or _request_body.get( @@ -7716,9 +7729,8 @@ def get_standard_logging_metadata( ) if isinstance(metadata, dict): # Update the clean_metadata with values from input metadata that match StandardLoggingMetadata fields - for key in StandardLoggingMetadata.__annotations__: - if key in metadata: - clean_metadata[key] = metadata[key] + for key in (metadata.keys() & StandardLoggingMetadata.__annotations__.keys()) - {"is_silent_experiment"}: + clean_metadata[key] = metadata[key] if metadata.get("user_api_key") is not None: if is_valid_sha256_hash(str(metadata.get("user_api_key"))): diff --git a/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py b/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py index 8b94c34942c..58fe5b4297f 100644 --- a/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py +++ b/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py @@ -3,7 +3,7 @@ import json import re import time import traceback -from collections.abc import Mapping, Sequence +from collections.abc import Generator, Iterator, Mapping, Sequence from typing import Final, Literal, cast from pydantic import ConfigDict, TypeAdapter, ValidationError, with_config @@ -19,7 +19,7 @@ from litellm.litellm_core_utils.prompt_templates.common_utils import ( from litellm.llms.ollama.common_utils import OllamaResponseIdNormalizer from litellm.types.llms.databricks import DatabricksTool from litellm.types.llms.openai import ( - ChatCompletionReasoningItem, + ChatCompletionAnnotation, ChatCompletionThinkingBlock, ImageURLListItem, OpenAIModerationResponse, @@ -59,6 +59,31 @@ _CHOICES_FIELDS: Final[frozenset] = frozenset(Choices.model_fields.keys()) _MODEL_RESPONSE_FIELDS: Final[frozenset] = frozenset(ModelResponse.model_fields.keys()) | {"usage"} +_ANNOTATION_ENTRIES: Final = TypeAdapter(tuple[object, ...]) + + +def _is_provider_specific_annotation(annotation: object) -> bool: + match annotation: + case {"type": "url_citation"}: + return False + case {"type": _}: + return True + case _: + return False + + +def _annotation_entries(annotations: object) -> tuple[object, ...]: + return _ANNOTATION_ENTRIES.validate_python(annotations) if isinstance(annotations, list) else () + + +def partition_annotations(annotations: object) -> tuple[object, tuple[object, ...]]: + entries: Final = _annotation_entries(annotations) + others: Final = tuple(annotation for annotation in entries if _is_provider_specific_annotation(annotation)) + if not others: + return annotations, () + return [annotation for annotation in entries if not _is_provider_specific_annotation(annotation)] or None, others + + def _normalize_images_for_message( images: list[dict] | None, ) -> list[ImageURLListItem] | None: @@ -145,31 +170,89 @@ def _split_assembled_content_for_replay(content: str | None) -> list[str]: return _REPLAY_CONTENT_SLICE_RE.findall(content) -def _clear_later_replay_slice_metadata(choice: StreamingChoices) -> None: - # Rebuild the delta as content-only so every accumulate-able field (role, - # tool_calls, reasoning_content, thinking_blocks, audio, images, - # annotations, ...) is dropped on later slices instead of an enumerated - # subset; repeating any of them makes downstream handlers that accumulate - # streamed deltas collect it once per slice, and a field added to Delta - # later can't silently re-introduce the duplication. - choice.delta = Delta(content=choice.delta.content) - choice.logprobs = None - if hasattr(choice, "enhancements"): - del choice.enhancements +_REPLAY_REASONING_FIELDS: Final = frozenset({"reasoning_content", "thinking_blocks", "reasoning_items"}) +_REPLAY_LEAD_FIELDS: Final = _REPLAY_REASONING_FIELDS | {"role", "provider_specific_fields"} +_REPLAY_TOOL_FIELDS: Final = frozenset({"tool_calls", "function_call"}) +_REPLAY_TEXT_FIELDS: Final = frozenset({"content", "annotations"}) -def _first_replay_delta( - message: Mapping[str, object], reasoning_items: Sequence[ChatCompletionReasoningItem] | None -) -> Delta: - return Delta( - content=message.get("content"), - role=message["role"], - function_call=message.get("function_call"), - tool_calls=message.get("tool_calls"), - reasoning_items=None if reasoning_items is None else list(reasoning_items), - provider_specific_fields=_cached_provider_specific_fields( - _CACHED_MESSAGE_PROVIDER_FIELDS.validate_python(message) - ), +def _fields_in(fields: Mapping[str, object], keys: frozenset[str]) -> dict[str, object]: + return {key: value for key, value in fields.items() if key in keys} + + +def _replay_deltas(delta: Delta) -> tuple[Delta, ...]: + fields: Final[dict[str, object]] = delta.model_dump(exclude_none=True) + lead: Final = _fields_in(fields, _REPLAY_LEAD_FIELDS) if _REPLAY_REASONING_FIELDS.intersection(fields) else {} + pieces: Final = _split_assembled_content_for_replay(delta.content) or ([delta.content] if delta.content else []) + if not pieces and not lead: + return (delta,) + if not pieces: + held_back: Final = lead.keys() | {"content"} + rest: Final = {key: value for key, value in fields.items() if key not in held_back} + return (Delta.model_validate(lead),) + ((Delta.model_validate(rest),) if rest else ()) + tools: Final = _fields_in(fields, _REPLAY_TOOL_FIELDS) + annotations: Final = _fields_in(fields, frozenset({"annotations"})) + moved: Final = lead.keys() | _REPLAY_TOOL_FIELDS | _REPLAY_TEXT_FIELDS + first_slice: Final = {key: value for key, value in fields.items() if key not in moved} + last_index: Final = len(pieces) - 1 + return ( + ((Delta.model_validate(lead),) if lead else ()) + + tuple( + Delta.model_validate( + { + **(first_slice if index == 0 else {}), + "content": piece, + **(annotations if index == last_index else {}), + } + ) + for index, piece in enumerate(pieces) + ) + + ((Delta.model_validate(tools),) if tools else ()) + ) + + +def _replay_frame( + response: ModelResponseStream, delta: Delta, finish_reason: str | None, keeps_choice_metadata: bool +) -> ModelResponseStream: + frame: Final = response.model_copy(deep=True) + choice: Final = frame.choices[0] + choice.delta = delta + choice.finish_reason = finish_reason + if not keeps_choice_metadata: + choice.logprobs = None + if hasattr(choice, "enhancements"): + del choice.enhancements + return frame + + +def _replay_frames(response: ModelResponseStream) -> Iterator[ModelResponseStream]: + if len(response.choices) != 1: + yield response + return + usage: Final = getattr(response, "usage", None) + if usage is not None: + delattr(response, "usage") + choice: Final = response.choices[0] + deltas: Final = _replay_deltas(choice.delta) + first_text: Final = next((index for index, delta in enumerate(deltas) if delta.content), 0) + for index, delta in enumerate(deltas): + yield _replay_frame(response, delta, finish_reason=None, keeps_choice_metadata=index == first_text) + finish_frame: Final = _replay_frame( + response, Delta(), finish_reason=choice.finish_reason, keeps_choice_metadata=False + ) + if usage is not None: + setattr(finish_frame, "usage", usage) + yield finish_frame + + +def _first_replay_delta(message: Mapping[str, object]) -> Delta: + return Delta.model_validate( + { + **message, + "provider_specific_fields": _cached_provider_specific_fields( + _CACHED_MESSAGE_PROVIDER_FIELDS.validate_python(message) + ), + } ) @@ -234,7 +317,7 @@ async def convert_to_streaming_response_async( t["index"] = index pydantic_tool_calls.append(ChatCompletionDeltaToolCall(**t)) choice["message"]["tool_calls"] = pydantic_tool_calls - delta = _first_replay_delta(choice["message"], choice["message"].get("reasoning_items")) + delta = _first_replay_delta(choice["message"]) finish_reason = choice.get("finish_reason", None) if finish_reason is None: @@ -266,41 +349,14 @@ async def convert_to_streaming_response_async( if "model" in response_object: model_response_object.model = response_object["model"] - # Replay cached content with per-word cadence so stream=true cache hits - # don't arrive as a single SSE frame. Multi-choice (n>1) responses and - # unsplittable content (None/empty/whitespace-free) keep the original - # single-yield behavior. - slices: list[str] = [] - if len(model_response_object.choices) == 1: - slices = _split_assembled_content_for_replay(model_response_object.choices[0].delta.content) - if len(slices) <= 1: - yield model_response_object - await asyncio.sleep(0) - return - - # Detach usage from the base object so we can re-attach it only to the - # final slice chunk. A non-None usage always lives in __pydantic_extra__ - # here (set via setattr above), so delattr cannot fail. - original_usage: Final = getattr(model_response_object, "usage", None) - if original_usage is not None: - delattr(model_response_object, "usage") - original_finish_reason: Final = model_response_object.choices[0].finish_reason - last_idx: Final = len(slices) - 1 - for i, piece in enumerate(slices): - slice_chunk = model_response_object.model_copy(deep=True) - slice_chunk.choices[0].delta.content = piece - if i > 0: - _clear_later_replay_slice_metadata(slice_chunk.choices[0]) - slice_chunk.choices[0].finish_reason = original_finish_reason if i == last_idx else None - if i == last_idx and original_usage is not None: - setattr(slice_chunk, "usage", original_usage) - yield slice_chunk + for frame in _replay_frames(model_response_object): + yield frame await asyncio.sleep(0) def convert_to_streaming_response( response_object: dict | None = None, -): +) -> Generator[ModelResponseStream, None, None]: # used for yielding Cache hits when stream == True if response_object is None: raise Exception("Error in response object format") @@ -356,30 +412,7 @@ def convert_to_streaming_response( if "model" in response_object: model_response_object.model = response_object["model"] - # Replay cached content with per-word cadence on sync cache-hit paths - # (S3Cache, sync completion()). See convert_to_streaming_response_async - # for the full rationale — this mirrors its tail. - slices: list[str] = [] - if len(model_response_object.choices) == 1: - slices = _split_assembled_content_for_replay(model_response_object.choices[0].delta.content) - if len(slices) <= 1: - yield model_response_object - return - - original_usage: Final = getattr(model_response_object, "usage", None) - if original_usage is not None: - delattr(model_response_object, "usage") - original_finish_reason: Final = model_response_object.choices[0].finish_reason - last_idx: Final = len(slices) - 1 - for i, piece in enumerate(slices): - slice_chunk = model_response_object.model_copy(deep=True) - slice_chunk.choices[0].delta.content = piece - if i > 0: - _clear_later_replay_slice_metadata(slice_chunk.choices[0]) - slice_chunk.choices[0].finish_reason = original_finish_reason if i == last_idx else None - if i == last_idx and original_usage is not None: - setattr(slice_chunk, "usage", original_usage) - yield slice_chunk + yield from _replay_frames(model_response_object) from collections import defaultdict @@ -787,6 +820,9 @@ def convert_to_model_response_object( provider_specific_fields = dict(choice["message"].get("provider_specific_fields", None) or {}) for f in choice["message"].keys() - _MESSAGE_FIELDS: provider_specific_fields[f] = choice["message"][f] + annotations, other_annotations = partition_annotations(choice["message"].get("annotations", None)) + if other_annotations: + provider_specific_fields["annotations"] = list(other_annotations) # Handle reasoning models that display `reasoning_content` within `content` reasoning_content, content = extract_reasoning_content(choice["message"]) @@ -809,7 +845,9 @@ def convert_to_model_response_object( reasoning_content=reasoning_content, thinking_blocks=thinking_blocks, reasoning_items=choice["message"].get("reasoning_items", None), - annotations=choice["message"].get("annotations", None), + annotations=cast( # cast-ok: provider annotation dicts pass through whole, keeping keys such as url_citation.content + list[ChatCompletionAnnotation] | None, annotations + ), images=_normalize_images_for_message(choice["message"].get("images", None)), ) finish_reason = choice.get("finish_reason", None) diff --git a/litellm/litellm_core_utils/llm_response_utils/response_metadata.py b/litellm/litellm_core_utils/llm_response_utils/response_metadata.py index ace57a64aae..051a8fdf256 100644 --- a/litellm/litellm_core_utils/llm_response_utils/response_metadata.py +++ b/litellm/litellm_core_utils/llm_response_utils/response_metadata.py @@ -27,6 +27,15 @@ def _timing_window_start( return start_time, False +def response_ms_since_logging_start(logging_obj: LiteLLMLoggingObject) -> float | None: + reported_ms: Final = logging_obj.response_timing_metrics.get("_response_ms") + if reported_ms is None: + return None + logging_start: Final = logging_obj.start_time + window_start: Final = _timing_window_start(logging_start, logging_obj)[0] + return reported_ms - max(0.0, logging_start.timestamp() - window_start.timestamp()) * 1000 + + def _union_duration_ms(windows: object, lower: float, upper: float) -> float | None: if not isinstance(windows, (list, tuple)): return None diff --git a/litellm/litellm_core_utils/logging_worker.py b/litellm/litellm_core_utils/logging_worker.py index ff649953e69..8f217b1ea5e 100644 --- a/litellm/litellm_core_utils/logging_worker.py +++ b/litellm/litellm_core_utils/logging_worker.py @@ -6,7 +6,7 @@ import atexit import contextvars import inspect import logging -from collections.abc import Coroutine, Iterator +from collections.abc import Awaitable, Callable, Coroutine, Iterator from typing import Final from typing_extensions import TypedDict @@ -66,14 +66,19 @@ class LoggingWorker: max_queue_size: int = LOGGING_WORKER_MAX_QUEUE_SIZE, concurrency: int = LOGGING_WORKER_CONCURRENCY, timeout_summary_window: float = LOGGING_WORKER_TIMEOUT_SUMMARY_WINDOW_SECONDS, + *, + retry_sleep: Callable[[float], Awaitable[None]] = asyncio.sleep, ): self.timeout = timeout self.max_queue_size = max_queue_size self.concurrency = concurrency self.timeout_summary_window = timeout_summary_window self._queue: asyncio.Queue[LoggingTask] | None = None - self._worker_task: asyncio.Task | None = None - self._running_tasks: set[asyncio.Task] = set() + self._worker_task: asyncio.Task[None] | None = None + self._running_tasks: set[asyncio.Task[None]] = set() + self._overflow_tasks: set[asyncio.Task[None]] = set() # mutable-ok: done callbacks discard refs + self._pending_enqueues: dict[int, LoggingTask] = {} # mutable-ok: retries and shutdown share callback ownership + self._retry_sleep = retry_sleep self._dequeued_tasks: dict[int, LoggingTask] = {} # mutable-ok: refs so flush can rescue never-started tasks self._sem: asyncio.Semaphore | None = None self._bound_loop: asyncio.AbstractEventLoop | None = None @@ -168,6 +173,7 @@ class LoggingWorker: self._sem = None self._worker_task = None self._running_tasks.clear() + self._overflow_tasks.clear() self._timeout_summary_task = None self._timeout_burst_count = 0 self._queue = new_queue @@ -185,6 +191,10 @@ class LoggingWorker: self._sem = asyncio.Semaphore(self.concurrency) if self._worker_task is None or self._worker_task.done(): self._worker_task = asyncio.create_task(self._worker_loop()) + if not self._overflow_tasks: + self._aggressive_clear_in_progress = False + for pending in tuple(self._pending_enqueues.values()): + self._schedule_delayed_enqueue_retry(pending) async def _process_log_task( self, task: LoggingTask, sem: asyncio.Semaphore, queue: "asyncio.Queue[LoggingTask]" @@ -278,12 +288,24 @@ class LoggingWorker: # Capture the current context when enqueueing task: Final = LoggingTask(coroutine=coroutine, context=contextvars.copy_context()) + self._enqueue_task(task) + + def _enqueue_task(self, task: LoggingTask) -> None: + if self._queue is None: + return try: self._queue.put_nowait(task) except asyncio.QueueFull: - # Queue is full - handle it appropriately verbose_logger.exception("LoggingWorker queue is full") + self._pending_enqueues[id(task)] = task self._handle_queue_full(task) + else: + self._pending_enqueues.pop(id(task), None) + + def _start_overflow_task(self, coroutine: Coroutine[object, object, None]) -> None: + task: Final = asyncio.create_task(coroutine) + self._overflow_tasks.add(task) + task.add_done_callback(self._overflow_tasks.discard) def _should_start_aggressive_clear(self) -> bool: """ @@ -326,7 +348,7 @@ class LoggingWorker: if self._should_start_aggressive_clear(): self._mark_aggressive_clear_started() # Schedule clearing as async task so enqueue returns immediately (non-blocking) - asyncio.create_task(self._aggressively_clear_queue_async(task)) + self._start_overflow_task(self._aggressively_clear_queue_async(task)) else: # Cooldown active or clear in progress, schedule a delayed retry self._schedule_delayed_enqueue_retry(task) @@ -363,7 +385,7 @@ class LoggingWorker: delay: Final = self._calculate_retry_delay() # Schedule the retry as a background task - asyncio.create_task(self._retry_enqueue_task(task, delay)) + self._start_overflow_task(self._retry_enqueue_task(task, delay)) except RuntimeError: # No event loop, drop the task as we can't schedule a retry pass @@ -373,17 +395,8 @@ class LoggingWorker: Retry enqueueing the task after delay, preserving original context. This is called as a background task from _schedule_delayed_enqueue_retry. """ - await asyncio.sleep(delay) - - # Try to enqueue the task directly, preserving its original context - if self._queue is None: - return - - try: - self._queue.put_nowait(task) - except asyncio.QueueFull: - # Still full - handle it appropriately (clear or retry again) - self._handle_queue_full(task) + await self._retry_sleep(delay) + self._enqueue_task(task) def _extract_tasks_from_queue(self) -> list[LoggingTask]: """ @@ -423,15 +436,10 @@ class LoggingWorker: return extracted_tasks: Final = self._extract_tasks_from_queue() - - # Add new task to extracted tasks to process directly if new_task is not None: - self._track_dequeued(new_task) - extracted_tasks.append(new_task) - - # Process extracted tasks directly + self._enqueue_task(new_task) if extracted_tasks: - await self._process_extracted_tasks(extracted_tasks) + await asyncio.gather(*(self._process_single_task(task) for task in extracted_tasks)) except Exception as e: verbose_logger.exception("LoggingWorker error during aggressive clear: %s", e) finally: @@ -439,7 +447,7 @@ class LoggingWorker: self._aggressive_clear_in_progress = False async def _process_single_task(self, task: LoggingTask) -> None: - """Process a single task and mark it done.""" + """Process a task extracted from the queue and mark it done.""" if self._queue is None: return @@ -455,17 +463,6 @@ class LoggingWorker: self._untrack_dequeued(task) self._queue.task_done() - async def _process_extracted_tasks(self, tasks: list[LoggingTask]) -> None: - """ - Process tasks that were extracted from the queue to make room. - Processes them concurrently without semaphore limits for maximum speed. - """ - if not tasks or self._queue is None: - return - - # Process all tasks concurrently for maximum speed - await asyncio.gather(*[self._process_single_task(task) for task in tasks]) - def ensure_initialized_and_enqueue(self, async_coroutine: Coroutine[object, None, None]) -> None: """ Ensure the logging worker is initialized and enqueue the coroutine. @@ -473,32 +470,63 @@ class LoggingWorker: self.start() self.enqueue(async_coroutine) - async def stop(self) -> None: + @property + def pending_count(self) -> int: + return ( + (self._queue.qsize() if self._queue is not None else 0) + + len(self._dequeued_tasks) + + len(self._pending_enqueues) + ) + + async def stop(self, *, discard_pending: bool = False, timeout: float | None = None) -> None: """Stop the logging worker and clean up resources.""" + if discard_pending and self._queue is not None: + discarded: Final = self._drain_pending(self._queue) + for task in discarded: + task["coroutine"].close() + self._queue.task_done() + if discarded: + verbose_logger.warning("LoggingWorker shutdown: %d queued callbacks discarded", len(discarded)) if self._timeout_summary_task is not None: self._timeout_summary_task.cancel() self._timeout_summary_task = None self._emit_timeout_summary() - if self._worker_task is None and not self._running_tasks: - # No worker launched and no in-flight tasks to drain. - return + if discard_pending: + rejected: Final = tuple(self._pending_enqueues.values()) + self._pending_enqueues.clear() + for task in rejected: + task["coroutine"].close() + if rejected: + verbose_logger.warning("LoggingWorker shutdown: %d overflow callbacks discarded", len(rejected)) - tasks_to_cancel: Final[list[asyncio.Task]] = list(self._running_tasks) - if self._worker_task: - # Include the main worker loop so it stops fetching work. - tasks_to_cancel.append(self._worker_task) + tasks_to_cancel: Final = ( + *self._running_tasks, + *self._overflow_tasks, + *((self._worker_task,) if self._worker_task is not None else ()), + ) + if not tasks_to_cancel: + return for task in tasks_to_cancel: # Propagate cancellation to every pending task. task.cancel() # Wait for cancellation to settle; ignore errors raised during shutdown. - await asyncio.gather(*tasks_to_cancel, return_exceptions=True) + done, pending = await asyncio.wait(tasks_to_cancel, timeout=timeout) + await asyncio.gather(*done, return_exceptions=True) + if pending: + verbose_logger.warning("LoggingWorker shutdown: %d tasks still alive after cancellation", len(pending)) - self._worker_task = None + if discard_pending and self._queue is not None: + for task in self._unstarted_dequeued_tasks(): + task["coroutine"].close() + self._untrack_dequeued(task) + self._queue.task_done() + if self._worker_task is not None and self._worker_task.done(): + self._worker_task = None # Drop references to completed tasks so we can restart cleanly. - self._running_tasks.clear() + self._running_tasks.difference_update(done) async def flush(self) -> None: """Flush the logging queue. @@ -516,6 +544,9 @@ class LoggingWorker: return self.start() await self._queue.join() + while self._overflow_tasks: + await asyncio.wait(tuple(self._overflow_tasks)) + await self._queue.join() async def clear_queue(self): """ diff --git a/litellm/litellm_core_utils/prompt_templates/common_utils.py b/litellm/litellm_core_utils/prompt_templates/common_utils.py index 9bc53791b36..b3f89b33d1d 100644 --- a/litellm/litellm_core_utils/prompt_templates/common_utils.py +++ b/litellm/litellm_core_utils/prompt_templates/common_utils.py @@ -424,6 +424,109 @@ def anthropic_image_source_to_openai_url(image_source: Mapping[str, object]) -> return None +@dataclass(frozen=True, slots=True) +class InlineDocument: + filename: str + file_data: str + + +def _document_media_type(source: object) -> str: + match source: + case {"media_type": str() as media_type} if media_type: + return media_type + case _: + return "application/pdf" + + +def anthropic_inline_document(document: Mapping[str, object]) -> InlineDocument | None: + source: Final = document.get("source") + match source: + case {"type": "base64", "data": str() as data} if data: + title: Final = document.get("title") + return InlineDocument( + filename=title if isinstance(title, str) and title else "document.pdf", + file_data=f"data:{_document_media_type(source)};base64,{data}", + ) + case _: + return None + + +def is_pdf_file_part(part: object) -> bool: + match part: + case {"type": "file", "file": {"file_data": str() as file_data}} if file_data.startswith("data:"): + return file_data.startswith("data:application/pdf") + case {"type": "file", "file": {"format": str() as file_format}}: + return file_format == "application/pdf" + case {"type": "file", "file": {"filename": str() as filename}}: + return filename.lower().endswith(".pdf") + case _: + return False + + +_CONTENT_PARTS: Final = TypeAdapter(tuple[object, ...]) + + +def content_parts(message: Mapping[str, object]) -> Sequence[object]: + content: Final = message.get("content") + return _CONTENT_PARTS.validate_python(content) if isinstance(content, list) else () + + +def _has_pdf_file_part(message: Mapping[str, object]) -> bool: + return any(is_pdf_file_part(part) for part in content_parts(message)) + + +def messages_have_pdf_file_part(messages: Sequence[Mapping[str, object]]) -> bool: + return any(_has_pdf_file_part(message) for message in messages) + + +def _message_without_pdf_file_parts(message: AllMessageValues) -> AllMessageValues: + if not _has_pdf_file_part(message): + return message + kept: Final = [part for part in content_parts(message) if not is_pdf_file_part(part)] + return cast(AllMessageValues, {**message, "content": kept}) # cast-ok: same message with fewer content parts + + +def _is_pdf_only_user_turn(message: AllMessageValues) -> bool: + parts: Final = content_parts(message) + return message["role"] == "user" and bool(parts) and all(is_pdf_file_part(part) for part in parts) + + +def messages_without_pdf_file_parts(messages: Sequence[AllMessageValues]) -> tuple[AllMessageValues, ...]: + return tuple( + _message_without_pdf_file_parts(message) for message in messages if not _is_pdf_only_user_turn(message) + ) + + +_CONTENT_PART: Final = TypeAdapter(Mapping[str, object]) + + +def _file_part_as_image_url(part: object) -> object: + match part: + case {"type": "file", "file": {"file_data": str() as file_data}} if file_data: + fields: Final = _CONTENT_PART.validate_python(part) + return {key: value for key, value in fields.items() if key != "file"} | { + "type": "image_url", + "image_url": {"url": file_data}, + } + case _: + return part + + +def message_with_file_parts_as_image_urls(message: Mapping[str, object]) -> Mapping[str, object]: + parts: Final = content_parts(message) + if not parts: + return message + return {**message, "content": [_file_part_as_image_url(part) for part in parts]} + + +def _typed_message_with_file_parts_as_image_urls(message: AllMessageValues) -> AllMessageValues: + return cast(AllMessageValues, message_with_file_parts_as_image_urls(message)) # cast-ok: same message, image parts + + +def messages_with_file_parts_as_image_urls(messages: Sequence[AllMessageValues]) -> tuple[AllMessageValues, ...]: + return tuple(_typed_message_with_file_parts_as_image_urls(message) for message in messages) + + def _image_part_url(part: Mapping[str, object]) -> str | None: """The image URL carried by one content part, whichever of the three dialects wrote it.""" part_type: Final = part.get("type") diff --git a/litellm/litellm_core_utils/realtime_streaming.py b/litellm/litellm_core_utils/realtime_streaming.py index 927caec3350..484bacdc72c 100644 --- a/litellm/litellm_core_utils/realtime_streaming.py +++ b/litellm/litellm_core_utils/realtime_streaming.py @@ -20,6 +20,7 @@ from litellm.constants import ( REALTIME_SESSION_SUCCESS_LOGGED_KEY, ) from litellm.litellm_core_utils.logging_worker import GLOBAL_LOGGING_WORKER +from litellm.llms.base_llm.realtime.transcription_protocol import RealtimeTranscriptionProtocolError from litellm.llms.base_llm.realtime.transformation import BaseRealtimeConfig, RealtimeBackend from litellm.types.llms.openai import ( OpenAIRealtimeEvents, @@ -1281,24 +1282,29 @@ class RealTimeStreaming: verbose_logger.exception("Connection closed in backend to client send messages - %s", e) close: Final = backend_close_from(e) self._flush_unbilled_transcription_usage() - if self._backend_refused_session(close): - await self.log_backend_refusal(e) - else: - await self.log_messages() + await self._log_session_end(e if self._backend_refused_session(close) else None) return close except asyncio.CancelledError: self._flush_unbilled_transcription_usage() - await self.log_messages() + await self._log_session_end(None) raise except Exception as e: verbose_logger.exception("Error in backend to client send messages: %s", e) self._flush_unbilled_transcription_usage() - await self.log_messages() + await self._log_session_end(None) return BackendClose(code=1011, reason="proxy failed while relaying the upstream websocket") def _backend_refused_session(self, close: BackendClose) -> bool: return close.code != 1000 and not self._backend_sent_frames + async def _log_session_end(self, transport_refusal: Exception | None) -> None: + provider_refusal: Final = None if self.provider_config is None else self.provider_config.session_refusal() + refusal: Final = provider_refusal or transport_refusal + if refusal is None: + await self.log_messages() + return + await self.log_backend_refusal(refusal) + async def log_backend_refusal(self, error: Exception) -> None: if not self.logging_obj: return @@ -1691,7 +1697,13 @@ class RealTimeStreaming: # ``_maybe_send_guardrail_turn_detection_update`` would retry. if msg_type == "session.update" and self.provider_config is None: await self._send_session_gate_once() - sent = await self._send_to_backend(message) + try: + sent = await self._send_to_backend(message) + except RealtimeTranscriptionProtocolError as e: + await self.websocket.send_text(realtime_error_event(str(e), error_type="invalid_request_error")) + if self._uses_deferred_backend_setup(): + raise + continue if guardrail_turn_detection_injected and sent: self._guardrail_turn_detection_update_sent = True diff --git a/litellm/litellm_core_utils/served_output_texts.py b/litellm/litellm_core_utils/served_output_texts.py index 854785f304b..b19973e06c3 100644 --- a/litellm/litellm_core_utils/served_output_texts.py +++ b/litellm/litellm_core_utils/served_output_texts.py @@ -4,12 +4,13 @@ post-call guardrail rewrote rather than the provider response the proxy assemble from __future__ import annotations from collections.abc import Sequence +from itertools import chain from typing import Final, Literal -from pydantic import BaseModel, Field, TypeAdapter, ValidationError +from pydantic import BaseModel, ConfigDict, Field, TypeAdapter, ValidationError from litellm.types.llms.base import LiteLLMBaseModel -from litellm.types.utils import ModelResponse, ModelResponseStream +from litellm.types.utils import ModelResponse, ModelResponseStream, TextCompletionResponse SERVED_OUTPUT_TEXTS_KEY: Final = "served_output_texts" @@ -44,6 +45,19 @@ class _ChatChoices(LiteLLMBaseModel): choices: list[object] +class _TextCompletionChoice(LiteLLMBaseModel): + model_config = ConfigDict(frozen=True) + + index: int | None = None + text: str | None = None + + +class _TextCompletionChunk(LiteLLMBaseModel): + model_config = ConfigDict(frozen=True) + + choices: tuple[_TextCompletionChoice, ...] + + def _as_json_object(response: object) -> dict[str, object] | None: candidate: Final = response.model_dump() if isinstance(response, BaseModel) else response try: @@ -108,8 +122,13 @@ def served_output_texts(response: object) -> ServedTexts | None: def served_stream_output_texts(chunks: Sequence[object]) -> ServedTexts | None: - if chunks and all(isinstance(chunk, ModelResponseStream) for chunk in chunks): - return _chat_stream_texts(tuple(chunk for chunk in chunks if isinstance(chunk, ModelResponseStream))) + chat_chunks: Final = tuple(chunk for chunk in chunks if isinstance(chunk, ModelResponseStream)) + if chunks and len(chat_chunks) == len(chunks): + return _indexed_texts_by_choice(tuple(chain.from_iterable(_chat_chunk_texts(chunk) for chunk in chat_chunks))) + if chunks and all(isinstance(chunk, TextCompletionResponse) for chunk in chunks): + return _indexed_texts_by_choice( + tuple(chain.from_iterable(_text_completion_chunk_texts(chunk) for chunk in chunks)) + ) from litellm.llms.anthropic.messages.stream_assembly import ( assemble_anthropic_sse_stream, is_anthropic_sse_stream, @@ -121,19 +140,26 @@ def served_stream_output_texts(chunks: Sequence[object]) -> ServedTexts | None: return _chat_texts(assembled) if assembled is not None else None -def _chat_stream_choice_text(chunks: Sequence[ModelResponseStream], index: int) -> str | None: - contents: Final = tuple( - content - for chunk in chunks +def _chat_chunk_texts(chunk: ModelResponseStream) -> tuple[tuple[int, str | None], ...]: + return tuple( + (choice.index or 0, content if isinstance(content := choice.delta.content, str) else None) for choice in chunk.choices - if (choice.index or 0) == index and isinstance(content := choice.delta.content, str) ) - return "".join(contents) if contents else None -def _chat_stream_texts(chunks: Sequence[ModelResponseStream]) -> ServedTexts | None: - choice_count: Final = max(((choice.index or 0) + 1 for chunk in chunks for choice in chunk.choices), default=0) - texts: Final = tuple(_chat_stream_choice_text(chunks, index) for index in range(choice_count)) +def _text_completion_chunk_texts(chunk: object) -> tuple[tuple[int, str | None], ...]: + parsed: Final = _TextCompletionChunk.model_validate(_as_json_object(chunk)) + return tuple((choice.index or 0, choice.text) for choice in parsed.choices) + + +def _joined_choice_text(indexed_texts: Sequence[tuple[int, str | None]], index: int) -> str | None: + texts: Final = tuple(text for text_index, text in indexed_texts if text_index == index and text is not None) + return "".join(texts) if texts else None + + +def _indexed_texts_by_choice(indexed_texts: Sequence[tuple[int, str | None]]) -> ServedTexts | None: + choice_count: Final = max((index + 1 for index, _ in indexed_texts), default=0) + texts: Final = tuple(_joined_choice_text(indexed_texts, index) for index in range(choice_count)) return texts if any(text is not None for text in texts) else None @@ -170,6 +196,8 @@ def _choice_with_text(choice: object, text: str | None) -> object: if choice_obj is None or text is None: return choice message: Final = _as_json_object(choice_obj.get("message")) - if message is None or message.get("content") == text: - return choice - return {**choice_obj, "message": {**message, "content": text}} + if message is not None: + return choice if message.get("content") == text else {**choice_obj, "message": {**message, "content": text}} + if isinstance(choice_obj.get("text"), str): + return {**choice_obj, "text": text} + return choice diff --git a/litellm/litellm_core_utils/streaming_chunk_builder_utils.py b/litellm/litellm_core_utils/streaming_chunk_builder_utils.py index 719f75546b9..8e1791c3c92 100644 --- a/litellm/litellm_core_utils/streaming_chunk_builder_utils.py +++ b/litellm/litellm_core_utils/streaming_chunk_builder_utils.py @@ -12,9 +12,14 @@ from typing_extensions import ReadOnly, Required import litellm from litellm._logging import verbose_logger -from litellm.litellm_core_utils.core_helpers import chunk_usage, provider_reported_usage_cost +from litellm.litellm_core_utils.core_helpers import ( + USAGE_COST_BILLED_PROVIDERS, + chunk_usage, + provider_reported_usage_cost, +) from litellm.litellm_core_utils.hidden_params import HIDDEN_PARAMS_ATTR, set_hidden_params from litellm.litellm_core_utils.llm_cost_calc.guardrail_cost import BEDROCK_TRACE_FIELDS, object_items, object_mapping +from litellm.litellm_core_utils.llm_response_utils.convert_dict_to_response import partition_annotations from litellm.litellm_core_utils.prompt_templates.common_utils import ( get_content_from_model_response, # pyright: ignore[reportUnknownVariableType] # response param accepts an untyped dict ) @@ -1352,7 +1357,7 @@ def stream_chunk_builder_text_completion( return TextCompletionResponse(**response) -_CALCULATOR_PRICED_REPORTED_COST_PROVIDERS: Final = frozenset({LlmProviders.XAI.value}) +_CALCULATOR_PRICED_REPORTED_COST_PROVIDERS: Final = frozenset({LlmProviders.XAI.value}) | USAGE_COST_BILLED_PROVIDERS def _logged_provider(logging_obj: Optional["Logging"]) -> object: @@ -1696,10 +1701,17 @@ def stream_chunk_builder( # Merge annotations from ALL chunks — providers may spread # them across multiple streaming chunks or send them only in # the final chunk. - all_annotations: Final[list] = [] + all_annotations: Final[list[object]] = [] for ac in annotation_chunks: all_annotations.extend(ac["choices"][0]["delta"]["annotations"]) - response["choices"][0]["message"]["annotations"] = all_annotations + message_annotations, provider_annotations = partition_annotations(all_annotations) + if message_annotations is not None: + response["choices"][0]["message"]["annotations"] = message_annotations + if provider_annotations: + response.choices[0].message.provider_specific_fields = { + **(response.choices[0].message.provider_specific_fields or {}), + "annotations": list(provider_annotations), + } audio_chunks: Final = [ chunk @@ -1762,7 +1774,10 @@ def stream_chunk_builder( if combined_provider_fields: _choice = response.choices[0] - _choice.message.provider_specific_fields = combined_provider_fields + _choice.message.provider_specific_fields = { + **(_choice.message.provider_specific_fields or {}), + **combined_provider_fields, + } completion_output = get_content_from_model_response(response) diff --git a/litellm/llms/anthropic/chat/guardrail_translation/handler.py b/litellm/llms/anthropic/chat/guardrail_translation/handler.py index d371aab4581..31b0f06251d 100644 --- a/litellm/llms/anthropic/chat/guardrail_translation/handler.py +++ b/litellm/llms/anthropic/chat/guardrail_translation/handler.py @@ -432,27 +432,30 @@ class AnthropicMessagesHandler(BaseTranslation): message, _ = serialize_http_exception_detail(exc.detail) return tuple(anthropic_sse_error_frames(message)) - def _standalone_block_chunks(self, exc: "ModifyResponseException") -> list[bytes]: + def build_block_response(self, exc: "ModifyResponseException") -> "AnthropicMessagesResponse": import uuid - from litellm.llms.anthropic.messages.native.fake_stream_iterator import ( - FakeAnthropicMessagesStreamIterator, - ) from litellm.llms.base_llm.guardrail_translation.utils import ( blocked_response_usage, ) from litellm.types.utils import AnthropicMessagesResponse - block_response: Final = AnthropicMessagesResponse( + return AnthropicMessagesResponse( id=f"msg_{uuid.uuid4()}", type="message", role="assistant", content=[{"type": "text", "text": exc.message}], model=exc.model, stop_reason="end_turn", - usage=blocked_response_usage(getattr(exc, "original_response", None)), + usage=blocked_response_usage(exc.original_response), ) - return list(FakeAnthropicMessagesStreamIterator(response=block_response)) + + def _standalone_block_chunks(self, exc: "ModifyResponseException") -> list[bytes]: + from litellm.llms.anthropic.messages.native.fake_stream_iterator import ( + FakeAnthropicMessagesStreamIterator, + ) + + return list(FakeAnthropicMessagesStreamIterator(response=self.build_block_response(exc))) def _block_continuation_chunks( self, exc: "ModifyResponseException", responses_so_far: Sequence[object] diff --git a/litellm/llms/anthropic/chat/handler.py b/litellm/llms/anthropic/chat/handler.py index 8a82ed98991..e1436473426 100644 --- a/litellm/llms/anthropic/chat/handler.py +++ b/litellm/llms/anthropic/chat/handler.py @@ -17,6 +17,7 @@ import litellm import litellm.litellm_core_utils import litellm.types import litellm.types.utils +from litellm.anthropic_beta_gated_fields import strip_beta_gated_request_body from litellm.anthropic_beta_headers_manager import ( update_request_with_filtered_beta, ) @@ -506,11 +507,20 @@ class AnthropicChatCompletion(BaseLLM): The pair stays mutable because the streaming path rewrites it in place (`data["stream"] = True`) before sending. """ - request_headers, data = update_request_with_filtered_beta( + request_headers, filtered_data = update_request_with_filtered_beta( headers=headers, request_data=request_data, provider=custom_llm_provider, ) + data: Final = dict( + strip_beta_gated_request_body( + filtered_data, + request_headers, + drop_params=litellm_params.get("drop_params"), # pyright: ignore[reportUnknownMemberType, reportUnknownArgumentType] # litellm_params is the handler's bare dict + model=model, + provider=custom_llm_provider, + ) + ) logging_obj.pre_call( input=messages, api_key=api_key, diff --git a/litellm/llms/anthropic/common_utils.py b/litellm/llms/anthropic/common_utils.py index e591f3a3fde..3b7fcdea0e4 100644 --- a/litellm/llms/anthropic/common_utils.py +++ b/litellm/llms/anthropic/common_utils.py @@ -51,6 +51,7 @@ from litellm.types.llms.anthropic import ( ANTHROPIC_OAUTH_BETA_HEADER, ANTHROPIC_OAUTH_TOKEN_PREFIX, ANTHROPIC_SERVER_TOOL_USE_ID_PREFIX, + ANTHROPIC_THINKING_BINDING_CONTROLS_BETA_HEADER, ANTHROPIC_THINKING_DISPLAY_UPDATES_BETA_HEADER, ANTHROPIC_TOOL_SEARCH_TOOL_TYPES, AllAnthropicToolsValues, @@ -600,6 +601,20 @@ class AnthropicModelInfo(BaseLLMModelInfo): return False return thinking.get("type") in ("adaptive", "enabled") and thinking.get("display") == "updates" + def thinking_betas(self, thinking: AnthropicThinkingParam | None) -> tuple[str, ...]: + return ( + *( + (ANTHROPIC_THINKING_DISPLAY_UPDATES_BETA_HEADER,) + if self.is_thinking_display_updates_used(thinking) + else () + ), + *( + (ANTHROPIC_THINKING_BINDING_CONTROLS_BETA_HEADER,) + if isinstance(thinking, dict) and "block_binding" in thinking + else () + ), + ) + def is_mid_conversation_tool_change_used(self, messages: Sequence[object]) -> bool: for message in messages: if message_field(message, "role") != "system": @@ -1180,7 +1195,7 @@ class AnthropicModelInfo(BaseLLMModelInfo): *, custom_llm_provider: str, is_mid_conversation_output_config_used: bool = False, - is_thinking_display_updates_used: bool = False, + thinking: AnthropicThinkingParam | None = None, is_mid_conversation_tool_change_used: bool = False, ) -> list[str]: """ @@ -1217,13 +1232,10 @@ class AnthropicModelInfo(BaseLLMModelInfo): if is_mid_conversation_output_config_used: betas.append(ANTHROPIC_MID_CONVERSATION_OUTPUT_CONFIG_BETA_HEADER) - thinking_display_betas: Final = ( - (ANTHROPIC_THINKING_DISPLAY_UPDATES_BETA_HEADER,) if is_thinking_display_updates_used else () - ) tool_change_betas: Final = ( (ANTHROPIC_MID_CONVERSATION_TOOL_CHANGES_BETA_HEADER,) if is_mid_conversation_tool_change_used else () ) - return list(set(betas).union(thinking_display_betas, tool_change_betas)) + return list(set(betas).union(self.thinking_betas(thinking), tool_change_betas)) @staticmethod def _make_api_key_auth_header( @@ -1285,7 +1297,7 @@ class AnthropicModelInfo(BaseLLMModelInfo): api_base: str | None = None, use_bearer_for_custom_base: bool = False, is_mid_conversation_output_config_used: bool = False, - is_thinking_display_updates_used: bool = False, + thinking: AnthropicThinkingParam | None = None, is_mid_conversation_tool_change_used: bool = False, wif_minted: bool = False, ) -> dict: @@ -1346,7 +1358,7 @@ class AnthropicModelInfo(BaseLLMModelInfo): betas.update(user_anthropic_beta_headers) all_betas: Final = betas.union( - (ANTHROPIC_THINKING_DISPLAY_UPDATES_BETA_HEADER,) if is_thinking_display_updates_used else (), + self.thinking_betas(thinking), (ANTHROPIC_MID_CONVERSATION_TOOL_CHANGES_BETA_HEADER,) if is_mid_conversation_tool_change_used else (), ) @@ -1433,7 +1445,7 @@ class AnthropicModelInfo(BaseLLMModelInfo): auth_token=auth_token, file_id_used=file_id_used, is_mid_conversation_output_config_used=is_mid_conversation_output_config_used, - is_thinking_display_updates_used=self.is_thinking_display_updates_used(optional_params.get("thinking")), + thinking=optional_params.get("thinking"), is_mid_conversation_tool_change_used=self.is_mid_conversation_tool_change_used(messages), web_search_tool_used=web_search_tool_used, is_vertex_request=optional_params.get("is_vertex_request", False), diff --git a/litellm/llms/anthropic/messages/adapters/transformation.py b/litellm/llms/anthropic/messages/adapters/transformation.py index 5fc7a56600c..ddda759384f 100644 --- a/litellm/llms/anthropic/messages/adapters/transformation.py +++ b/litellm/llms/anthropic/messages/adapters/transformation.py @@ -123,6 +123,7 @@ from openai.types.chat.chat_completion_chunk import Choice as OpenAIStreamingCho from litellm.litellm_core_utils.prompt_templates.common_utils import ( anthropic_image_source_to_openai_url, + anthropic_inline_document, parse_tool_call_arguments, reasoning_content_from_thinking_blocks, with_prompt_cache_breakpoint, @@ -185,6 +186,8 @@ from litellm.types.llms.openai import ( AllMessageValues, ChatCompletionAssistantMessage, ChatCompletionAssistantToolCall, + ChatCompletionFileObject, + ChatCompletionFileObjectFile, ChatCompletionImageObject, ChatCompletionImageUrlObject, ChatCompletionRedactedThinkingBlock, @@ -203,7 +206,7 @@ from litellm.types.llms.openai import ( ChatCompletionUserMessage, ToolMessageContentPart, ) -from litellm.types.utils import Choices, ModelResponse, StreamingChoices, Usage +from litellm.types.utils import Choices, LlmProviders, ModelResponse, StreamingChoices, Usage from litellm.utils import supports_mid_conversation_system from .streaming_iterator import AnthropicSSEStream, AnthropicStreamWrapper @@ -362,6 +365,9 @@ class AnthropicAdapter: _BlockT: Final = TypeVar("_BlockT", bound=Mapping[str, object]) +_CLAUDE_OVER_REASONING_EFFORT_PROVIDERS: Final = frozenset({LlmProviders.KYMA.value}) + + class LiteLLMAnthropicMessagesAdapter: def __init__(self): pass @@ -495,7 +501,9 @@ class LiteLLMAnthropicMessagesAdapter: for m in ordered_messages: user_message: ChatCompletionUserMessage | None = None tool_message_list: list[ChatCompletionToolMessage] = [] - new_user_content_list: list[ChatCompletionTextObject | ChatCompletionImageObject] = [] + new_user_content_list: list[ + ChatCompletionTextObject | ChatCompletionImageObject | ChatCompletionFileObject + ] = [] if m["role"] == "system": system_message = self._translate_midturn_system_message_to_openai(m, model) if system_message is not None: @@ -528,13 +536,8 @@ class LiteLLMAnthropicMessagesAdapter: self._add_prompt_cache_breakpoint_if_present(content, image_obj) ) elif content.get("type") == "document": - # Convert Anthropic document format (PDF, etc.) to OpenAI format - source = content.get("source", {}) - openai_image_url = self._translate_anthropic_image_to_openai(cast(dict, source)) - - if openai_image_url: - image_url_obj = ChatCompletionImageUrlObject(url=openai_image_url) - doc_obj = ChatCompletionImageObject(type="image_url", image_url=image_url_obj) + doc_obj = self._translate_anthropic_document_to_openai(content) + if doc_obj is not None: self._add_cache_control_if_applicable(content, doc_obj, model) new_user_content_list.append(doc_obj) elif content.get("type") == "tool_result": @@ -1128,7 +1131,8 @@ class LiteLLMAnthropicMessagesAdapter: """Translate Anthropic thinking to either thinking or reasoning_effort. A Claude-family target keeps ``thinking`` verbatim, since every bridged provider serving one - speaks that param. Carrying its adaptive effort tier alongside takes two different params, + speaks that param, except Kyma, which serves Claude over the OpenAI dialect and takes only + ``reasoning_effort``, so its Claude models get the non-Claude mapping. Carrying its adaptive effort tier alongside takes two different params, because the two are not interchangeable at the provider mapping below. Bedrock takes ``output_config`` directly, which attaches the tier and leaves ``thinking`` @@ -1159,7 +1163,9 @@ class LiteLLMAnthropicMessagesAdapter: is_bedrock_target: Final = model.startswith(("bedrock/", "converse/", "invoke/")) or self.is_bedrock_arn_model( model ) - is_claude_target: Final = self.is_anthropic_claude_model(model) or self.is_bedrock_arn_model(model) + is_claude_target: Final = ( + self.is_anthropic_claude_model(model) or self.is_bedrock_arn_model(model) + ) and custom_llm_provider not in _CLAUDE_OVER_REASONING_EFFORT_PROVIDERS output_config: Final = anthropic_message_request.get("output_config") if is_claude_target: @@ -1351,7 +1357,7 @@ class LiteLLMAnthropicMessagesAdapter: text: Final = tool_result_text_part(block) return None if text is None else ChatCompletionTextObject(type="text", text=text) case "image" | "document": - return self._tool_result_image_part(block.get("source")) + return self._source_image_url_part(block.get("source")) case "tool_reference": return ChatCompletionToolReferenceObject( type="tool_reference", tool_name=str(block.get("tool_name") or "") @@ -1359,7 +1365,18 @@ class LiteLLMAnthropicMessagesAdapter: case _: return None - def _tool_result_image_part(self, image_source: object) -> ChatCompletionImageObject | None: + def _translate_anthropic_document_to_openai( + self, document: Mapping[str, object] + ) -> ChatCompletionFileObject | ChatCompletionImageObject | None: + inline_document: Final = anthropic_inline_document(document) + if inline_document is None: + return self._source_image_url_part(document.get("source")) + return ChatCompletionFileObject( + type="file", + file=ChatCompletionFileObjectFile(filename=inline_document.filename, file_data=inline_document.file_data), + ) + + def _source_image_url_part(self, image_source: object) -> ChatCompletionImageObject | None: if not isinstance(image_source, dict): return None openai_image_url = self._translate_anthropic_image_to_openai(image_source) diff --git a/litellm/llms/anthropic/messages/native/transformation.py b/litellm/llms/anthropic/messages/native/transformation.py index 75b0329315f..c086618e829 100644 --- a/litellm/llms/anthropic/messages/native/transformation.py +++ b/litellm/llms/anthropic/messages/native/transformation.py @@ -2,10 +2,21 @@ from collections.abc import AsyncIterator, Mapping, Sequence from typing import ClassVar, Final import httpx +from pydantic import TypeAdapter import litellm from litellm._logging import verbose_logger -from litellm.anthropic_beta_headers_manager import get_unsupported_headers, is_beta_header_supported_by_bundled_config +from litellm.anthropic_beta_gated_fields import ( + log_beta_gated_strips, + strip_beta_gated_native_request, + strip_beta_gated_system_messages, + unstripped_native_request, +) +from litellm.anthropic_beta_headers_manager import ( + get_unsupported_headers, + has_beta_headers_config, + is_beta_header_supported_by_bundled_config, +) from litellm.constants import ANTHROPIC_BILLING_METADATA_PREFIX from litellm.exceptions import AuthenticationError from litellm.litellm_core_utils.core_helpers import drops_unsupported_params @@ -20,7 +31,6 @@ from litellm.types.llms.anthropic import ( ANTHROPIC_ADVISOR_TOOL_TYPE, ANTHROPIC_BETA_HEADER_VALUES, ANTHROPIC_MID_CONVERSATION_TOOL_CHANGES_BETA_HEADER, - ANTHROPIC_THINKING_DISPLAY_UPDATES_BETA_HEADER, AnthropicMessagesRequest, ) from litellm.types.llms.anthropic_messages.anthropic_response import ( @@ -51,6 +61,8 @@ DEFAULT_ANTHROPIC_API_VERSION: Final = "2023-06-01" _CALLER_CREDENTIAL_HEADERS: Final = frozenset({"x-api-key", "authorization"}) +_MESSAGE_MAPPINGS: Final = TypeAdapter(tuple[Mapping[str, object], ...]) + def _carries_caller_credential(headers: Mapping[str, str]) -> bool: """Whether the caller sent their own Anthropic credential, in which case this passthrough @@ -85,6 +97,9 @@ def _ensure_stop_sequence(response: AnthropicMessagesResponse) -> AnthropicMessa return response +_MESSAGES_ADAPTER: Final = TypeAdapter(list[dict[str, object]]) + + class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): _workload_identity_eligible: ClassVar[bool] = True @@ -229,7 +244,18 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): else: return system_param - def _normalize_system_role_messages(self, anthropic_messages_request: dict, model: str) -> None: + def _strip_beta_gated_system_turns( + self, messages: Sequence[Mapping[str, object]], model: str, drop_params: object + ) -> Sequence[Mapping[str, object]]: + if not has_beta_headers_config(self._resolved_provider): + return messages + gated: Final = strip_beta_gated_system_messages(messages, frozenset(), drop_params=drop_params) + log_beta_gated_strips(gated.stripped, model=model, provider=self._resolved_provider) + return _MESSAGE_MAPPINGS.validate_python(gated.messages) if gated.stripped else messages + + def _normalize_system_role_messages( + self, anthropic_messages_request: dict, model: str, drop_params: object + ) -> None: """Normalize ``role: "system"`` entries in ``messages`` per the Anthropic ``/v1/messages`` contract, which the first-party API, Bedrock Invoke, Vertex, and Azure Foundry all enforce identically. @@ -277,7 +303,15 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): custom_llm_provider=self.custom_llm_provider, key="supports_mid_conversation_system", ) - else list(convert_mid_conversation_system_turns(messages[leading_count:])) + else list( + convert_mid_conversation_system_turns( + self._strip_beta_gated_system_turns( + messages[leading_count:], # pyright: ignore[reportUnknownArgumentType] # messages comes from the bare request dict + model=model, + drop_params=drop_params, + ) + ) + ) ) if hoisted or remaining != messages: anthropic_messages_request["messages"] = remaining @@ -336,7 +370,7 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): model=model, ), ) - return self._finalize_messages_headers(headers, optional_params, messages), api_base + return self.finalize_messages_headers(headers, optional_params, messages), api_base async def avalidate_anthropic_messages_environment( self, @@ -376,7 +410,7 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): model=model, ), ) - return self._finalize_messages_headers(oauth_headers, optional_params, messages), api_base + return self.finalize_messages_headers(oauth_headers, optional_params, messages), api_base def _require_auth_header(self, auth_header: Mapping[str, str] | None, model: str) -> Mapping[str, str]: if auth_header is None: @@ -410,7 +444,7 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): return config_allows_workload_identity(self) - def _finalize_messages_headers( + def finalize_messages_headers( self, headers: dict, # mutable-ok: out-param optional_params: dict, # mutable-ok: out-param @@ -619,6 +653,12 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): if thinking_enabled or effort_enabled: optional_params.pop("temperature", None) + def strips_beta_gated_fields_from_final_headers(self) -> bool: + """False on legs whose final ``anthropic_beta`` list is only known after this transform (Bedrock Invoke), + which strip beta-gated fields themselves once that list exists, and on providers with no row in + ``anthropic_beta_headers_config.json``, whose filter drops every beta and so cannot say which fields fail.""" + return has_beta_headers_config(self._resolved_provider) + def transform_anthropic_messages_request( self, model: str, @@ -720,18 +760,37 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): if not _has_advisor: messages = strip_advisor_blocks_from_messages(messages) + gated: Final = ( + strip_beta_gated_native_request( + headers, # pyright: ignore[reportUnknownArgumentType] # the base signature types this as a bare dict + anthropic_messages_optional_request_params, # pyright: ignore[reportUnknownArgumentType] # the base signature types this as a bare dict + messages, # pyright: ignore[reportUnknownArgumentType] # the base signature types this as a bare list + drop_params=getattr(litellm_params, "drop_params", None), + ) + if self.strips_beta_gated_fields_from_final_headers() + else unstripped_native_request(anthropic_messages_optional_request_params, messages) # pyright: ignore[reportUnknownArgumentType] # the base signature types these as bare dicts + ) + log_beta_gated_strips(gated.stripped, model=model, provider=self._resolved_provider) anthropic_messages_request: Final[AnthropicMessagesRequest] = AnthropicMessagesRequest( messages=strip_encrypted_reasoning_blocks_from_anthropic_messages( - drop_non_string_tool_result_texts(messages, getattr(litellm_params, "drop_params", None)) + drop_non_string_tool_result_texts( + _MESSAGES_ADAPTER.validate_python(gated.messages) if gated.messages_stripped else messages, + getattr(litellm_params, "drop_params", None), + ) ), max_tokens=max_tokens, model=model, **anthropic_messages_optional_request_params, ) + request: Final = ( + {**dict(anthropic_messages_request), "thinking": gated.thinking} + if gated.thinking_stripped + else dict(anthropic_messages_request) # pyright: ignore[reportUnknownVariableType] # built from the base signature's bare dict + ) if not isinstance(tools, list): - return dict(anthropic_messages_request) + return request return { - **anthropic_messages_request, + **request, "tools": self.tools_for_upstream( OBJECT_LIST_ADAPTER.validate_python(tools), model, litellm_params.drop_params ), @@ -866,17 +925,13 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): if AnthropicModelInfo().is_tool_search_used(tools): beta_values.add(get_tool_search_beta_header(custom_llm_provider)) - thinking_display_betas: Final = ( - (ANTHROPIC_THINKING_DISPLAY_UPDATES_BETA_HEADER,) - if AnthropicModelInfo().is_thinking_display_updates_used(optional_params.get("thinking")) - else () - ) + thinking_betas: Final = AnthropicModelInfo().thinking_betas(optional_params.get("thinking")) tool_change_betas: Final = ( (ANTHROPIC_MID_CONVERSATION_TOOL_CHANGES_BETA_HEADER,) if AnthropicModelInfo().is_mid_conversation_tool_change_used(messages) else () ) - all_beta_values: Final = beta_values.union(thinking_display_betas, tool_change_betas) + all_beta_values: Final = beta_values.union(thinking_betas, tool_change_betas) dangerous_tool_use: Final = ANTHROPIC_BETA_HEADER_VALUES.DANGEROUS_TOOL_USE_2026_09_03.value forwarded_beta_values: Final = ( # pyright: ignore[reportUnknownVariableType] # derived from the bare beta set all_beta_values - {dangerous_tool_use} diff --git a/litellm/llms/anthropic/messages/responses_adapters/transformation.py b/litellm/llms/anthropic/messages/responses_adapters/transformation.py index 1dd07c1739b..94c608f90e6 100644 --- a/litellm/llms/anthropic/messages/responses_adapters/transformation.py +++ b/litellm/llms/anthropic/messages/responses_adapters/transformation.py @@ -13,6 +13,7 @@ from typing import Any, Final, cast from litellm.litellm_core_utils.prompt_templates.common_utils import ( TOOL_RESULT_IMAGE_BOUNDARY, TOOL_RESULT_IMAGE_PLACEHOLDER, + anthropic_inline_document, encrypted_reasoning_signature, responses_reasoning_items_from_thinking_blocks, with_prompt_cache_breakpoint, @@ -106,27 +107,18 @@ class LiteLLMAnthropicToResponsesAPIAdapter: block: Mapping[str, object], ) -> dict[str, str] | None: # mutable-ok: API message payload """Convert an Anthropic document block to a Responses input_file part.""" + inline_document: Final = anthropic_inline_document(block) + if inline_document is not None: + return { + "type": "input_file", + "filename": inline_document.filename, + "file_data": inline_document.file_data, + } raw_source: Final = block.get("source") if not isinstance(raw_source, Mapping): return None source: Final = cast(Mapping[str, object], raw_source) # cast-ok: untrusted client payload - source_type: Final = source.get("type") - if source_type == "base64": - data: Final = source.get("data") - if not isinstance(data, str) or not data: - return None - raw_media_type: Final = source.get("media_type") - media_type: Final = ( - raw_media_type if isinstance(raw_media_type, str) and raw_media_type else "application/pdf" - ) - raw_title: Final = block.get("title") - filename: Final = raw_title if isinstance(raw_title, str) and raw_title else "document.pdf" - return { - "type": "input_file", - "filename": filename, - "file_data": f"data:{media_type};base64,{data}", - } - if source_type == "url": + if source.get("type") == "url": url: Final = source.get("url") if not isinstance(url, str) or not url: return None diff --git a/litellm/llms/anthropic/prompt_cache_prediction.py b/litellm/llms/anthropic/prompt_cache_prediction.py index 17c178d169e..27e003ce8b6 100644 --- a/litellm/llms/anthropic/prompt_cache_prediction.py +++ b/litellm/llms/anthropic/prompt_cache_prediction.py @@ -661,12 +661,13 @@ def prepare_native_baseline_body(request: Mapping[str, object], model: str) -> M TypeAdapter(tuple[str, ...]).validate_python(owned.get("additional_drop_params") or ()), dict(options), ) - body: Final = AnthropicMessagesConfig().transform_anthropic_messages_request( + config: Final = AnthropicMessagesConfig() + body: Final = config.transform_anthropic_messages_request( model=resolved_model, messages=strip_provider_specific_fields_from_anthropic_messages(messages), anthropic_messages_optional_request_params=filtered, litellm_params=GenericLiteLLMParams(), - headers={}, + headers=config.finalize_messages_headers({}, dict(filtered), list(messages)), # pyright: ignore[reportUnknownMemberType] # the shared helper takes and returns bare dicts ) return MappingProxyType(_JSON_OBJECT.validate_python(body)) except Exception: # noqa: BLE001 # an unsupported hypothetical request is unavailable, never an inference failure diff --git a/litellm/llms/azure/chat/gpt_reasoning_transformation.py b/litellm/llms/azure/chat/gpt_reasoning_transformation.py index 5bd03f511c5..72a0500e0af 100644 --- a/litellm/llms/azure/chat/gpt_reasoning_transformation.py +++ b/litellm/llms/azure/chat/gpt_reasoning_transformation.py @@ -24,6 +24,7 @@ class AzureOpenAIGPT5Config(AzureOpenAIConfig, OpenAIGPT5Config): GPT5_SERIES_ROUTE = "gpt5_series/" OPT_IN_REASONING_EFFORTS: ClassVar[frozenset[str]] = frozenset(("xhigh", "none")) + LOOKS_UP_BASE_MODEL: ClassVar[bool] = True @classmethod def _model_map_lookup_name(cls, model: str) -> str: @@ -88,7 +89,7 @@ class AzureOpenAIGPT5Config(AzureOpenAIConfig, OpenAIGPT5Config): drop_params: bool, api_version: str = "", *, - declared_reasoning_efforts: Collection[str] = (), + declared_reasoning_efforts: Collection[str] | None = None, ) -> dict: return OpenAIGPT5Config.map_openai_params( self, diff --git a/litellm/llms/azure_ai/anthropic/handler.py b/litellm/llms/azure_ai/anthropic/handler.py index ca4dad5ee3e..1a2003e6262 100644 --- a/litellm/llms/azure_ai/anthropic/handler.py +++ b/litellm/llms/azure_ai/anthropic/handler.py @@ -4,11 +4,12 @@ Azure Anthropic handler - reuses AnthropicChatCompletion logic with Azure authen import copy import json -from collections.abc import Callable +from collections.abc import Callable, Mapping from typing import Final import httpx +from litellm.anthropic_beta_gated_fields import strip_beta_gated_request_body from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj from litellm.llms.anthropic.chat.handler import AnthropicChatCompletion from litellm.llms.anthropic.chat.transformation import JsonModeRequest @@ -75,13 +76,22 @@ class AzureAnthropicChatCompletion(AnthropicChatCompletion): litellm_params=litellm_params, ) - data: Final = config.transform_request( + request_body: Final[Mapping[str, object]] = config.transform_request( model=model, messages=messages, optional_params=optional_params, litellm_params=litellm_params, headers=headers, ) + data: Final = dict( + strip_beta_gated_request_body( + request_body, + headers, # pyright: ignore[reportUnknownArgumentType] # validate_environment returns a bare dict + drop_params=litellm_params.get("drop_params"), # pyright: ignore[reportUnknownMemberType, reportUnknownArgumentType] # litellm_params is the handler's bare dict + model=model, + provider=custom_llm_provider, + ) + ) ## LOGGING logging_obj.pre_call( diff --git a/litellm/llms/azure_ai/anthropic/messages_transformation.py b/litellm/llms/azure_ai/anthropic/messages_transformation.py index 6b2da96de23..1f9ded827b6 100644 --- a/litellm/llms/azure_ai/anthropic/messages_transformation.py +++ b/litellm/llms/azure_ai/anthropic/messages_transformation.py @@ -145,6 +145,8 @@ class AzureAnthropicMessagesConfig(AnthropicMessagesConfig): litellm_params=litellm_params, headers=headers, ) - self._normalize_system_role_messages(anthropic_messages_request, model=model) + self._normalize_system_role_messages( + anthropic_messages_request, model=model, drop_params=getattr(litellm_params, "drop_params", None) + ) self._remove_scope_from_cache_control(anthropic_messages_request) return anthropic_messages_request diff --git a/litellm/llms/azure_ai/anthropic/transformation.py b/litellm/llms/azure_ai/anthropic/transformation.py index c75213c3e2c..6516827359d 100644 --- a/litellm/llms/azure_ai/anthropic/transformation.py +++ b/litellm/llms/azure_ai/anthropic/transformation.py @@ -107,9 +107,11 @@ class AzureAnthropicConfig(AnthropicConfig): is_vertex_request=optional_params.get("is_vertex_request", False), user_anthropic_beta_headers=user_anthropic_beta_headers, mcp_server_used=mcp_server_used, + thinking=optional_params.get("thinking"), # pyright: ignore[reportUnknownMemberType, reportUnknownArgumentType] # optional_params is the base config's bare dict ) # Merge headers - Azure auth (api-key or Authorization) takes precedence - headers = {**anthropic_headers, **headers} + merged_beta_header: Final = {k: v for k, v in anthropic_headers.items() if k == "anthropic-beta"} + headers = {**anthropic_headers, **headers, **merged_beta_header} # Ensure anthropic-version header is set if "anthropic-version" not in headers: diff --git a/litellm/llms/azure_ai/chat/transformation.py b/litellm/llms/azure_ai/chat/transformation.py index 6aed7eb3c54..d382d653a53 100644 --- a/litellm/llms/azure_ai/chat/transformation.py +++ b/litellm/llms/azure_ai/chat/transformation.py @@ -1,7 +1,7 @@ import copy import enum import re -from collections.abc import AsyncIterator, Collection, Iterator +from collections.abc import AsyncIterator, Collection, Iterator, Mapping from typing import TYPE_CHECKING, Final, cast import httpx @@ -10,10 +10,11 @@ from httpx import Response import litellm from litellm._logging import verbose_logger from litellm.litellm_core_utils.prompt_templates.common_utils import ( - audio_or_image_in_message_content, + content_parts, convert_content_list_to_str, drop_tool_reference_parts_from_tool_messages, filter_value_from_dict, + is_pdf_file_part, ) from litellm.llms.azure.common_utils import BaseAzureLLM from litellm.llms.azure_ai.common_utils import ( @@ -46,6 +47,20 @@ NON_OPENAI_SPEC_MESSAGE_FIELDS: Final = ( "cache_control", ) +LIST_ONLY_CONTENT_PART_TYPES: Final = frozenset({"image_url", "input_audio"}) + + +def _is_list_only_content_part(part: object) -> bool: + match part: + case {"type": str() as part_type} if part_type in LIST_ONLY_CONTENT_PART_TYPES: + return True + case _: + return is_pdf_file_part(part) + + +def _has_list_only_content_part(message: Mapping[str, object]) -> bool: + return any(_is_list_only_content_part(part) for part in content_parts(message)) + class AzureAIChatCompletionResponseIterator(OpenAIChatCompletionResponseIterator): def chunk_parser(self, chunk: dict[str, object]) -> ModelResponseStream: # mutable-ok: mirrors override contract @@ -106,7 +121,7 @@ class AzureAIStudioConfig(OpenAIConfig): model: str, drop_params: bool, *, - declared_reasoning_efforts: Collection[str] = (), + declared_reasoning_efforts: Collection[str] | None = None, ) -> dict[str, object]: # mutable-ok: OpenAIConfig.map_openai_params signature if not azureAIGPT5Config.is_model_gpt_5_model(model): return super().map_openai_params( @@ -246,7 +261,7 @@ class AzureAIStudioConfig(OpenAIConfig): these with "Extra inputs are not permitted", which breaks multi-turn Anthropic-format clients that echo thinking blocks back as history. 2. Transforms list content to a string. - 3. If message contains an image or audio, send as is (user-intended) + 3. If message contains an image, audio, or PDF file part, send as is (user-intended) Operates on a deep copy so the caller's messages keep their thinking blocks and provider metadata, which a fallback to another provider still needs. @@ -257,8 +272,7 @@ class AzureAIStudioConfig(OpenAIConfig): for field in NON_OPENAI_SPEC_MESSAGE_FIELDS: filter_value_from_dict(message_dict, field) - # Do nothing if the message contains an image or audio - if audio_or_image_in_message_content(message): + if _has_list_only_content_part(message): continue texts = convert_content_list_to_str(message=message) diff --git a/litellm/llms/azure_ai/passthrough/transformation.py b/litellm/llms/azure_ai/passthrough/transformation.py index 35b7642d7de..2e5514b3975 100644 --- a/litellm/llms/azure_ai/passthrough/transformation.py +++ b/litellm/llms/azure_ai/passthrough/transformation.py @@ -5,8 +5,9 @@ from types import MappingProxyType from typing import TYPE_CHECKING, Final import httpx -from pydantic import TypeAdapter, ValidationError +from pydantic import ConfigDict, TypeAdapter, ValidationError +from litellm._lazy_imports import get_default_encoding from litellm._logging import verbose_logger from litellm.llms.azure_ai.common_utils import ( AzureFoundryModelInfo, @@ -22,10 +23,15 @@ from litellm.llms.base_llm.passthrough.transformation import ( relayed_body, strip_leading_model_segment, ) +from litellm.llms.cohere.embed.transformation import ( + CohereEmbeddingRowShape, + CohereImageTokenBilling, + cohere_embedding_response, +) from litellm.rust_bridge.ocr.entrypoints import NATIVE_OCR_PASSTHROUGH_RESPONSE, NativeOcrPassthroughResponse from litellm.types.llms.openai import AllMessageValues from litellm.types.rerank import RerankResponse -from litellm.types.utils import CallTypes, ImageResponse, StandardPassThroughResponseObject +from litellm.types.utils import CallTypes, EmbeddingResponse, ImageResponse, StandardPassThroughResponseObject if TYPE_CHECKING: from httpx import URL, Response @@ -35,6 +41,7 @@ if TYPE_CHECKING: EMPTY_QUERY: Final[Mapping[str, object]] = MappingProxyType({}) +ECHOED_TEXTS: Final = TypeAdapter(tuple[str, ...], config=ConfigDict(hide_input_in_errors=True)) def api_version_from(litellm_params: Mapping[str, object]) -> str | None: @@ -85,9 +92,29 @@ def relay_query_params( return MappingProxyType({**(request_query_params or EMPTY_QUERY), "api-version": api_version}) +def echoed_texts(body: Mapping[str, object]) -> tuple[str, ...]: + try: + return ECHOED_TEXTS.validate_python(body.get("texts")) + except ValidationError: + return () + + +def cohere_embed_relay_response(body: Mapping[str, object]) -> EmbeddingResponse: + return cohere_embedding_response( + body, + None, + get_default_encoding(), + echoed_texts(body), + CohereImageTokenBilling.FOLDED_INTO_INPUT_TOKENS, + CohereEmbeddingRowShape.TYPED, + ) + + FOUNDRY_RELAY_SHAPES: Final = ( RelayShape("/rerank", CallTypes.arerank, RerankResponse.model_validate), RelayShape("/providers/blackforestlabs/v1/flux-2-pro", CallTypes.aimage_generation, ImageResponse.model_validate), + RelayShape("/providers/cohere/v1/embed", CallTypes.aembedding, cohere_embed_relay_response), + RelayShape("/providers/cohere/v2/embed", CallTypes.aembedding, cohere_embed_relay_response), ) diff --git a/litellm/llms/azure_ai/responses/transformation.py b/litellm/llms/azure_ai/responses/transformation.py index da734cf8189..b19915648ab 100644 --- a/litellm/llms/azure_ai/responses/transformation.py +++ b/litellm/llms/azure_ai/responses/transformation.py @@ -232,7 +232,7 @@ class AzureAIAgentResponsesAPIConfig(AzureAIResponsesAPIConfig): model: str, drop_params: bool, *, - declared_reasoning_efforts: Collection[str], + declared_reasoning_efforts: Collection[str] | None, ) -> Mapping[str, object]: mapped: Final = super().map_openai_params_for_deployment( response_api_optional_params, model, drop_params, declared_reasoning_efforts=declared_reasoning_efforts diff --git a/litellm/llms/base_llm/realtime/transformation.py b/litellm/llms/base_llm/realtime/transformation.py index 1f4ad29fa74..f1539d16b30 100644 --- a/litellm/llms/base_llm/realtime/transformation.py +++ b/litellm/llms/base_llm/realtime/transformation.py @@ -4,7 +4,6 @@ from types import TracebackType from typing import TYPE_CHECKING, Any, Protocol import httpx -from typing_extensions import Self from litellm.types.llms.openai import OpenAIRealtimeStreamSessionEvents from litellm.types.realtime import ( @@ -24,7 +23,7 @@ else: class RealtimeBackend(Protocol): - async def __aenter__(self) -> Self: ... + async def __aenter__(self) -> object: ... async def __aexit__( self, @@ -97,9 +96,15 @@ class BaseRealtimeConfig(ABC): def unbilled_usage_on_session_close(self, model: str) -> RealtimeInputAudioTranscriptionUsage | None: return None + def session_refusal(self) -> Exception | None: + return None + async def open_backend(self, url: str, headers: Mapping[str, str]) -> RealtimeBackend | None: return None + def wrap_backend(self, backend: RealtimeBackend) -> RealtimeBackend: + return backend + def transform_session_created_event( self, model: str, diff --git a/litellm/llms/base_llm/vector_store/transformation.py b/litellm/llms/base_llm/vector_store/transformation.py index 6885183aeba..4924c5a5acc 100644 --- a/litellm/llms/base_llm/vector_store/transformation.py +++ b/litellm/llms/base_llm/vector_store/transformation.py @@ -226,6 +226,9 @@ class BaseVectorStoreConfig: ``get_complete_url`` adds a query string, like Azure, override this.""" return f"{api_base}/{encoded_vector_store_id}" + def get_vector_store_list_url(self, api_base: str) -> str: + return api_base + def get_error_class(self, error_message: str, status_code: int, headers: dict | httpx.Headers) -> BaseLLMException: from ..chat.transformation import BaseLLMException diff --git a/litellm/llms/bedrock/chat/converse_transformation.py b/litellm/llms/bedrock/chat/converse_transformation.py index f54c352fd6e..443e722168d 100644 --- a/litellm/llms/bedrock/chat/converse_transformation.py +++ b/litellm/llms/bedrock/chat/converse_transformation.py @@ -17,6 +17,8 @@ from typing_extensions import ReadOnly, TypedDict import litellm from litellm._logging import verbose_logger +from litellm.anthropic_beta_gated_fields import active_beta_values, log_beta_gated_strips, strip_beta_gated_thinking +from litellm.anthropic_beta_headers_manager import filter_and_transform_beta_headers from litellm.constants import ( BEDROCK_MIN_THINKING_BUDGET_TOKENS, RESPONSE_FORMAT_TOOL_NAME, @@ -117,6 +119,7 @@ from litellm.utils import ( ) from ..common_utils import ( + DROP_DISABLED_REASONING_EFFORT_WARNING, BedrockError, BedrockModelInfo, bedrock_cache_point_ttl_supported, @@ -1192,11 +1195,7 @@ class AmazonConverseConfig(BaseConfig): ), status_code=400, ) - verbose_logger.debug( - "Dropping unsupported `reasoning_effort=%s` for Bedrock model=%s.", - value, - model, - ) + verbose_logger.warning(DROP_DISABLED_REASONING_EFFORT_WARNING, "reasoning_effort", value, model) elif param == "reasoning_effort" and isinstance(value, str): self._handle_reasoning_effort_parameter( model=model, reasoning_effort=value, optional_params=optional_params @@ -1828,7 +1827,16 @@ class AmazonConverseConfig(BaseConfig): filtered_tools.append(tool) base_model: Final = BedrockModelInfo.get_base_model(model) - client_beta_list: Final = get_anthropic_beta_from_headers(headers or {}) + header_beta_list: Final = get_anthropic_beta_from_headers(headers or {}) + client_beta_list: Final = header_beta_list or filter_and_transform_beta_headers( + sorted( + active_beta_values( + {}, + additional_request_params.get("anthropic_beta"), # pyright: ignore[reportUnknownMemberType, reportUnknownArgumentType] # additional_request_params is a bare dict + ) + ), + provider="bedrock_converse", + ) eager_beta: Final = ( (ANTHROPIC_FINE_GRAINED_TOOL_STREAMING_BETA_HEADER,) if base_model.startswith("anthropic") @@ -1836,7 +1844,15 @@ class AmazonConverseConfig(BaseConfig): and ANTHROPIC_FINE_GRAINED_TOOL_STREAMING_BETA_HEADER not in client_beta_list else () ) - anthropic_beta_list: Final = [*client_beta_list, *eager_beta] + thinking_betas: Final = AnthropicModelInfo().thinking_betas( + additional_request_params.get("thinking"), # pyright: ignore[reportUnknownMemberType, reportUnknownArgumentType] # additional_request_params is a bare dict + ) + missing_thinking_betas: Final = ( + tuple(beta for beta in thinking_betas if beta not in client_beta_list) + if base_model.startswith("anthropic") + else () + ) + anthropic_beta_list: Final = [*client_beta_list, *eager_beta, *missing_thinking_betas] # Only separate tools if computer use tools are actually present if filtered_tools and self.is_computer_use_tool_used(filtered_tools, model): @@ -1906,7 +1922,8 @@ class AmazonConverseConfig(BaseConfig): else: computer_use_header = "computer-use-2024-10-22" - anthropic_beta_list.append(computer_use_header) + if computer_use_header not in anthropic_beta_list: + anthropic_beta_list.append(computer_use_header) # Transform computer use tools to proper Bedrock format transformed_computer_tools: Final = self._transform_computer_use_tools(computer_use_tools) additional_request_params["tools"] = transformed_computer_tools @@ -2031,6 +2048,16 @@ class AmazonConverseConfig(BaseConfig): bedrock_tools, anthropic_beta_list = self._process_tools_and_beta( original_tools, model, headers, additional_request_params, litellm_params, deployment_model ) + requested_thinking: Final = additional_request_params.get("thinking") # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType] # _prepare_request_params hands back a bare dict + body_betas: Final = additional_request_params.get("anthropic_beta") # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType] # same bare dict + gated_thinking: Final = strip_beta_gated_thinking( + requested_thinking, # pyright: ignore[reportUnknownArgumentType] # same bare dict + active_beta_values({}, body_betas) | frozenset(anthropic_beta_list), # pyright: ignore[reportUnknownArgumentType] # _process_tools_and_beta hands back a bare list + drop_params=drop_params, + ) + if gated_thinking.stripped: + log_beta_gated_strips(gated_thinking.stripped, model=model, provider="bedrock_converse") + additional_request_params["thinking"] = gated_thinking.thinking # Append cachePoint to tools if cache_control_injection_points has tool_config cache_injection_points: Final = additional_request_params.pop("cache_control_injection_points", None) diff --git a/litellm/llms/bedrock/chat/invoke_transformations/anthropic_claude_transformation.py b/litellm/llms/bedrock/chat/invoke_transformations/anthropic_claude_transformation.py index eb6ed5c75e3..e32df0bbdbb 100644 --- a/litellm/llms/bedrock/chat/invoke_transformations/anthropic_claude_transformation.py +++ b/litellm/llms/bedrock/chat/invoke_transformations/anthropic_claude_transformation.py @@ -4,6 +4,7 @@ from typing import TYPE_CHECKING, Any, Final import httpx from pydantic import TypeAdapter +from litellm.anthropic_beta_gated_fields import strip_beta_gated_request_body from litellm.anthropic_beta_headers_manager import filter_and_transform_beta_headers from litellm.litellm_core_utils.llm_cost_calc.guardrail_cost import object_mapping from litellm.litellm_core_utils.prompt_templates.factory import ( @@ -210,7 +211,15 @@ class AmazonAnthropicClaudeConfig(AmazonInvokeConfig, AnthropicConfig): if beta_list: _anthropic_request["anthropic_beta"] = beta_list - return _anthropic_request + return dict( + strip_beta_gated_request_body( + _anthropic_request, # pyright: ignore[reportUnknownArgumentType] # built by the base signature's bare dict transform + {}, + drop_params=litellm_params.get("drop_params"), # pyright: ignore[reportUnknownMemberType, reportUnknownArgumentType] # litellm_params is the base signature's bare dict + model=model, + provider="bedrock_invoke", + ) + ) @property def uses_async_transform_request(self) -> bool: @@ -294,7 +303,6 @@ class AmazonAnthropicClaudeConfig(AmazonInvokeConfig, AnthropicConfig): programmatic_tool_calling_used: Final = self.is_programmatic_tool_calling_used(tools) input_examples_used: Final = self.is_input_examples_used(tools) is_mid_conversation_output_config_used: Final = self.is_mid_conversation_output_config_used(messages) - is_thinking_display_updates_used: Final = self.is_thinking_display_updates_used(thinking) client_beta_set: Final = set(get_anthropic_beta_from_headers(headers)) user_beta_set: Final = ( @@ -312,7 +320,7 @@ class AmazonAnthropicClaudeConfig(AmazonInvokeConfig, AnthropicConfig): mcp_server_used=self.is_mcp_server_used(optional_params.get("mcp_servers")), custom_llm_provider="bedrock", is_mid_conversation_output_config_used=is_mid_conversation_output_config_used, - is_thinking_display_updates_used=is_thinking_display_updates_used, + thinking=thinking, ) beta_set.update(auto_betas) diff --git a/litellm/llms/bedrock/claude_platform/transformation.py b/litellm/llms/bedrock/claude_platform/transformation.py index 0194f1c7e45..a5ecc37d394 100644 --- a/litellm/llms/bedrock/claude_platform/transformation.py +++ b/litellm/llms/bedrock/claude_platform/transformation.py @@ -72,6 +72,7 @@ class BedrockClaudePlatformConfig(BedrockClaudePlatformMixin, AnthropicConfig): ), code_execution_tool_used=self.is_code_execution_tool_used(tools=optional_params.get("tools")), container_with_skills_used=self.is_container_with_skills_used(optional_params=optional_params), + thinking=optional_params.get("thinking"), # pyright: ignore[reportUnknownMemberType, reportUnknownArgumentType] # optional_params is the base config's bare dict ) anthropic_headers["anthropic-workspace-id"] = workspace_id return {**headers, **anthropic_headers} diff --git a/litellm/llms/bedrock/common_utils.py b/litellm/llms/bedrock/common_utils.py index dd721e059a9..4175479f068 100644 --- a/litellm/llms/bedrock/common_utils.py +++ b/litellm/llms/bedrock/common_utils.py @@ -1331,6 +1331,12 @@ def _mantle_api_base_from_env() -> str | None: return next((base[: -len(suffix)] for suffix in _MANTLE_OPENAI_BASE_SUFFIXES if base.endswith(suffix)), base) +DROP_DISABLED_REASONING_EFFORT_WARNING: Final = ( + "Dropping %s=%r for Bedrock model=%r (drop_params=True) because the model map marks that level unsupported. " + "Send a level the model supports." +) + + def bedrock_reasoning_effort_disabled(model: str, effort: str) -> bool: from litellm.utils import is_explicitly_disabled_factory diff --git a/litellm/llms/bedrock/embed/embedding.py b/litellm/llms/bedrock/embed/embedding.py index 4d27d94664f..2f0ed4d0e82 100644 --- a/litellm/llms/bedrock/embed/embedding.py +++ b/litellm/llms/bedrock/embed/embedding.py @@ -9,9 +9,11 @@ from collections.abc import Callable, Mapping from typing import TYPE_CHECKING, Final, get_args, overload import httpx +from pydantic import TypeAdapter import litellm from litellm.constants import BEDROCK_EMBEDDING_PROVIDERS_LITERAL +from litellm.litellm_core_utils.hidden_params import set_hidden_param from litellm.litellm_core_utils.url_utils import join_api_base from litellm.llms.cohere.embed.handler import embedding as cohere_embedding from litellm.llms.custom_httpx.http_handler import ( @@ -36,7 +38,7 @@ from ..base_aws_llm import ( pop_aws_auth_params, run_aws_signing, ) -from ..common_utils import BedrockError +from ..common_utils import BedrockError, split_bedrock_region_path from .amazon_nova_transformation import AmazonNovaEmbeddingConfig from .amazon_titan_g1_transformation import AmazonTitanG1Config from .amazon_titan_multimodal_transformation import ( @@ -64,12 +66,21 @@ def _sign_get_request( return request.prepare() +_OPTIONAL_STR: Final[TypeAdapter[str | None]] = TypeAdapter(str | None) + + +def _without_region_path(model: str) -> str: + region, model_id = split_bedrock_region_path(model) + return model if region is None else model_id + + class BedrockEmbedding(BaseAWSLLM): @overload def _load_credentials( self, optional_params: dict, # mutable-ok: the implementation pops the aws_* keys out of the caller's dict in place bearer_token: None = None, + region_from_model: str | None = None, ) -> tuple[Credentials, str]: ... @overload @@ -77,15 +88,18 @@ class BedrockEmbedding(BaseAWSLLM): self, optional_params: dict, # mutable-ok: the implementation pops the aws_* keys out of the caller's dict in place bearer_token: str, + region_from_model: str | None = None, ) -> tuple[None, str]: ... def _load_credentials( self, optional_params: dict, bearer_token: str | None = None, + region_from_model: str | None = None, ) -> tuple[Credentials | None, str]: auth_params: Final = pop_aws_auth_params(optional_params) - aws_region_name = optional_params.pop("aws_region_name", None) + explicit_region_name: Final = optional_params.pop("aws_region_name", None) + aws_region_name = region_from_model if explicit_region_name is None else explicit_region_name ### SET REGION NAME ### if aws_region_name is None: @@ -184,14 +198,17 @@ class BedrockEmbedding(BaseAWSLLM): provider: BEDROCK_EMBEDDING_PROVIDERS_LITERAL, is_async_invoke: bool | None = False, batch_data: list[dict] | None = None, + aws_region_name: str | None = None, ) -> EmbeddingResponse | None: """ Transforms the response from the Bedrock embedding provider to the OpenAI format. """ returned_response: EmbeddingResponse | None = None + bedrock_model: Final = _without_region_path(model) + is_async_job: Final = bool(is_async_invoke) and len(response_list) == 1 and "invocationArn" in response_list[0] # Handle async invoke responses (single response with invocationArn) - if is_async_invoke and len(response_list) == 1 and "invocationArn" in response_list[0]: + if is_async_job: if provider == "twelvelabs": returned_response = TwelveLabsMarengoEmbeddingConfig().transform_async_invoke_response( response=response_list[0], model=model @@ -227,15 +244,15 @@ class BedrockEmbedding(BaseAWSLLM): ) else: # Handle regular invoke responses - if model == "amazon.titan-embed-image-v1": + if bedrock_model == "amazon.titan-embed-image-v1": returned_response = AmazonTitanMultimodalEmbeddingG1Config().transform_response( response_list=response_list, model=model, batch_data=batch_data ) - elif model == "amazon.titan-embed-text-v1": + elif bedrock_model == "amazon.titan-embed-text-v1": returned_response = AmazonTitanG1Config().transform_response(response_list=response_list, model=model) - elif model == "amazon.titan-embed-text-v2:0": + elif bedrock_model == "amazon.titan-embed-text-v2:0": returned_response = AmazonTitanV2Config().transform_response(response_list=response_list, model=model) - elif model == "amazon.titan-embed-g1-text-02": + elif bedrock_model == "amazon.titan-embed-g1-text-02": returned_response = AmazonTitanG1Config().transform_response(response_list=response_list, model=model) elif provider == "twelvelabs": returned_response = TwelveLabsMarengoEmbeddingConfig().transform_response( @@ -251,6 +268,8 @@ class BedrockEmbedding(BaseAWSLLM): ########################################################## if returned_response is None: raise Exception(f"Unable to map model response to known provider format. model={model}") + if aws_region_name is not None and not is_async_job: + set_hidden_param(returned_response, "region_name", aws_region_name) return returned_response def _single_func_embeddings( @@ -319,6 +338,7 @@ class BedrockEmbedding(BaseAWSLLM): provider=provider, is_async_invoke=is_async_invoke, batch_data=batch_data, + aws_region_name=aws_region_name, ) async def _async_single_func_embeddings( @@ -390,6 +410,7 @@ class BedrockEmbedding(BaseAWSLLM): provider=provider, is_async_invoke=is_async_invoke, batch_data=batch_data, + aws_region_name=aws_region_name, ) def embeddings( @@ -409,26 +430,29 @@ class BedrockEmbedding(BaseAWSLLM): litellm_params: dict, api_key: str | None = None, ) -> EmbeddingResponse: + model_id_param: Final = _OPTIONAL_STR.validate_python(optional_params.pop("model_id", None)) or None + has_async_invoke: Final = "async_invoke/" in model + model_without_async_route: Final = model.replace("async_invoke/", "", 1) + path_region: Final = split_bedrock_region_path(model_without_async_route)[0] if model_id_param is None else None + bedrock_model: Final = _without_region_path(model_without_async_route) credentials, aws_region_name = self._load_credentials( - optional_params, bearer_token=bedrock_bearer_token(api_key) + optional_params, + bearer_token=bedrock_bearer_token(api_key), + region_from_model=self._get_aws_region_from_model_arn(model_id_param) or path_region, ) ### TRANSFORMATION ### - unencoded_model_id: Final = optional_params.pop("model_id", None) or model # default to model if not passed + unencoded_model_id: Final = model_id_param or bedrock_model modelId: Final = urllib.parse.quote(unencoded_model_id, safe="") aws_region_name = self._get_aws_region_name( optional_params={"aws_region_name": aws_region_name}, - model=model, + model=bedrock_model, model_id=unencoded_model_id, ) - # Check async invoke needs to be used - has_async_invoke: Final = "async_invoke/" in model - if has_async_invoke: - model = model.replace("async_invoke/", "", 1) - provider: Final = self.get_bedrock_embedding_provider(model) + provider: Final = self.get_bedrock_embedding_provider(bedrock_model) if provider is None: raise Exception( - f"Unable to determine bedrock embedding provider for model: {model}. " + f"Unable to determine bedrock embedding provider for model: {bedrock_model}. " f"Supported providers: {list(get_args(BEDROCK_EMBEDDING_PROVIDERS_LITERAL))}" ) inference_params = copy.deepcopy(optional_params) @@ -441,9 +465,9 @@ class BedrockEmbedding(BaseAWSLLM): batch_data: list | None = None if provider == "cohere": data = BedrockCohereEmbeddingConfig().transform_request( - model=model, input=input, inference_params=inference_params + model=bedrock_model, input=input, inference_params=inference_params ) - elif provider == "amazon" and model in [ + elif provider == "amazon" and bedrock_model in [ "amazon.titan-embed-image-v1", "amazon.titan-embed-text-v1", "amazon.titan-embed-text-v2:0", @@ -451,28 +475,28 @@ class BedrockEmbedding(BaseAWSLLM): ]: batch_data = [] for i in input: - if model == "amazon.titan-embed-image-v1": + if bedrock_model == "amazon.titan-embed-image-v1": transformed_request: AmazonEmbeddingRequest = ( AmazonTitanMultimodalEmbeddingG1Config().transform_request( input=i, inference_params=inference_params ) ) - elif model == "amazon.titan-embed-text-v1": + elif bedrock_model == "amazon.titan-embed-text-v1": transformed_request = AmazonTitanG1Config().transform_request( input=i, inference_params=inference_params ) - elif model == "amazon.titan-embed-text-v2:0": + elif bedrock_model == "amazon.titan-embed-text-v2:0": transformed_request = AmazonTitanV2Config().transform_request( input=i, inference_params=inference_params ) - elif model == "amazon.titan-embed-g1-text-02": + elif bedrock_model == "amazon.titan-embed-g1-text-02": transformed_request = AmazonTitanG1Config().transform_request( input=i, inference_params=inference_params ) else: raise Exception( "Unmapped model. Received={}. Expected={}".format( - model, + bedrock_model, [ "amazon.titan-embed-image-v1", "amazon.titan-embed-text-v1", @@ -485,7 +509,7 @@ class BedrockEmbedding(BaseAWSLLM): elif provider == "twelvelabs": batch_data = [] for i in input: - twelvelabs_request = TwelveLabsMarengoEmbeddingConfig(model=model).transform_request( + twelvelabs_request = TwelveLabsMarengoEmbeddingConfig(model=bedrock_model).transform_request( input=i, inference_params=inference_params, async_invoke_route=has_async_invoke, @@ -527,7 +551,7 @@ class BedrockEmbedding(BaseAWSLLM): extra_headers=extra_headers, endpoint_url=endpoint_url, aws_region_name=aws_region_name, - model=model, + model=model_without_async_route, logging_obj=logging_obj, api_key=api_key, provider=provider, @@ -541,7 +565,7 @@ class BedrockEmbedding(BaseAWSLLM): extra_headers=extra_headers, endpoint_url=endpoint_url, aws_region_name=aws_region_name, - model=model, + model=model_without_async_route, logging_obj=logging_obj, api_key=api_key, provider=provider, @@ -570,8 +594,9 @@ class BedrockEmbedding(BaseAWSLLM): ## ROUTING ## # Convert CaseInsensitiveDict to regular dict for httpx compatibility headers_for_request: Final = dict(prepped.headers) if hasattr(prepped, "headers") else {} + set_hidden_param(model_response, "region_name", aws_region_name) return cohere_embedding( - model=model, + model=model_without_async_route, input=input, model_response=model_response, logging_obj=logging_obj, diff --git a/litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude_transformation.py b/litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude_transformation.py index 60ce73abd82..e5d6a168d57 100644 --- a/litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude_transformation.py +++ b/litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude_transformation.py @@ -7,6 +7,7 @@ from pydantic import TypeAdapter import litellm from litellm._logging import verbose_logger +from litellm.anthropic_beta_gated_fields import log_beta_gated_strips, strip_beta_gated_native_request from litellm.anthropic_beta_headers_manager import filter_and_transform_beta_headers from litellm.constants import ( BEDROCK_MIN_THINKING_BUDGET_TOKENS, @@ -549,6 +550,9 @@ class AmazonAnthropicClaudeMessagesConfig( "edits": retained_edits, } + def strips_beta_gated_fields_from_final_headers(self) -> bool: + return False + def _get_bedrock_invoke_anthropic_beta_headers( self, model: str, @@ -584,9 +588,7 @@ class AmazonAnthropicClaudeMessagesConfig( ), custom_llm_provider="bedrock", is_mid_conversation_output_config_used=is_mid_conversation_output_config_used, - is_thinking_display_updates_used=anthropic_model_info.is_thinking_display_updates_used( - anthropic_messages_request.get("thinking") - ), + thinking=anthropic_messages_request.get("thinking"), is_mid_conversation_tool_change_used=anthropic_model_info.is_mid_conversation_tool_change_used( outgoing_messages_typed ), @@ -757,7 +759,9 @@ class AmazonAnthropicClaudeMessagesConfig( ), "display": "updates", } - self._normalize_system_role_messages(anthropic_messages_request, model=model) + self._normalize_system_role_messages( + anthropic_messages_request, model=model, drop_params=getattr(litellm_params, "drop_params", None) + ) ######################################################### ############## BEDROCK Invoke SPECIFIC TRANSFORMATION ### ######################################################### @@ -825,6 +829,20 @@ class AmazonAnthropicClaudeMessagesConfig( if filtered_betas: anthropic_messages_request["anthropic_beta"] = filtered_betas + final_request: Final = OBJECT_MAPPING_ADAPTER.validate_python(anthropic_messages_request) # pyright: ignore[reportUnknownArgumentType] # built from the base signature's bare dict + gated: Final = strip_beta_gated_native_request( + {}, + final_request, + final_request.get("messages"), + body_betas=filtered_betas, + drop_params=getattr(litellm_params, "drop_params", None), + ) + log_beta_gated_strips(gated.stripped, model=model, provider="bedrock_invoke") + if gated.thinking_stripped: + anthropic_messages_request["thinking"] = gated.thinking + if gated.messages_stripped: + anthropic_messages_request["messages"] = list(gated.messages) + outbound_tools: Final = tools_without_eager_input_streaming(anthropic_messages_request) if outbound_tools is not None: anthropic_messages_request["tools"] = outbound_tools diff --git a/litellm/llms/bedrock/responses/transformation.py b/litellm/llms/bedrock/responses/transformation.py index 7cdd7b08f99..ff44d973d97 100644 --- a/litellm/llms/bedrock/responses/transformation.py +++ b/litellm/llms/bedrock/responses/transformation.py @@ -52,6 +52,7 @@ from litellm.llms.base_llm.responses.codex_compat import drop_unsupported_tools, from litellm.llms.bedrock.base_aws_llm import BaseAWSLLM from litellm.llms.bedrock.common_utils import ( BEDROCK_CHAT_COMPLETIONS_ROUTE_PREFIX, + DROP_DISABLED_REASONING_EFFORT_WARNING, BedrockError, bedrock_reasoning_effort_disabled, bedrock_supports_openai_responses, @@ -173,7 +174,7 @@ def _without_disabled_reasoning_effort( ), status_code=400, ) - verbose_logger.debug("Dropping unsupported `reasoning.effort=%s` for Bedrock model=%s.", effort, model) + verbose_logger.warning(DROP_DISABLED_REASONING_EFFORT_WARNING, "reasoning.effort", effort, model) rest: Final = {key: value for key, value in reasoning.items() if key != "effort"} without_reasoning: Final = {key: value for key, value in params.items() if key != "reasoning"} return {**without_reasoning, "reasoning": rest} if rest else without_reasoning diff --git a/litellm/llms/cohere/embed/transformation.py b/litellm/llms/cohere/embed/transformation.py index 590164ffa0f..1477e45997b 100644 --- a/litellm/llms/cohere/embed/transformation.py +++ b/litellm/llms/cohere/embed/transformation.py @@ -10,11 +10,13 @@ Convers Docs - https://docs.cohere.com/v2/reference/embed """ -from collections.abc import Iterable, Mapping, Sized -from typing import Final, Protocol, cast +from collections.abc import Iterable, Iterator, Mapping, Sequence, Sized +from enum import Enum +from itertools import chain, starmap +from typing import Final, Protocol, TypeAlias, cast import httpx -from pydantic import ConfigDict, TypeAdapter +from pydantic import BaseModel, ConfigDict, Field, TypeAdapter import litellm from litellm import COHERE_DEFAULT_EMBEDDING_INPUT_TYPE @@ -32,14 +34,117 @@ from litellm.utils import is_base64_encoded from ..common_utils import CohereError _JSON_OBJECT: Final = TypeAdapter(Mapping[str, object], config=ConfigDict(hide_input_in_errors=True)) -_EMBEDDINGS_BY_TYPE: Final = TypeAdapter(Mapping[str, Iterable[object]], config=ConfigDict(hide_input_in_errors=True)) _DECODED_JSON: Final = TypeAdapter(object) class _SupportsEncode(Protocol): """Tokenizer handle: the embedding usage path only encodes text to measure its token length.""" - def encode(self, text: str, /) -> Sized: ... + def encode_ordinary(self, text: str, /) -> Sized: ... + + +class CohereImageTokenBilling(Enum): + SEPARATE_FROM_INPUT_TOKENS = "separate_from_input_tokens" + FOLDED_INTO_INPUT_TOKENS = "folded_into_input_tokens" + + +class CohereEmbeddingRowShape(Enum): + OPENAI = "openai" + TYPED = "typed" + + +class _CohereBilledUnits(BaseModel): + model_config = ConfigDict(frozen=True, hide_input_in_errors=True) + + input_tokens: int | None = None + image_tokens: int | None = None + images: int | None = None + + +class _CohereEmbedMeta(BaseModel): + model_config = ConfigDict(frozen=True, hide_input_in_errors=True) + + billed_units: _CohereBilledUnits = Field(default_factory=_CohereBilledUnits) + + +_CohereEmbeddings: TypeAlias = Mapping[str, Sequence[object]] | Sequence[object] + + +class _CohereEmbedBody(BaseModel): + model_config = ConfigDict(frozen=True, hide_input_in_errors=True) + + embeddings: _CohereEmbeddings + meta: _CohereEmbedMeta = Field(default_factory=_CohereEmbedMeta) + + +def _embedding_rows(vectors: Sequence[object]) -> Iterator[Mapping[str, object]]: + return ({"object": "embedding", "index": index, "embedding": vector} for index, vector in enumerate(vectors)) + + +def _typed_embedding_rows(embedding_type: str, vectors: Sequence[object]) -> Iterator[Mapping[str, object]]: + return ({**row, "type": embedding_type} for row in _embedding_rows(vectors)) + + +def _response_rows(embeddings: _CohereEmbeddings, row_shape: CohereEmbeddingRowShape) -> Iterable[Mapping[str, object]]: + if not isinstance(embeddings, Mapping): + return _embedding_rows(embeddings) + match row_shape: + case CohereEmbeddingRowShape.OPENAI: + return chain.from_iterable(map(_embedding_rows, embeddings.values())) + case CohereEmbeddingRowShape.TYPED: + return chain.from_iterable(starmap(_typed_embedding_rows, embeddings.items())) + + +def _prompt_usage(prompt_tokens: int, prompt_tokens_details: PromptTokensDetailsWrapper | None) -> Usage: + return Usage( + prompt_tokens=prompt_tokens, + completion_tokens=0, + total_tokens=prompt_tokens, + prompt_tokens_details=prompt_tokens_details, + ) + + +def _billed_usage(billed_units: _CohereBilledUnits, image_billing: CohereImageTokenBilling) -> Usage: + match image_billing: + case CohereImageTokenBilling.SEPARATE_FROM_INPUT_TOKENS: + image_units: Final = (billed_units.image_tokens or 0) + (billed_units.images or 0) + return _prompt_usage( + (billed_units.input_tokens or 0) + image_units, + PromptTokensDetailsWrapper( + text_tokens=billed_units.input_tokens, + image_tokens=image_units or None, + image_count=billed_units.images, + ), + ) + case CohereImageTokenBilling.FOLDED_INTO_INPUT_TOKENS: + return _prompt_usage(billed_units.input_tokens or 0, None) + + +def _cohere_embed_usage( + billed_units: _CohereBilledUnits, + input_texts: Iterable[str], + encoding: _SupportsEncode, + image_billing: CohereImageTokenBilling, +) -> Usage: + if billed_units.input_tokens is None and billed_units.image_tokens is None and billed_units.images is None: + return _prompt_usage(sum(len(encoding.encode_ordinary(text)) for text in input_texts), None) + return _billed_usage(billed_units, image_billing) + + +def cohere_embedding_response( + body: Mapping[str, object], + model: str | None, + encoding: _SupportsEncode, + input_texts: Iterable[str], + image_billing: CohereImageTokenBilling, + row_shape: CohereEmbeddingRowShape, +) -> EmbeddingResponse: + cohere_body: Final = _CohereEmbedBody.model_validate(body) + return EmbeddingResponse( + model=model, + data=list(_response_rows(cohere_body.embeddings, row_shape)), + usage=_cohere_embed_usage(cohere_body.meta.billed_units, input_texts, encoding, image_billing), + ) class CohereEmbeddingConfig(BaseEmbeddingConfig): @@ -145,44 +250,16 @@ class CohereEmbeddingConfig(BaseEmbeddingConfig): ), ) - def _calculate_usage(self, input: list[str], encoding: _SupportsEncode, meta: dict) -> Usage: - input_tokens = 0 - - text_tokens: Final[int | None] = meta.get("billed_units", {}).get("input_tokens") - - image_tokens: Final[int | None] = meta.get("billed_units", {}).get("images") - - prompt_tokens_details: PromptTokensDetailsWrapper | None = None - if image_tokens is None and text_tokens is None: - for text in input: - input_tokens += len(encoding.encode(text)) - else: - prompt_tokens_details = PromptTokensDetailsWrapper( - image_tokens=image_tokens, - text_tokens=text_tokens, - ) - if image_tokens: - input_tokens += image_tokens - if text_tokens: - input_tokens += text_tokens - - return Usage( - prompt_tokens=input_tokens, - completion_tokens=0, - total_tokens=input_tokens, - prompt_tokens_details=prompt_tokens_details, - ) - - def _transform_response( + def transform_native_embed_response( self, response: httpx.Response, api_key: str | None, logging_obj: LiteLLMLoggingObj, - data: dict | CohereEmbeddingRequest, - model_response: EmbeddingResponse, + data: Mapping[str, object] | CohereEmbeddingRequest, model: str, encoding: _SupportsEncode, - input: list, + input: Sequence[str], + row_shape: CohereEmbeddingRowShape, ) -> EmbeddingResponse: response_json: Final = _DECODED_JSON.validate_python(response.json()) ## LOGGING @@ -192,38 +269,15 @@ class CohereEmbeddingConfig(BaseEmbeddingConfig): additional_args={"complete_input_dict": data}, original_response=response_json, ) - """ - response - { - 'object': "list", - 'data': [ - - ] - 'model', - 'usage' - } - """ - payload: Final = _JSON_OBJECT.validate_python(response_json) - embeddings: Final = _EMBEDDINGS_BY_TYPE.validate_python(payload["embeddings"]) - output_data: Final = [] - for k, embedding_list in embeddings.items(): - for idx, embedding in enumerate(embedding_list): - output_data.append({"object": "embedding", "index": idx, "embedding": embedding}) - model_response.object = "list" - model_response.data = output_data - model_response.model = model - input_tokens = 0 - for text in input: - input_tokens += len(encoding.encode(text)) - - setattr( - model_response, - "usage", - self._calculate_usage(input, encoding, dict(_JSON_OBJECT.validate_python(payload.get("meta", {})))), + return cohere_embedding_response( + _JSON_OBJECT.validate_python(response_json), + model, + encoding, + input, + CohereImageTokenBilling.SEPARATE_FROM_INPUT_TOKENS, + row_shape, ) - return model_response - def transform_embedding_response( self, model: str, @@ -235,15 +289,15 @@ class CohereEmbeddingConfig(BaseEmbeddingConfig): optional_params: dict, litellm_params: dict, ) -> EmbeddingResponse: - return self._transform_response( + return self.transform_native_embed_response( response=raw_response, api_key=api_key, logging_obj=logging_obj, data=request_data, - model_response=model_response, model=model, encoding=litellm.encoding, input=logging_obj.model_call_details["input"], + row_shape=CohereEmbeddingRowShape.OPENAI, ) def get_error_class(self, error_message: str, status_code: int, headers: dict | httpx.Headers) -> BaseLLMException: diff --git a/litellm/llms/custom_httpx/llm_http_handler.py b/litellm/llms/custom_httpx/llm_http_handler.py index 9700465ebc3..c970b507288 100644 --- a/litellm/llms/custom_httpx/llm_http_handler.py +++ b/litellm/llms/custom_httpx/llm_http_handler.py @@ -50,7 +50,11 @@ from litellm.litellm_core_utils.audio_utils.subtitle_utils import ( SUBTITLE_RESPONSE_FORMATS, synthesize_subtitle_document, ) -from litellm.litellm_core_utils.core_helpers import as_str_mapping, set_provider_response_headers_in_hidden_params +from litellm.litellm_core_utils.core_helpers import ( + as_str_mapping, + qualify_agentic_followup_model, + set_provider_response_headers_in_hidden_params, +) from litellm.litellm_core_utils.dot_notation_indexing import delete_nested_value from litellm.litellm_core_utils.get_litellm_params import AWS_CREDENTIAL_KWARGS_KEYS from litellm.litellm_core_utils.get_llm_provider_logic import get_provider_affinity_header @@ -2577,6 +2581,7 @@ class BaseLLMHTTPHandler: "preset_cache_key": None, "stream_response": {}, "model_info": kwargs.get("model_info"), + **litellm_params.per_second_pricing(), **vertex_location_params, **anthropic_messages_optional_request_params, }, @@ -6275,9 +6280,7 @@ class BaseLLMHTTPHandler: if patch.messages is None: raise ValueError("Agentic loop plan missing patched messages") - full_model_name = patch.model or model - if "/" not in full_model_name: - full_model_name = f"{custom_llm_provider}/{full_model_name}" + full_model_name: Final = qualify_agentic_followup_model(patch.model, model, custom_llm_provider) optional_params_for_followup: Final = dict(optional_params) optional_params_for_followup.update(patch.optional_params) @@ -6920,7 +6923,7 @@ class BaseLLMHTTPHandler: try: ssl_context: Final = realtime_ssl_for_url(url) provider_backend: Final = await provider_config.open_backend(url, headers) - backend_ws: Final = ( + backend_ws: Final = provider_config.wrap_backend( provider_backend if provider_backend is not None else await self._open_realtime_backend_ws(websockets, url, headers, ssl_context) @@ -6958,9 +6961,7 @@ class BaseLLMHTTPHandler: await backend_ws.send(_session_config) realtime_streaming.session_configuration_request = _session_config - # For providers that defer setup until client session.update, optionally - # send synthetic session.created to unblock clients waiting on connect. - if not provider_config.requires_session_configuration(): + if not _session_config: synthetic_session: Final = provider_config.transform_session_created_event( model=model, logging_session_id=logging_obj.litellm_trace_id, @@ -11149,7 +11150,7 @@ class BaseLLMHTTPHandler: litellm_params=dict(litellm_params), ) - url: Final = api_base + url: Final = vector_store_provider_config.get_vector_store_list_url(api_base) params: Final[dict[str, object]] = {} if after is not None: @@ -11228,7 +11229,7 @@ class BaseLLMHTTPHandler: litellm_params=dict(litellm_params), ) - url: Final = api_base + url: Final = vector_store_provider_config.get_vector_store_list_url(api_base) params: Final[dict[str, object]] = {} if after is not None: diff --git a/litellm/llms/dashscope/chat/transformation.py b/litellm/llms/dashscope/chat/transformation.py index 966d1a01b8d..dfff9cec298 100644 --- a/litellm/llms/dashscope/chat/transformation.py +++ b/litellm/llms/dashscope/chat/transformation.py @@ -10,6 +10,7 @@ from litellm.secret_managers.main import get_secret_str from litellm.types.llms.openai import AllMessageValues, ChatCompletionToolParam from ...openai.chat.gpt_transformation import OpenAIGPTConfig +from ..common_utils import resolve_dashscope_openai_api_base class DashScopeChatConfig(OpenAIGPTConfig): @@ -55,11 +56,9 @@ class DashScopeChatConfig(OpenAIGPTConfig): def _get_openai_compatible_provider_info( self, api_base: str | None, api_key: str | None ) -> tuple[str | None, str | None]: - api_base = ( - api_base or get_secret_str("DASHSCOPE_API_BASE") or "https://dashscope.aliyuncs.com/compatible-mode/v1" - ) + resolved_api_base: Final = resolve_dashscope_openai_api_base(api_base) dynamic_api_key: Final = api_key or get_secret_str("DASHSCOPE_API_KEY") - return api_base, dynamic_api_key + return resolved_api_base, dynamic_api_key def get_openai_compatible_provider_info( self, @@ -69,7 +68,7 @@ class DashScopeChatConfig(OpenAIGPTConfig): return self._get_openai_compatible_provider_info(api_base, api_key) def _resolve_chat_api_base(self, api_base: str | None) -> str: - return api_base or "https://dashscope.aliyuncs.com/compatible-mode/v1" + return resolve_dashscope_openai_api_base(api_base) def get_complete_url( self, diff --git a/litellm/llms/dashscope/common_utils.py b/litellm/llms/dashscope/common_utils.py index 3e2e718a469..5ad2aa17a3a 100644 --- a/litellm/llms/dashscope/common_utils.py +++ b/litellm/llms/dashscope/common_utils.py @@ -2,6 +2,7 @@ Common utilities for the DashScope LLM provider. """ +from dataclasses import dataclass from typing import TYPE_CHECKING, Final from urllib.parse import urlparse @@ -19,10 +20,123 @@ if TYPE_CHECKING: from litellm.llms.base_llm.videos.transformation import BaseVideoConfig DASHSCOPE_CHAT_COMPATIBLE_PATH: Final = "/compatible-mode/v1" -DASHSCOPE_RERANK_PATH: Final = "/compatible-api/v1/reranks" +DASHSCOPE_ANTHROPIC_PATH: Final = "/apps/anthropic" +DASHSCOPE_NATIVE_V1_PATH: Final = "/api/v1" +DASHSCOPE_COMPATIBLE_API_PATH: Final = "/compatible-api/v1" +DASHSCOPE_RERANK_PATH: Final = f"{DASHSCOPE_COMPATIBLE_API_PATH}/reranks" +DASHSCOPE_IMAGE_PATH: Final = "/api/v1/services/aigc/multimodal-generation/generation" +DASHSCOPE_DEFAULT_ORIGIN: Final = "https://dashscope.aliyuncs.com" + +_STANDARD_DASHSCOPE_HOSTS: Final = frozenset( + { + "dashscope.aliyuncs.com", + "dashscope-intl.aliyuncs.com", + "dashscope-us.aliyuncs.com", + } +) +_WORKSPACE_HOST_SUFFIX: Final = ".maas.aliyuncs.com" +_EXCLUDED_WORKSPACE_PREFIXES: Final = ("trial.", "token-plan.") +_PROTOCOL_PATH_PREFIXES: Final = ( + DASHSCOPE_CHAT_COMPATIBLE_PATH, + DASHSCOPE_ANTHROPIC_PATH, + DASHSCOPE_NATIVE_V1_PATH, + DASHSCOPE_COMPATIBLE_API_PATH, +) -def _rerank_base_for_chat_shaped_base(api_base: str | None) -> str | None: +@dataclass(frozen=True, slots=True) +class DashScopeEndpoints: + origin: str + openai: str + anthropic: str + native: str + + +def _is_dashscope_host(host: str) -> bool: + if host in _STANDARD_DASHSCOPE_HOSTS: + return True + return host.endswith(_WORKSPACE_HOST_SUFFIX) and not host.startswith(_EXCLUDED_WORKSPACE_PREFIXES) + + +def _dashscope_origin(api_base: str | None) -> str | None: + if not api_base: + return None + parsed: Final = urlparse(api_base) + if parsed.scheme not in ("http", "https") or not parsed.netloc: + return None + if parsed.query or parsed.fragment: + return None + host: Final = (parsed.hostname or "").lower() + if not _is_dashscope_host(host): + return None + path: Final = parsed.path.rstrip("/") + if path and not any(path == prefix or path.startswith(f"{prefix}/") for prefix in _PROTOCOL_PATH_PREFIXES): + return None + return f"{parsed.scheme}://{parsed.netloc}" + + +def resolve_dashscope_endpoints(api_base: str | None) -> DashScopeEndpoints | None: + """ + Derive DashScope's protocol-specific bases from any standard DashScope base. + + Accepts the domain root, an OpenAI-compatible base, an Anthropic Messages + base, or a native `/api/v1` base for `dashscope.aliyuncs.com`, + `dashscope-intl.aliyuncs.com`, `dashscope-us.aliyuncs.com`, and + `{workspace}.{region}.maas.aliyuncs.com`. Returns None for any other host + or path so callers keep the caller-supplied base untouched. + """ + origin: Final = _dashscope_origin(api_base) + if origin is None: + return None + return DashScopeEndpoints( + origin=origin, + openai=f"{origin}{DASHSCOPE_CHAT_COMPATIBLE_PATH}", + anthropic=f"{origin}{DASHSCOPE_ANTHROPIC_PATH}", + native=f"{origin}{DASHSCOPE_NATIVE_V1_PATH}", + ) + + +def is_dashscope_native_endpoint_url(api_base: str) -> bool: + return urlparse(api_base).path.rstrip("/").startswith(f"{DASHSCOPE_NATIVE_V1_PATH}/") + + +def resolve_dashscope_openai_api_base( + api_base: str | None, + env_var: str = "DASHSCOPE_API_BASE", + default: str = f"{DASHSCOPE_DEFAULT_ORIGIN}{DASHSCOPE_CHAT_COMPATIBLE_PATH}", +) -> str: + candidate: Final = api_base or get_secret_str(env_var) + if candidate and is_dashscope_native_endpoint_url(candidate): + return candidate + endpoints: Final = resolve_dashscope_endpoints(candidate) + if endpoints is not None: + return endpoints.openai + return candidate or default + + +def resolve_dashscope_anthropic_api_base( + api_base: str | None, + env_var: str = "DASHSCOPE_API_BASE", + default: str = f"{DASHSCOPE_DEFAULT_ORIGIN}{DASHSCOPE_ANTHROPIC_PATH}", +) -> str: + candidate: Final = api_base or get_secret_str(env_var) + endpoints: Final = resolve_dashscope_endpoints(candidate) + if endpoints is not None: + return endpoints.anthropic + return candidate or default + + +def dashscope_base_selects_anthropic_messages(api_base: str | None) -> bool: + candidate: Final = api_base or get_secret_str("DASHSCOPE_API_BASE") + if not candidate: + return False + path: Final = urlparse(candidate).path.rstrip("/") + if path.endswith(DASHSCOPE_ANTHROPIC_PATH) or f"{DASHSCOPE_ANTHROPIC_PATH}/" in path: + return True + return not path and resolve_dashscope_endpoints(candidate) is not None + + +def _chat_shaped_aliyun_origin(api_base: str | None) -> str | None: if api_base is None: return None parsed: Final = urlparse(api_base) @@ -30,14 +144,24 @@ def _rerank_base_for_chat_shaped_base(api_base: str | None) -> str | None: on_aliyun_host: Final = host == "aliyuncs.com" or host.endswith(".aliyuncs.com") if not on_aliyun_host or parsed.path.rstrip("/") != DASHSCOPE_CHAT_COMPATIBLE_PATH: return None - return f"{parsed.scheme}://{parsed.netloc}{DASHSCOPE_RERANK_PATH}" + return f"{parsed.scheme}://{parsed.netloc}" + + +def _is_compatible_api_base(api_base: str) -> bool: + path: Final = urlparse(api_base).path.rstrip("/") + return path == DASHSCOPE_COMPATIBLE_API_PATH or path.startswith(f"{DASHSCOPE_COMPATIBLE_API_PATH}/") def resolve_dashscope_family_rerank_api_base(api_base: str | None, env_var: str, default_rerank_base: str) -> str: - remapped: Final = _rerank_base_for_chat_shaped_base(api_base) - if api_base is not None and remapped is None: + if api_base is None: + return get_secret_str(env_var) or default_rerank_base + if _is_compatible_api_base(api_base): return api_base - return get_secret_str(env_var) or remapped or default_rerank_base + chat_origin: Final = _chat_shaped_aliyun_origin(api_base) + if chat_origin is not None: + return get_secret_str(env_var) or f"{chat_origin}{DASHSCOPE_RERANK_PATH}" + endpoints: Final = resolve_dashscope_endpoints(api_base) + return f"{endpoints.origin}{DASHSCOPE_RERANK_PATH}" if endpoints is not None else api_base def get_dashscope_family_embedding_config(custom_llm_provider: str) -> "BaseEmbeddingConfig": diff --git a/litellm/llms/dashscope/embed/transformation.py b/litellm/llms/dashscope/embed/transformation.py index 35132346261..0f01a4b3172 100644 --- a/litellm/llms/dashscope/embed/transformation.py +++ b/litellm/llms/dashscope/embed/transformation.py @@ -25,7 +25,7 @@ from litellm.secret_managers.main import get_secret_str from litellm.types.llms.openai import AllEmbeddingInputValues, AllMessageValues from litellm.types.utils import EmbeddingResponse, Usage -from ..common_utils import DashScopeError +from ..common_utils import DashScopeError, resolve_dashscope_openai_api_base DEFAULT_API_BASE: Final = "https://dashscope.aliyuncs.com/compatible-mode/v1" @@ -91,7 +91,7 @@ class DashScopeEmbeddingConfig(BaseEmbeddingConfig): return resolved_api_key def _resolve_embedding_api_base(self, api_base: str | None) -> str: - return api_base or get_secret_str("DASHSCOPE_API_BASE") or DEFAULT_API_BASE + return resolve_dashscope_openai_api_base(api_base, default=DEFAULT_API_BASE) def validate_environment( self, diff --git a/litellm/llms/dashscope/image_generation/transformation.py b/litellm/llms/dashscope/image_generation/transformation.py index d1faa6c6cd0..2f528b5a2c3 100644 --- a/litellm/llms/dashscope/image_generation/transformation.py +++ b/litellm/llms/dashscope/image_generation/transformation.py @@ -39,6 +39,13 @@ from litellm.types.llms.openai import ( ) from litellm.types.utils import ImageObject, ImageResponse +from ..common_utils import ( + DASHSCOPE_CHAT_COMPATIBLE_PATH, + DASHSCOPE_IMAGE_PATH, + is_dashscope_native_endpoint_url, + resolve_dashscope_endpoints, +) + if TYPE_CHECKING: from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj from litellm.litellm_core_utils.tokenizer import Encoding as Tokenizer @@ -53,8 +60,6 @@ _DECODED_JSON: Final = TypeAdapter(object) DEFAULT_API_BASE: Final = "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation" -CHAT_COMPATIBLE_MODE_PATH: Final = "/compatible-mode/v1" - # Maps OpenAI size strings (WxH) to DashScope size strings (W*H) OPENAI_TO_DASHSCOPE_SIZE: Final[dict] = { "256x256": "256*256", @@ -102,8 +107,14 @@ class DashScopeImageGenerationConfig(BaseImageGenerationConfig): raise ValueError("DASHSCOPE_API_KEY is not set") return resolved_api_key - def _resolve_image_api_base(self, image_api_base: str | None) -> str: - return image_api_base or get_secret_str("DASHSCOPE_API_BASE_IMAGE") or DEFAULT_API_BASE + def _image_api_base_env(self) -> str | None: + return get_secret_str("DASHSCOPE_API_BASE_IMAGE") + + def _default_image_api_base(self) -> str: + return DEFAULT_API_BASE + + def _configured_chat_api_base(self) -> str | None: + return get_secret_str("DASHSCOPE_API_BASE") def get_complete_url( self, @@ -114,10 +125,18 @@ class DashScopeImageGenerationConfig(BaseImageGenerationConfig): litellm_params: dict, stream: bool | None = None, ) -> str: - image_api_base: Final = ( - api_base if api_base and not api_base.rstrip("/").endswith(CHAT_COMPATIBLE_MODE_PATH) else None - ) - return self._resolve_image_api_base(image_api_base) + image_env_api_base: Final = self._image_api_base_env() + configured_api_base: Final = api_base or self._configured_chat_api_base() + if not configured_api_base or configured_api_base.rstrip("/").endswith(DASHSCOPE_CHAT_COMPATIBLE_PATH): + return image_env_api_base or self._default_image_api_base() + if is_dashscope_native_endpoint_url(configured_api_base): + return configured_api_base + endpoints: Final = resolve_dashscope_endpoints(configured_api_base) + if endpoints is None: + return configured_api_base + if not api_base and image_env_api_base: + return image_env_api_base + return f"{endpoints.origin}{DASHSCOPE_IMAGE_PATH}" def validate_environment( self, diff --git a/litellm/llms/dashscope/messages/__init__.py b/litellm/llms/dashscope/messages/__init__.py new file mode 100644 index 00000000000..08cd184404c --- /dev/null +++ b/litellm/llms/dashscope/messages/__init__.py @@ -0,0 +1,7 @@ +""" +DashScope Anthropic-compatible Messages Module +""" + +from .transformation import DashScopeAnthropicMessagesConfig + +__all__ = ["DashScopeAnthropicMessagesConfig"] diff --git a/litellm/llms/dashscope/messages/transformation.py b/litellm/llms/dashscope/messages/transformation.py new file mode 100644 index 00000000000..9d0d1db91c8 --- /dev/null +++ b/litellm/llms/dashscope/messages/transformation.py @@ -0,0 +1,96 @@ +""" +DashScope Anthropic-compatible Messages transformation config. +""" + +from typing import ClassVar, Final + +import litellm +from litellm.exceptions import AuthenticationError +from litellm.litellm_core_utils.url_utils import ensure_api_route +from litellm.llms.anthropic.messages.native.transformation import ( + AnthropicMessagesConfig, +) +from litellm.secret_managers.main import get_secret_str, normalize_nonempty_secret_str + +from ..common_utils import ( + dashscope_base_selects_anthropic_messages, + resolve_dashscope_anthropic_api_base, +) + + +class DashScopeAnthropicMessagesConfig(AnthropicMessagesConfig): + """ + DashScope exposes an Anthropic-compatible Messages API at + ``{origin}/apps/anthropic`` for every DashScope domain, including + ``dashscope.aliyuncs.com`` and ``{workspace}.{region}.maas.aliyuncs.com``. + """ + + _workload_identity_eligible: ClassVar[bool] = False + + @property + def custom_llm_provider(self) -> str | None: + return "dashscope" + + def should_strip_billing_metadata(self) -> bool: + return True + + def handles_web_search_natively(self) -> bool: + return False + + def serves_api_base(self, model: str, api_base: str | None) -> bool: + return dashscope_base_selects_anthropic_messages(api_base) + + @staticmethod + def get_api_key(api_key: str | None = None) -> str | None: + return ( + normalize_nonempty_secret_str(api_key) + or normalize_nonempty_secret_str(get_secret_str("DASHSCOPE_API_KEY")) + or normalize_nonempty_secret_str(litellm.api_key) + ) + + @staticmethod + def get_api_base(api_base: str | None = None) -> str: + return resolve_dashscope_anthropic_api_base(api_base) + + def validate_anthropic_messages_environment( + self, + headers: dict[str, str], # mutable-ok: mirrors the legacy base override signature + model: str, + messages: list[object], # mutable-ok: mirrors the legacy base override signature + optional_params: dict[str, object], # mutable-ok: mirrors the legacy base override signature + litellm_params: dict[str, object], # mutable-ok: mirrors the legacy base override signature + api_key: str | None = None, + api_base: str | None = None, + ) -> tuple[dict[str, str], str | None]: # mutable-ok: mirrors the base signature + resolved_api_key: Final = self.get_api_key(api_key=api_key) + if resolved_api_key is None: + present_headers: Final = {name.lower() for name in headers} + if "x-api-key" not in present_headers and "authorization" not in present_headers: + raise AuthenticationError( + message=( + "Missing DashScope API key. Set DASHSCOPE_API_KEY or pass api_key explicitly. " + "Anthropic credentials are never sent to a DashScope endpoint." + ), + llm_provider="dashscope", + model=model, + ) + return super().validate_anthropic_messages_environment( # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType, reportReturnType] # base signature uses an unparameterized dict + headers=headers, + model=model, + messages=messages, + optional_params=optional_params, + litellm_params=litellm_params, + api_key=resolved_api_key, + api_base=api_base, + ) + + def get_complete_url( + self, + api_base: str | None, + api_key: str | None, + model: str, + optional_params: dict[str, object], # mutable-ok: mirrors the base get_complete_url signature + litellm_params: dict[str, object], # mutable-ok: mirrors the base get_complete_url signature + stream: bool | None = None, + ) -> str: + return ensure_api_route(self.get_api_base(api_base=api_base), "v1/messages") diff --git a/litellm/llms/dashscope/qwen_ai_platform.py b/litellm/llms/dashscope/qwen_ai_platform.py index f7cc50cc82a..db01c8a0a15 100644 --- a/litellm/llms/dashscope/qwen_ai_platform.py +++ b/litellm/llms/dashscope/qwen_ai_platform.py @@ -3,7 +3,7 @@ from typing import Final from litellm.secret_managers.main import get_secret_str from .chat.transformation import DashScopeChatConfig -from .common_utils import resolve_dashscope_family_rerank_api_base +from .common_utils import resolve_dashscope_family_rerank_api_base, resolve_dashscope_openai_api_base from .embed.transformation import DashScopeEmbeddingConfig from .image_generation.transformation import DashScopeImageGenerationConfig from .rerank.transformation import DashScopeRerankConfig @@ -38,7 +38,7 @@ class QwenAIPlatformChatConfig(DashScopeChatConfig): return self._resolve_chat_api_base(api_base), _resolve_qwen_ai_platform_api_key(api_key) def _resolve_chat_api_base(self, api_base: str | None) -> str: - return api_base or get_secret_str("QWEN_AI_PLATFORM_API_BASE") or QWEN_AI_PLATFORM_API_BASE + return resolve_dashscope_openai_api_base(api_base, "QWEN_AI_PLATFORM_API_BASE", QWEN_AI_PLATFORM_API_BASE) class QwenAIPlatformEmbeddingConfig(DashScopeEmbeddingConfig): @@ -46,7 +46,7 @@ class QwenAIPlatformEmbeddingConfig(DashScopeEmbeddingConfig): return _require_qwen_ai_platform_api_key(api_key) def _resolve_embedding_api_base(self, api_base: str | None) -> str: - return api_base or get_secret_str("QWEN_AI_PLATFORM_API_BASE") or QWEN_AI_PLATFORM_API_BASE + return resolve_dashscope_openai_api_base(api_base, "QWEN_AI_PLATFORM_API_BASE", QWEN_AI_PLATFORM_API_BASE) class QwenAIPlatformRerankConfig(DashScopeRerankConfig): @@ -63,8 +63,14 @@ class QwenAIPlatformImageGenerationConfig(DashScopeImageGenerationConfig): def _resolve_api_key(self, api_key: str | None) -> str: return _require_qwen_ai_platform_api_key(api_key) - def _resolve_image_api_base(self, image_api_base: str | None) -> str: - return image_api_base or get_secret_str("QWEN_AI_PLATFORM_API_BASE_IMAGE") or QWEN_AI_PLATFORM_IMAGE_API_BASE + def _image_api_base_env(self) -> str | None: + return get_secret_str("QWEN_AI_PLATFORM_API_BASE_IMAGE") + + def _default_image_api_base(self) -> str: + return QWEN_AI_PLATFORM_IMAGE_API_BASE + + def _configured_chat_api_base(self) -> str | None: + return get_secret_str("QWEN_AI_PLATFORM_API_BASE") class QwenAIPlatformVideoConfig(DashScopeVideoConfig): diff --git a/litellm/llms/dashscope/qwencloud.py b/litellm/llms/dashscope/qwencloud.py index a12e21b0983..6ae42c62e42 100644 --- a/litellm/llms/dashscope/qwencloud.py +++ b/litellm/llms/dashscope/qwencloud.py @@ -3,7 +3,7 @@ from typing import Final from litellm.secret_managers.main import get_secret_str from .chat.transformation import DashScopeChatConfig -from .common_utils import resolve_dashscope_family_rerank_api_base +from .common_utils import resolve_dashscope_family_rerank_api_base, resolve_dashscope_openai_api_base from .embed.transformation import DashScopeEmbeddingConfig from .image_generation.transformation import DashScopeImageGenerationConfig from .rerank.transformation import DashScopeRerankConfig @@ -38,7 +38,7 @@ class QwenCloudChatConfig(DashScopeChatConfig): return self._resolve_chat_api_base(api_base), _resolve_qwencloud_api_key(api_key) def _resolve_chat_api_base(self, api_base: str | None) -> str: - return api_base or get_secret_str("QWENCLOUD_API_BASE") or QWENCLOUD_API_BASE + return resolve_dashscope_openai_api_base(api_base, "QWENCLOUD_API_BASE", QWENCLOUD_API_BASE) class QwenCloudEmbeddingConfig(DashScopeEmbeddingConfig): @@ -46,7 +46,7 @@ class QwenCloudEmbeddingConfig(DashScopeEmbeddingConfig): return _require_qwencloud_api_key(api_key) def _resolve_embedding_api_base(self, api_base: str | None) -> str: - return api_base or get_secret_str("QWENCLOUD_API_BASE") or QWENCLOUD_API_BASE + return resolve_dashscope_openai_api_base(api_base, "QWENCLOUD_API_BASE", QWENCLOUD_API_BASE) class QwenCloudRerankConfig(DashScopeRerankConfig): @@ -63,8 +63,14 @@ class QwenCloudImageGenerationConfig(DashScopeImageGenerationConfig): def _resolve_api_key(self, api_key: str | None) -> str: return _require_qwencloud_api_key(api_key) - def _resolve_image_api_base(self, image_api_base: str | None) -> str: - return image_api_base or get_secret_str("QWENCLOUD_API_BASE_IMAGE") or QWENCLOUD_IMAGE_API_BASE + def _image_api_base_env(self) -> str | None: + return get_secret_str("QWENCLOUD_API_BASE_IMAGE") + + def _default_image_api_base(self) -> str: + return QWENCLOUD_IMAGE_API_BASE + + def _configured_chat_api_base(self) -> str | None: + return get_secret_str("QWENCLOUD_API_BASE") class QwenCloudVideoConfig(DashScopeVideoConfig): diff --git a/litellm/llms/dashscope/videos/transformation.py b/litellm/llms/dashscope/videos/transformation.py index 5c738423a5a..59e42e7b926 100644 --- a/litellm/llms/dashscope/videos/transformation.py +++ b/litellm/llms/dashscope/videos/transformation.py @@ -38,6 +38,8 @@ from litellm.types.videos.utils import ( ) from litellm.utils import get_model_info +from ..common_utils import resolve_dashscope_endpoints + if TYPE_CHECKING: from litellm.litellm_core_utils.litellm_logging import Logging from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler @@ -206,6 +208,9 @@ def _parse_task_response(raw_response: httpx.Response) -> _TaskResponse: def _normalized_api_base(api_base: str) -> str: + endpoints: Final = resolve_dashscope_endpoints(api_base) + if endpoints is not None: + return endpoints.origin trimmed: Final = api_base.rstrip("/") if trimmed.endswith(DASHSCOPE_COMPATIBLE_MODE_PATH): return trimmed[: -len(DASHSCOPE_COMPATIBLE_MODE_PATH)] diff --git a/litellm/llms/databricks/chat/transformation.py b/litellm/llms/databricks/chat/transformation.py index 48118c23ed7..60081c38073 100644 --- a/litellm/llms/databricks/chat/transformation.py +++ b/litellm/llms/databricks/chat/transformation.py @@ -10,6 +10,7 @@ from typing import TYPE_CHECKING, Any, Final, Literal, cast, overload import httpx from pydantic import BaseModel, ConfigDict, TypeAdapter +from litellm.anthropic_beta_gated_fields import log_beta_gated_strips, strip_beta_gated_thinking from litellm.constants import RESPONSE_FORMAT_TOOL_NAME from litellm.litellm_core_utils.llm_response_utils.convert_dict_to_response import ( handle_invalid_parallel_tool_calls, @@ -514,6 +515,14 @@ class DatabricksConfig(DatabricksBase, OpenAILikeChatConfig, AnthropicConfig): self.update_optional_params_with_thinking_tokens( non_default_params=non_default_params, optional_params=mapped_params ) + gated_thinking: Final = strip_beta_gated_thinking( + mapped_params.get("thinking"), # pyright: ignore[reportUnknownMemberType, reportUnknownArgumentType] # mapped_params is the base config's bare dict + frozenset(), + drop_params=drop_params, + ) + if gated_thinking.stripped: + log_beta_gated_strips(gated_thinking.stripped, model=model, provider="databricks") + mapped_params["thinking"] = gated_thinking.thinking return mapped_params diff --git a/litellm/llms/databricks/messages/transformation.py b/litellm/llms/databricks/messages/transformation.py index e22ad85b5f9..90b63d860d3 100644 --- a/litellm/llms/databricks/messages/transformation.py +++ b/litellm/llms/databricks/messages/transformation.py @@ -144,7 +144,7 @@ class DatabricksAnthropicMessagesConfig(DatabricksBase, AnthropicMessagesConfig) ("content-type" if key.lower() == "content-type" else key): value for key, value in {**auth_headers, **client_anthropic_headers}.items() } - finalized_headers: Final[dict[str, str]] = self._finalize_messages_headers( # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType] # the shared helper returns a bare dict # mutable-ok: becomes the outbound headers + finalized_headers: Final[dict[str, str]] = self.finalize_messages_headers( # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType] # the shared helper returns a bare dict # mutable-ok: becomes the outbound headers headers=normalized_headers, optional_params=optional_params, messages=messages, @@ -191,6 +191,8 @@ class DatabricksAnthropicMessagesConfig(DatabricksBase, AnthropicMessagesConfig) litellm_params=litellm_params, headers=headers, ) - self._normalize_system_role_messages(anthropic_messages_request, model=model) # pyright: ignore[reportUnknownMemberType] # the shared helper takes a bare dict + self._normalize_system_role_messages( # pyright: ignore[reportUnknownMemberType] # the shared helper takes a bare dict + anthropic_messages_request, model=model, drop_params=getattr(litellm_params, "drop_params", None) + ) self._remove_scope_from_cache_control(anthropic_messages_request) # pyright: ignore[reportUnknownMemberType] # the shared helper takes a bare dict return anthropic_messages_request diff --git a/litellm/llms/gigachat/chat/transformation.py b/litellm/llms/gigachat/chat/transformation.py index 65921b628fb..81a186eadf4 100644 --- a/litellm/llms/gigachat/chat/transformation.py +++ b/litellm/llms/gigachat/chat/transformation.py @@ -38,6 +38,15 @@ else: _EMPTY_FUNCTION: Final[Mapping[str, object]] = MappingProxyType({}) _JSON_VALUE: Final = TypeAdapter(object) +_CONTENT_PART: Final = TypeAdapter(Mapping[str, object]) + + +def _file_part_data(part: Mapping[str, object]) -> str | None: + match part: + case {"type": "file", "file": {"file_data": str() as file_data}} if file_data: + return file_data + case _: + return None def is_valid_json(value: str) -> bool: @@ -295,7 +304,7 @@ class GigaChatConfig(BaseConfig): def _transform_list_content(self, content: Sequence) -> tuple[str, Sequence[object]]: """ - Extract text and image attachments from a multimodal message content list. + Extract text, image and file attachments from a multimodal message content list. Args: content: List of content parts (OpenAI multimodal format) @@ -320,6 +329,10 @@ class GigaChatConfig(BaseConfig): file_id = self._upload_image(upload_url) if file_id: attachments.append(file_id) + elif (file_data := _file_part_data(_CONTENT_PART.validate_python(part))) and ( + uploaded_file_id := self._upload_image(file_data) + ): + attachments.append(uploaded_file_id) text: Final = "\n".join(texts) if texts else "" return text, attachments diff --git a/litellm/llms/oci/chat/transformation.py b/litellm/llms/oci/chat/transformation.py index e8d37f857e6..8257a75b4a5 100644 --- a/litellm/llms/oci/chat/transformation.py +++ b/litellm/llms/oci/chat/transformation.py @@ -20,6 +20,7 @@ import litellm from litellm._logging import verbose_logger from litellm.constants import DEFAULT_OCI_CHAT_MAX_TOKENS from litellm.litellm_core_utils.logging_utils import track_llm_api_timing +from litellm.litellm_core_utils.prompt_templates.common_utils import messages_with_file_parts_as_image_urls from litellm.litellm_core_utils.streaming_handler import CustomStreamWrapper from litellm.litellm_core_utils.url_utils import join_api_base from litellm.llms.base_llm.chat.transformation import BaseConfig, BaseLLMException @@ -681,7 +682,9 @@ class OCIChatConfig(BaseConfig): servingMode=serving_mode, chatRequest=OCIChatRequestPayload( apiFormat=vendor.value, - messages=adapt_messages_to_generic_oci_standard(messages), + messages=adapt_messages_to_generic_oci_standard( + list(messages_with_file_parts_as_image_urls(messages)) + ), **self._get_optional_params(vendor, optional_params, model), ), ) diff --git a/litellm/llms/openai/chat/gpt_reasoning_transformation.py b/litellm/llms/openai/chat/gpt_reasoning_transformation.py index 0311f6c6590..8c9551d01c0 100644 --- a/litellm/llms/openai/chat/gpt_reasoning_transformation.py +++ b/litellm/llms/openai/chat/gpt_reasoning_transformation.py @@ -5,8 +5,10 @@ from collections.abc import Collection from typing import ClassVar, Final import litellm +from litellm._logging import verbose_logger from litellm.utils import ( declared_value_factory, + has_catalog_entry, is_explicitly_disabled_factory, supports_factory, ) @@ -16,6 +18,25 @@ from .gpt_transformation import OpenAIGPTConfig _GPT_SERIES_VERSION: Final = re.compile(r"^gpt-(\d+)(?:\.(\d+))?(?=[.-]|$)") +def refused_reasoning_effort_warning(*, suggest_declared_levels: bool, suggest_base_model: bool) -> str: + return "".join( + ( + "Dropping %s=%r for model=%r (drop_params=True) because the model map does not mark that level " + "supported for %r.", + ( + " If the deployment supports it, list every level it supports in model_info.reasoning_effort_levels." + if suggest_declared_levels + else "" + ), + ( + " If the deployment name is not its underlying model, set model_info.base_model to that model." + if suggest_base_model + else "" + ), + ) + ) + + def _catalogue_declares_default_effort() -> bool: """Whether the loaded cost map carries default_reasoning_effort for ANY entry. @@ -93,6 +114,7 @@ class OpenAIGPT5Config(OpenAIGPTConfig): OPT_IN_REASONING_EFFORTS: ClassVar[frozenset[str]] = frozenset(("xhigh",)) ROUTE_SPECIFIC_REASONING_EFFORTS: ClassVar[frozenset[str]] = frozenset(("max",)) + LOOKS_UP_BASE_MODEL: ClassVar[bool] = False @classmethod def is_model_gpt_5_model(cls, model: str) -> bool: @@ -307,7 +329,7 @@ class OpenAIGPT5Config(OpenAIGPTConfig): model: str, drop_params: bool, *, - declared_reasoning_efforts: Collection[str] = (), + declared_reasoning_efforts: Collection[str] | None = None, ) -> dict: if self.is_model_gpt_5_search_model(model): if "max_tokens" in non_default_params: @@ -338,10 +360,21 @@ class OpenAIGPT5Config(OpenAIGPTConfig): if ( requested_effort is not None - and requested_effort not in declared_reasoning_efforts + and requested_effort not in (declared_reasoning_efforts or ()) and self.refuses_reasoning_effort_level(model, requested_effort) ): if litellm.drop_params or drop_params: + verbose_logger.warning( + refused_reasoning_effort_warning( + suggest_declared_levels=declared_reasoning_efforts is not None, + suggest_base_model=self.LOOKS_UP_BASE_MODEL + and not has_catalog_entry(self._model_map_lookup_name(model), None), + ), + "reasoning_effort", + requested_effort, + model, + self._model_map_lookup_name(model), + ) non_default_params.pop("reasoning_effort", None) optional_params.pop("reasoning_effort", None) else: diff --git a/litellm/llms/openai/common_utils.py b/litellm/llms/openai/common_utils.py index 216725b9449..a9d2dcdffb8 100644 --- a/litellm/llms/openai/common_utils.py +++ b/litellm/llms/openai/common_utils.py @@ -104,6 +104,19 @@ def is_openai_backed_api_base(api_base: str) -> bool: return hostname is not None and (hostname == _OPENAI_API_HOST or hostname.endswith(f".{_OPENAI_API_HOST}")) +_OPENAI_AGENTS_API_RESOURCES: Final = frozenset({"agents", "vaults"}) + + +def openai_api_resource(path: str) -> str | None: + segments: Final = tuple(segment for segment in path.split("/") if segment) + resource_segments: Final = segments[1:] if segments[:1] == ("v1",) else segments + return next(iter(resource_segments), None) + + +def is_openai_agents_api_path(path: str) -> bool: + return openai_api_resource(path) in _OPENAI_AGENTS_API_RESOURCES + + class OpenAIError(BaseLLMException): def __init__( self, diff --git a/litellm/llms/openai/completion/guardrail_translation/handler.py b/litellm/llms/openai/completion/guardrail_translation/handler.py index c4bc04d4627..27c82cfc004 100644 --- a/litellm/llms/openai/completion/guardrail_translation/handler.py +++ b/litellm/llms/openai/completion/guardrail_translation/handler.py @@ -5,18 +5,30 @@ This module provides guardrail translation support for OpenAI's text completion The handler processes the 'prompt' parameter for guardrails. """ -from collections.abc import Mapping +import json +from collections.abc import Mapping, Sequence +from itertools import chain from typing import TYPE_CHECKING, Final from litellm._logging import verbose_proxy_logger -from litellm.llms.base_llm.guardrail_translation.base_translation import BaseTranslation -from litellm.types.utils import GenericGuardrailAPIInputs +from litellm.llms.base_llm.guardrail_translation.base_translation import ( + BaseTranslation, + StreamingScanKey, + StreamTransformSink, +) +from litellm.llms.base_llm.guardrail_translation.utils import ( + blocked_chat_stream_usage, + stream_item_field, + stream_item_items, +) +from litellm.types.utils import GenericGuardrailAPIInputs, TextChoices, TextCompletionResponse, Usage if TYPE_CHECKING: - from litellm.integrations.custom_guardrail import CustomGuardrail + from fastapi import HTTPException + + from litellm.integrations.custom_guardrail import CustomGuardrail, ModifyResponseException from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj from litellm.types.proxy.auth.user_api_key_auth import UserAPIKeyAuth - from litellm.types.utils import TextCompletionResponse class OpenAITextCompletionHandler(BaseTranslation): @@ -32,6 +44,9 @@ class OpenAITextCompletionHandler(BaseTranslation): - A list of strings (for batch completions) """ + delivers_ended_stream_rewrites = True + assembles_streamed_response = True + async def process_input_messages( self, data: dict, @@ -125,7 +140,7 @@ class OpenAITextCompletionHandler(BaseTranslation): guardrail_to_apply: "CustomGuardrail", litellm_logging_obj: "LiteLLMLoggingObj | None" = None, user_api_key_dict: "UserAPIKeyAuth | None" = None, - request_data: dict | None = None, + request_data: dict[str, object] | None = None, ) -> "TextCompletionResponse": """ Process output response by applying guardrails to completion text. @@ -162,11 +177,7 @@ class OpenAITextCompletionHandler(BaseTranslation): if "response" not in request_data: request_data["response"] = response - # Add user API key metadata with prefixed keys - if "litellm_metadata" not in request_data: - user_metadata: Final = self.transform_user_api_key_dict_to_metadata(user_api_key_dict) - if user_metadata: - request_data["litellm_metadata"] = user_metadata + self.merge_user_api_key_metadata_into_request(request_data, user_api_key_dict) inputs: Final = GenericGuardrailAPIInputs(texts=texts_to_check) # Include model information from the response if available @@ -195,3 +206,168 @@ class OpenAITextCompletionHandler(BaseTranslation): ) return response + + async def process_output_streaming_response( + self, + responses_so_far: list[object], # mutable-ok: rewrites the caller's buffered chunks in place + guardrail_to_apply: "CustomGuardrail", + litellm_logging_obj: "LiteLLMLoggingObj | None" = None, + user_api_key_dict: "UserAPIKeyAuth | None" = None, + request_data: dict[str, object] | None = None, # mutable-ok: the hooks share and mutate the request payload + stream_transform_sink: StreamTransformSink | None = None, + deliver_ended_stream_rewrites: bool = False, + ) -> Sequence[object]: + chunks: Final = _text_completion_chunks(responses_so_far) + if not self._check_streaming_has_ended(chunks): + return responses_so_far + assembled: Final = self._assemble_ended_stream(chunks) + pre_guardrail_texts: Final = _choice_texts(assembled) + await self.process_output_response( + response=assembled, + guardrail_to_apply=guardrail_to_apply, + litellm_logging_obj=litellm_logging_obj, + user_api_key_dict=user_api_key_dict, + request_data=request_data, + ) + if not deliver_ended_stream_rewrites: + return responses_so_far + rewrites: Final = tuple( + after for before, after in zip(pre_guardrail_texts, _choice_texts(assembled)) if after != before + ) + for choice_index, text in rewrites: + self._write_choice_text(chunks, choice_index, text) + return responses_so_far + + def get_streaming_scan_key(self, responses_so_far: Sequence[object]) -> StreamingScanKey | None: + chunks: Final = _text_completion_chunks(responses_so_far) + if not self._check_streaming_has_ended(chunks): + return None + assembled: Final = self._assemble_ended_stream(chunks) + return StreamingScanKey(texts=tuple(text for _, text in _choice_texts(assembled)), stream_ended=True) + + def released_stream_as_ended(self, responses_so_far: Sequence[object]) -> tuple[object, ...]: + chunks: Final = _text_completion_chunks(responses_so_far) + streamed_choices: Final = tuple(chain.from_iterable(chunk.choices for chunk in chunks)) + finished_indices: Final = frozenset( + _choice_index(choice) for choice in streamed_choices if choice.finish_reason + ) + unfinished_indices: Final = tuple( + dict.fromkeys( + _choice_index(choice) for choice in streamed_choices if _choice_index(choice) not in finished_indices + ) + ) + if not unfinished_indices: + return tuple(responses_so_far) + terminator: Final = TextCompletionResponse( + id=chunks[0].id, + model=chunks[0].model, + object="text_completion", + choices=[TextChoices(index=index, text="", finish_reason="length") for index in unfinished_indices], + ) + return (*responses_so_far, terminator) + + def build_block_sse_chunks( + self, + exc: "ModifyResponseException", + stream_started: bool = False, + responses_so_far: Sequence[object] | None = None, + ) -> Sequence[bytes]: + streamed: Final = _text_completion_chunks(responses_so_far or ()) + source: Final = streamed[0] if streamed else TextCompletionResponse(model=exc.model) + message_chunk: Final = TextCompletionResponse( + id=source.id, + created=source.created, + model=source.model or exc.model, + object="text_completion", + choices=[TextChoices(index=0, text=exc.message)], + ).model_copy(update={"usage": None}) + final_chunk: Final = TextCompletionResponse( + id=source.id, + created=source.created, + model=source.model or exc.model, + object="text_completion", + choices=[TextChoices(index=0, text="", finish_reason="content_filter")], + ).model_copy(update={"usage": _blocked_stream_usage(exc.original_response)}) + return tuple( + f"data: {chunk.model_dump_json(exclude_none=True)}\n\n".encode() for chunk in (message_chunk, final_chunk) + ) + + def build_stream_error_items( + self, + exc: "HTTPException", + responses_so_far: Sequence[object] | None = None, + ) -> Sequence[bytes] | None: + from litellm.proxy.common_request_processing import sse_error_payload + + _, error_obj = sse_error_payload(exc) + return (f'data: {{"error": {json.dumps(error_obj)}}}\n\n'.encode(),) + + def _check_streaming_has_ended(self, responses_so_far: Sequence[object]) -> bool: + streamed_choices: Final = chain.from_iterable(stream_item_items(item, "choices") for item in responses_so_far) + return any(stream_item_field(choice, "finish_reason") is not None for choice in streamed_choices) + + @staticmethod + def _assemble_ended_stream(responses_so_far: Sequence[TextCompletionResponse]) -> TextCompletionResponse: + streamed_choices: Final = tuple(chain.from_iterable(item.choices for item in responses_so_far)) + choice_indices: Final = tuple(dict.fromkeys(_choice_index(choice) for choice in streamed_choices)) + assembled_choices: Final = [ + TextChoices( + index=index, + text="".join(_choice_text(choice) for choice in streamed_choices if _choice_index(choice) == index), + finish_reason=next( + ( + choice.finish_reason + for choice in streamed_choices + if _choice_index(choice) == index and choice.finish_reason + ), + None, + ), + ) + for index in choice_indices + ] + first: Final = responses_so_far[0] + return TextCompletionResponse( + id=first.id, + created=first.created, + model=first.model, + object="text_completion", + choices=assembled_choices, + usage=next((item.usage for item in reversed(responses_so_far) if item.usage is not None), None), + ) + + @staticmethod + def _write_choice_text(responses_so_far: Sequence[TextCompletionResponse], choice_index: int, text: str) -> None: + streamed_choices: Final = chain.from_iterable(item.choices for item in responses_so_far) + head, *tail = (choice for choice in streamed_choices if _choice_index(choice) == choice_index) + head.text = text + for choice in tail: + if isinstance(stream_item_field(choice, "text"), str): + choice.text = "" + + +def _blocked_stream_usage(original_response: object) -> Usage | None: + prompt_tokens, completion_tokens = blocked_chat_stream_usage(original_response) + if isinstance(original_response, list) and prompt_tokens == completion_tokens == 0: + return None + return Usage( + prompt_tokens=prompt_tokens, + completion_tokens=completion_tokens, + total_tokens=prompt_tokens + completion_tokens, + ) + + +def _text_completion_chunks(responses_so_far: Sequence[object]) -> tuple[TextCompletionResponse, ...]: + return tuple(item for item in responses_so_far if isinstance(item, TextCompletionResponse)) + + +def _choice_text(choice: TextChoices) -> str: + text: Final = stream_item_field(choice, "text") + return text if isinstance(text, str) else "" + + +def _choice_index(choice: TextChoices) -> int: + return choice.index or 0 + + +def _choice_texts(response: TextCompletionResponse) -> tuple[tuple[int, str], ...]: + return tuple((_choice_index(choice), _choice_text(choice)) for choice in response.choices) diff --git a/litellm/llms/openai/responses/transformation.py b/litellm/llms/openai/responses/transformation.py index 903e6177e7b..9b8515a9ecd 100644 --- a/litellm/llms/openai/responses/transformation.py +++ b/litellm/llms/openai/responses/transformation.py @@ -32,6 +32,7 @@ from litellm.litellm_core_utils.url_utils import encode_url_path_segment, join_a from litellm.llms.base_llm.responses.transformation import BaseResponsesAPIConfig from litellm.llms.openai.chat.gpt_reasoning_transformation import ( is_gpt_reasoning_series_name, + refused_reasoning_effort_warning, unsupported_reasoning_effort_message, ) from litellm.responses.litellm_completion_transformation.custom_tools import TOOL_CALL_ITEM_ID_PREFIX_BY_TYPE @@ -55,6 +56,9 @@ from ..common_utils import OpenAIError from ..workload_identity import get_workload_identity_bearer_token, resolve_openai_workload_identity_config OPENAI_RESPONSES_API_MIN_MAX_OUTPUT_TOKENS: Final = 16 +DROP_UNSUPPORTED_REASONING_WARNING: Final = ( + "Dropping reasoning=%r for model=%r (drop_params=True) because its model cost map entry lacks supports_reasoning." +) _RAW_RESPONSE_JSON: Final = TypeAdapter(dict[str, object], config=ConfigDict(strict=True)) if TYPE_CHECKING: @@ -417,12 +421,19 @@ class OpenAIResponsesAPIConfig(BaseResponsesAPIConfig): return declared return info["key"] in _bundled_openai_reasoning_models() + @staticmethod + def _is_known_non_reasoning_model(model: str) -> bool: + from litellm.utils import is_known_non_reasoning_model + + return is_known_non_reasoning_model(model.split("/")[-1], LlmProviders.OPENAI.value) + @staticmethod def _requests_reasoning_effort(reasoning: object) -> bool: - effort: Final = ( - reasoning.get("effort") if isinstance(reasoning, Mapping) else getattr(reasoning, "effort", None) - ) - return effort is not None + return OpenAIResponsesAPIConfig._requested_effort(reasoning) is not None + + @staticmethod + def _requested_effort(reasoning: object) -> object: + return reasoning.get("effort") if isinstance(reasoning, Mapping) else getattr(reasoning, "effort", None) @staticmethod def _enforce_min_max_output_tokens(max_output_tokens: "int | None") -> "int | None": @@ -464,17 +475,20 @@ class OpenAIResponsesAPIConfig(BaseResponsesAPIConfig): ) -> dict: return dict( self.map_openai_params_for_deployment( - response_api_optional_params, model, drop_params, declared_reasoning_efforts=() + response_api_optional_params, model, drop_params, declared_reasoning_efforts=None ) ) + def honors_declared_reasoning_efforts(self) -> bool: + return type(self).map_openai_params is OpenAIResponsesAPIConfig.map_openai_params + def map_openai_params_for_deployment( self, response_api_optional_params: ResponsesAPIOptionalRequestParams, model: str, drop_params: bool, *, - declared_reasoning_efforts: Collection[str], + declared_reasoning_efforts: Collection[str] | None, ) -> Mapping[str, object]: """No mapping applied since inputs are in OpenAI spec already. @@ -494,7 +508,9 @@ class OpenAIResponsesAPIConfig(BaseResponsesAPIConfig): and not self._supports_reasoning_param(model=model) ): if drop_params or litellm.drop_params: - params.pop("reasoning", None) + dropped_reasoning: Final = params.pop("reasoning", None) + if self._requested_effort(dropped_reasoning) != "none" or not self._is_known_non_reasoning_model(model): + verbose_logger.warning(DROP_UNSUPPORTED_REASONING_WARNING, dropped_reasoning, model) else: raise litellm.UnsupportedParamsError( message=( @@ -512,7 +528,7 @@ class OpenAIResponsesAPIConfig(BaseResponsesAPIConfig): if ( isinstance(reasoning, dict) and isinstance(requested_effort, str) - and requested_effort not in declared_reasoning_efforts + and requested_effort not in (declared_reasoning_efforts or ()) and self._effort_level_is_disabled(lookup_name, requested_effort) ): if not (drop_params or litellm.drop_params): @@ -520,6 +536,15 @@ class OpenAIResponsesAPIConfig(BaseResponsesAPIConfig): message=unsupported_reasoning_effort_message("reasoning.effort", requested_effort, model), status_code=400, ) + verbose_logger.warning( + refused_reasoning_effort_warning( + suggest_declared_levels=declared_reasoning_efforts is not None, suggest_base_model=False + ), + "reasoning.effort", + requested_effort, + model, + lookup_name, + ) remaining: Final = reasoning.copy() remaining.pop("effort", None) if remaining: diff --git a/litellm/llms/openai_like/providers.json b/litellm/llms/openai_like/providers.json index fe88ac03e04..ad47b334ed4 100644 --- a/litellm/llms/openai_like/providers.json +++ b/litellm/llms/openai_like/providers.json @@ -270,6 +270,13 @@ "/v1/responses" ] }, + "kyma": { + "base_url": "https://kymaapi.com/v1", + "api_key_env": "KYMA_API_KEY", + "provider_info": { + "supports_function_calling": true + } + }, "streamlake": { "base_url": "https://vanchin.streamlake.ai/api/gateway/v1/endpoints", "api_key_env": "STREAMLAKE_API_KEY", diff --git a/litellm/llms/opensearch/__init__.py b/litellm/llms/opensearch/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/litellm/llms/opensearch/vector_stores/__init__.py b/litellm/llms/opensearch/vector_stores/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/litellm/llms/opensearch/vector_stores/transformation.py b/litellm/llms/opensearch/vector_stores/transformation.py new file mode 100644 index 00000000000..2d862bc8ea6 --- /dev/null +++ b/litellm/llms/opensearch/vector_stores/transformation.py @@ -0,0 +1,415 @@ +import hashlib +import json +import re +from collections.abc import Mapping, Sequence +from functools import partial +from typing import TYPE_CHECKING, Final, Literal, NoReturn +from urllib.parse import quote, urlsplit + +import httpx +from pydantic import ConfigDict, Field, TypeAdapter, ValidationError + +from litellm._logging import verbose_logger +from litellm.exceptions import AuthenticationError, BadRequestError +from litellm.litellm_core_utils.core_helpers import drops_unsupported_params +from litellm.litellm_core_utils.optional_imports import ensure_optional_import +from litellm.llms.base_llm.chat.transformation import BaseLLMException +from litellm.llms.base_llm.vector_store.transformation import ( + BaseQueryEmbeddingVectorStoreConfig, + VectorStoreEmbeddingExecutor, +) +from litellm.llms.bedrock.base_aws_llm import BaseAWSLLM, SignsRequestsWithAWS, sign_aws_json_post +from litellm.types.llms.base import LiteLLMBaseModel +from litellm.types.llms.bedrock import AwsAuthParams +from litellm.types.router import GenericLiteLLMParams +from litellm.types.utils import EmbeddingResponse +from litellm.types.vector_stores import ( + BaseVectorStoreAuthCredentials, + VectorStoreCreateOptionalRequestParams, + VectorStoreIndexEndpoints, + VectorStoreResultContent, + VectorStoreSearchOptionalRequestParams, + VectorStoreSearchResponse, + VectorStoreSearchResult, +) + +if TYPE_CHECKING: + from botocore.credentials import Credentials + + from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj + +DEFAULT_EMBEDDING_FIELD: Final = "embedding" +DEFAULT_TEXT_FIELD: Final = "text" +DEFAULT_MAX_NUM_RESULTS: Final = 10 +MAX_MAX_NUM_RESULTS: Final = 50 +_PARAM_PREFIX: Final = "opensearch_" +_AWS_HOSTS: Final = ( + re.compile(r"\.(?P[a-z0-9-]+)\.(?Paoss|aoss-fips|es)\.amazonaws\.com(?:\.cn)?\Z"), + re.compile(r"\.(?Paoss|aos)\.(?P[a-z0-9-]+)\.on\.aws\Z"), +) +_ACCOUNT_ID_LABEL: Final = re.compile(r"\A\d{12}\.") +_QUERY_VECTOR: Final = TypeAdapter(tuple[float, ...]) +_ATTRIBUTE_VALUE_TYPES: Final = (str, int, float, bool) +_SEARCH_ONLY_MESSAGE: Final = ( + "The OpenSearch vector store is search-only. Create the k-NN index in OpenSearch, " + "then register it here with its name as opensearch_index_name or vector_store_id." +) +_PER_ACCOUNT_MESSAGE: Final = ( + "api_base is an OpenSearch Serverless per-account endpoint, which needs a collection header the vector store " + "does not send. Use the collection endpoint instead, for example https://..aoss.amazonaws.com." +) +_SIGV4_ONLY_MESSAGE: Final = ( + "The OpenSearch vector store signs every request with AWS SigV4 and does not support api_key. " + "Remove api_key and give the vector store AWS credentials instead." +) +_CREDENTIALS_HINT: Final = ( + "Set aws_access_key_id and aws_secret_access_key, aws_role_name, or aws_profile_name on the vector store, " + "or run LiteLLM with an AWS identity." +) + + +def _bad_request(message: str) -> BadRequestError: + return BadRequestError(message=message, model=None, llm_provider="opensearch") + + +def _unauthenticated(reason: str) -> AuthenticationError: + return AuthenticationError( + message=f"No usable AWS credentials to sign the OpenSearch request ({reason}). {_CREDENTIALS_HINT}", + llm_provider="opensearch", + model=None, + ) + + +def _usable_credentials(credentials: object) -> "Credentials": + from botocore.credentials import Credentials + + if isinstance(credentials, Credentials): + return credentials + raise _unauthenticated("none were found") + + +class _OpenSearchParams(LiteLLMBaseModel): + model_config = ConfigDict(frozen=True, extra="ignore") + + litellm_embedding_model: str | None = None + opensearch_embedding_field: str | None = None + opensearch_index_name: str | None = None + opensearch_text_field: str | None = None + + @property + def embedding_field(self) -> str: + return self.opensearch_embedding_field or DEFAULT_EMBEDDING_FIELD + + @property + def text_field(self) -> str: + return self.opensearch_text_field or DEFAULT_TEXT_FIELD + + def index_name(self, vector_store_id: str) -> str: + return self.opensearch_index_name or vector_store_id + + def require_embedding_model(self) -> str: + if not self.litellm_embedding_model: + raise _bad_request( + "litellm_embedding_model is required for the OpenSearch vector store. It must be the model that " + f"produced the vectors in '{self.embedding_field}', for example openai/text-embedding-3-small." + ) + return self.litellm_embedding_model + + +class _SigningRegion(LiteLLMBaseModel): + model_config = ConfigDict(frozen=True, extra="ignore") + + aws_region_name: str | None = None + + +class _SearchOptions(LiteLLMBaseModel): + model_config = ConfigDict(frozen=True, extra="ignore") + + max_num_results: int | None = None + filters: Mapping[str, object] | None = None + ranking_options: Mapping[str, object] | None = None + rewrite_query: bool | None = None + + @property + def unsupported(self) -> tuple[str, ...]: + requested: Final = ( + ("filters", self.filters), + ("ranking_options", self.ranking_options), + ("rewrite_query", self.rewrite_query), + ) + return tuple(name for name, value in requested if value) + + @property + def k(self) -> int: + if self.max_num_results is None: + return DEFAULT_MAX_NUM_RESULTS + if not 1 <= self.max_num_results <= MAX_MAX_NUM_RESULTS: + raise _bad_request( + f"max_num_results must be between 1 and {MAX_MAX_NUM_RESULTS}, got {self.max_num_results}" + ) + return self.max_num_results + + +_KNOWN_PARAMS: Final = frozenset(name for name in _OpenSearchParams.model_fields if name.startswith(_PARAM_PREFIX)) + + +class _LoggedSearch(LiteLLMBaseModel): + model_config = ConfigDict(frozen=True, extra="ignore") + + query: str | tuple[str, ...] = "" + litellm_params: _OpenSearchParams = _OpenSearchParams() + + @property + def search_query(self) -> str: + return BaseQueryEmbeddingVectorStoreConfig.query_text(self.query) + + +class _Hit(LiteLLMBaseModel): + model_config = ConfigDict(frozen=True) + + id: str = Field(alias="_id") + score: float | None = Field(default=None, alias="_score") + source: Mapping[str, object] = Field(default_factory=dict, alias="_source") + + +class _Hits(LiteLLMBaseModel): + model_config = ConfigDict(frozen=True) + + hits: Sequence[_Hit] + + +class _SearchResponse(LiteLLMBaseModel): + model_config = ConfigDict(frozen=True) + + hits: _Hits + + +def aws_signing_target(url: str) -> tuple[Literal["aoss", "es"], str | None]: + hostname: Final = urlsplit(url).hostname or "" + match: Final = next(filter(None, (pattern.search(hostname) for pattern in _AWS_HOSTS)), None) + if match is None: + return "es", None + return ("aoss" if match["service"].startswith("aoss") else "es"), match["region"] + + +def _search_result(hit: _Hit, params: _OpenSearchParams) -> VectorStoreSearchResult: + text: Final = hit.source.get(params.text_field) + filename: Final = hit.source.get("filename") + return VectorStoreSearchResult( + score=hit.score, + content=[VectorStoreResultContent(type="text", text=text)] if isinstance(text, str) else [], + file_id=hit.id, + filename=filename if isinstance(filename, str) else None, + attributes={ + key: value + for key, value in hit.source.items() + if key not in (params.text_field, params.embedding_field) and isinstance(value, _ATTRIBUTE_VALUE_TYPES) + }, + ) + + +class OpenSearchVectorStoreConfig(BaseQueryEmbeddingVectorStoreConfig, SignsRequestsWithAWS): + def __init__(self, aws: BaseAWSLLM | None = None) -> None: + self._aws: Final = aws if aws is not None else BaseAWSLLM() + + def get_auth_credentials(self, litellm_params: Mapping[str, object]) -> BaseVectorStoreAuthCredentials: + return BaseVectorStoreAuthCredentials() + + def get_vector_store_endpoints_by_type(self) -> VectorStoreIndexEndpoints: + return VectorStoreIndexEndpoints(read=[], write=[]) + + def validate_environment( + self, headers: Mapping[str, object], litellm_params: GenericLiteLLMParams | None + ) -> dict[str, object]: # mutable-ok: the shared HTTP handler requires writable headers + params: Final = dict(litellm_params or {}) + self._signing_credentials(params, self._signing_region(params, str(params.get("api_base") or ""))) + return {**headers, "Content-Type": "application/json"} + + def _signing_region(self, params: Mapping[str, object], api_base: str) -> str: + host_region: Final = aws_signing_target(api_base)[1] + return self._aws.get_aws_region_name_for_non_llm_api_calls( + host_region or _SigningRegion.model_validate(params).aws_region_name + ) + + def _signing_credentials(self, params: Mapping[str, object], region: str) -> "Credentials": + if params.get("api_key"): + raise _bad_request(_SIGV4_ONLY_MESSAGE) + auth: Final = AwsAuthParams.model_validate(params) + if bool(auth.aws_access_key_id) != bool(auth.aws_secret_access_key): + raise _unauthenticated("aws_access_key_id and aws_secret_access_key must be set together") + ensure_optional_import("botocore") + from botocore.exceptions import ( + CredentialRetrievalError, + NoCredentialsError, + PartialCredentialsError, + ProfileNotFound, + ) + + try: + return _usable_credentials(self._aws.resolve_credentials(auth, region)) + except CredentialRetrievalError as error: + verbose_logger.warning("OpenSearch vector store AWS credential source failed: %s", error) + raise _unauthenticated("the AWS credential source failed, see the LiteLLM logs") from None + except (NoCredentialsError, PartialCredentialsError, ProfileNotFound) as error: + raise _unauthenticated(str(error)) from None + + def get_complete_url(self, api_base: str | None, litellm_params: Mapping[str, object]) -> str: + parsed: Final = urlsplit(api_base or "") + if parsed.scheme not in ("http", "https") or not parsed.hostname: + raise _bad_request( + "api_base must be the OpenSearch endpoint URL, for example " + "https://..aoss.amazonaws.com or https://." + ) + if _ACCOUNT_ID_LABEL.match(parsed.hostname) and aws_signing_target(api_base or "")[0] == "aoss": + raise _bad_request(_PER_ACCOUNT_MESSAGE) + return (api_base or "").rstrip("/") + + def get_vector_store_url(self, api_base: str, encoded_vector_store_id: str) -> NoReturn: + raise _bad_request(_SEARCH_ONLY_MESSAGE) + + def get_vector_store_list_url(self, api_base: str) -> NoReturn: + raise _bad_request(_SEARCH_ONLY_MESSAGE) + + @staticmethod + def _params( + litellm_params: Mapping[str, object], + optional_params: VectorStoreSearchOptionalRequestParams, + extra_body: Mapping[str, object] | None, + ) -> tuple[_OpenSearchParams, int]: + unknown: Final = sorted( + key for key in litellm_params if key.startswith(_PARAM_PREFIX) and key not in _KNOWN_PARAMS + ) + if unknown: + raise _bad_request( + f"Unknown OpenSearch vector store parameter(s): {', '.join(unknown)}. " + f"Supported: {', '.join(sorted(_KNOWN_PARAMS))}." + ) + if extra_body: + raise _bad_request("The OpenSearch vector store does not support extra_body.") + try: + options: Final = _SearchOptions.model_validate(optional_params) + except ValidationError as error: + raise _bad_request(f"Invalid vector store search parameters: {error}") from None + if options.unsupported and not drops_unsupported_params(litellm_params.get("drop_params")): + raise _bad_request( + f"The OpenSearch vector store does not support {', '.join(options.unsupported)} yet. " + "Remove it from the request, or set drop_params to drop it." + ) + try: + return _OpenSearchParams.model_validate(litellm_params), options.k + except ValidationError: + raise _bad_request( + "opensearch_embedding_field, opensearch_index_name, opensearch_text_field, and litellm_embedding_model " + "must be strings." + ) from None + + @staticmethod + def _search_request( + vector_store_id: str, + embedding: EmbeddingResponse, + k: int, + params: _OpenSearchParams, + api_base: str, + ) -> tuple[str, dict[str, object]]: # mutable-ok: the provider contract returns a writable JSON request body + vector: Final = _QUERY_VECTOR.validate_python(embedding.data[0]["embedding"]) # pyright: ignore[reportUnknownMemberType] # EmbeddingResponse.data is an untyped list, the vector is validated here + return ( + f"{api_base}/{quote(params.index_name(vector_store_id), safe='')}/_search", + { + "size": k, + "query": {"knn": {params.embedding_field: {"vector": vector, "k": k}}}, + "_source": {"excludes": [params.embedding_field]}, + }, + ) + + def transform_search_vector_store_request( + self, + vector_store_id: str, + query: str | Sequence[str], + vector_store_search_optional_params: VectorStoreSearchOptionalRequestParams, + api_base: str, + litellm_logging_obj: "LiteLLMLoggingObj", + litellm_params: Mapping[str, object], + extra_body: Mapping[str, object] | None = None, + embedding_executor: VectorStoreEmbeddingExecutor | None = None, + ) -> tuple[str, dict[str, object]]: # mutable-ok: the provider contract returns a writable JSON request body + params, k = self._params(litellm_params, vector_store_search_optional_params, extra_body) + embedding: Final = self.query_embedding_executor(embedding_executor, None).embed( + params.require_embedding_model(), self.query_text(query), self.query_embedding_configuration(litellm_params) + ) + return self._search_request(vector_store_id, embedding, k, params, api_base) + + async def atransform_search_vector_store_request( + self, + vector_store_id: str, + query: str | Sequence[str], + vector_store_search_optional_params: VectorStoreSearchOptionalRequestParams, + api_base: str, + litellm_logging_obj: "LiteLLMLoggingObj", + litellm_params: Mapping[str, object], + extra_body: Mapping[str, object] | None = None, + embedding_executor: VectorStoreEmbeddingExecutor | None = None, + ) -> tuple[str, dict[str, object]]: # mutable-ok: the provider contract returns a writable JSON request body + params, k = self._params(litellm_params, vector_store_search_optional_params, extra_body) + embedding: Final = await self.query_embedding_executor(embedding_executor, None).aembed( + params.require_embedding_model(), self.query_text(query), self.query_embedding_configuration(litellm_params) + ) + return self._search_request(vector_store_id, embedding, k, params, api_base) + + def sign_request( + self, + headers: Mapping[str, str], + optional_params: Mapping[str, object], + request_data: Mapping[str, object], + api_base: str, + api_key: str | None = None, + ) -> tuple[dict[str, str], bytes]: # mutable-ok: the shared HTTP handler requires writable headers + region: Final = self._signing_region(optional_params, api_base) + body: Final = json.dumps(request_data) + signed: Final = sign_aws_json_post( + get_credentials=partial(self._signing_credentials, optional_params, region), + service_name=aws_signing_target(api_base)[0], + aws_region_name=region, + url=api_base, + body=body, + headers={ + "Content-Type": "application/json", + "x-amz-content-sha256": hashlib.sha256(body.encode()).hexdigest(), + }, + ) + signed_headers: Final = dict(signed.headers.items()) + signed_names: Final = frozenset(name.lower() for name in signed_headers) + return { + **{name: value for name, value in headers.items() if name.lower() not in signed_names}, + **signed_headers, + }, body.encode() + + def transform_search_vector_store_response( + self, response: httpx.Response, litellm_logging_obj: "LiteLLMLoggingObj" + ) -> VectorStoreSearchResponse: + if not response.is_success: + raise BaseLLMException(status_code=response.status_code, message=response.text, headers=response.headers) + try: + hits: Final = _SearchResponse.model_validate_json(response.content).hits.hits + except ValidationError: + raise BaseLLMException( + status_code=502, + message="OpenSearch answered the search without a hits.hits list.", + headers=response.headers, + ) from None + logged: Final = _LoggedSearch.model_validate(litellm_logging_obj.model_call_details) + return VectorStoreSearchResponse( + object="vector_store.search_results.page", + search_query=logged.search_query, + data=[_search_result(hit, logged.litellm_params) for hit in hits], + ) + + def validate_create_vector_store(self) -> NoReturn: + raise _bad_request(_SEARCH_ONLY_MESSAGE) + + def transform_create_vector_store_request( + self, vector_store_create_optional_params: VectorStoreCreateOptionalRequestParams, api_base: str + ) -> NoReturn: + raise _bad_request(_SEARCH_ONLY_MESSAGE) + + def transform_create_vector_store_response(self, response: httpx.Response) -> NoReturn: + raise _bad_request(_SEARCH_ONLY_MESSAGE) diff --git a/litellm/llms/sap/chat/transformation.py b/litellm/llms/sap/chat/transformation.py index 622c28f3c90..0270f814328 100755 --- a/litellm/llms/sap/chat/transformation.py +++ b/litellm/llms/sap/chat/transformation.py @@ -11,6 +11,7 @@ import httpx from pydantic import BaseModel import litellm +from litellm.litellm_core_utils.prompt_templates.common_utils import message_with_file_parts_as_image_urls from litellm.litellm_core_utils.url_utils import join_api_base from litellm.types.llms.openai import AllMessageValues from litellm.types.utils import ModelResponse @@ -104,7 +105,7 @@ def _message_role_mapping(role: str) -> type[ChatMessage]: def _messages_to_sap_template(messages: list[AllMessageValues]) -> list: template: Final = [] for message in messages: - folded_message = _fold_message_cache_control(message) + folded_message = _fold_message_cache_control(message_with_file_parts_as_image_urls(message)) message_role_class = _message_role_mapping(str(folded_message["role"])) validated_message = validate_dict(folded_message, message_role_class) template.append(validated_message) diff --git a/litellm/llms/snowflake/chat/transformation.py b/litellm/llms/snowflake/chat/transformation.py index 030413030eb..928dcc3bc52 100644 --- a/litellm/llms/snowflake/chat/transformation.py +++ b/litellm/llms/snowflake/chat/transformation.py @@ -112,7 +112,7 @@ def _convert_image_url_to_anthropic(block: Mapping[str, object]) -> object: pdf/text data URIs become document blocks rather than malformed image blocks. """ image_url: Final = block.get("image_url") - url: Final = image_url if isinstance(image_url, str) else _image_url_field(image_url, "url") + url: Final = image_url if isinstance(image_url, str) else _str_field(image_url, "url") if not url: return block @@ -121,18 +121,31 @@ def _convert_image_url_to_anthropic(block: Mapping[str, object]) -> object: if select_anthropic_content_block_type_for_file(_data_uri_media_type(url)) == "document" else create_anthropic_image_param( image_url if isinstance(image_url, dict) else url, - format=_image_url_field(image_url, "format"), + format=_str_field(image_url, "format"), is_bedrock_invoke=True, ) ) + return _with_cache_control(converted, block) + + +def _convert_file_to_anthropic(block: Mapping[str, object]) -> object: + file_data: Final = _str_field(block.get("file"), "file_data") + if not file_data: + return block + return _with_cache_control( + anthropic_process_openai_file_message({"type": "file", "file": {"file_data": file_data}}), block + ) + + +def _with_cache_control(converted: Mapping[str, object], block: Mapping[str, object]) -> Mapping[str, object]: cache_control: Final = block.get("cache_control") if cache_control is None: return converted return {**converted, "cache_control": cache_control} -def _image_url_field(image_url: object, key: str) -> str | None: - value: Final = image_url.get(key) if isinstance(image_url, dict) else None +def _str_field(container: object, key: str) -> str | None: + value: Final = container.get(key) if isinstance(container, dict) else None return value if isinstance(value, str) else None @@ -141,15 +154,23 @@ def _data_uri_media_type(url: str) -> str: return match.group(1) if match else "" -def _convert_image_url_blocks_to_anthropic(content: object) -> object: +def _convert_media_block_to_anthropic(block: object) -> object: + mapping: Final = as_str_mapping(block) + if mapping is None: + return block + match mapping.get("type"): + case "image_url": + return _convert_image_url_to_anthropic(mapping) + case "file": + return _convert_file_to_anthropic(mapping) + case _: + return block + + +def _convert_media_blocks_to_anthropic(content: object) -> object: if not isinstance(content, list): return content - return [ - _convert_image_url_to_anthropic(block) - if isinstance(block, Mapping) and block.get("type") == "image_url" - else block - for block in content - ] + return [_convert_media_block_to_anthropic(block) for block in content] def _convert_tool_result_to_anthropic( @@ -391,7 +412,7 @@ class SnowflakeConfig(SnowflakeBaseConfig, OpenAIGPTConfig): conversation.append( { "role": role, - "content": _convert_image_url_blocks_to_anthropic(content), + "content": _convert_media_blocks_to_anthropic(content), } ) diff --git a/litellm/llms/soniox/common_utils.py b/litellm/llms/soniox/common_utils.py index 8d83b4c9218..796f6ca12e4 100644 --- a/litellm/llms/soniox/common_utils.py +++ b/litellm/llms/soniox/common_utils.py @@ -3,7 +3,7 @@ Shared utilities for the Soniox provider (https://soniox.com). """ from collections.abc import Mapping, Sequence -from typing import Final, TypeAlias +from typing import Final, Literal, TypeAlias from litellm.litellm_core_utils.audio_utils.subtitle_utils import ( SubtitleToken, @@ -17,6 +17,8 @@ SonioxToken: TypeAlias = Mapping[str, object] # Soniox API base URL. SONIOX_API_BASE: Final[str] = "https://api.soniox.com" +SONIOX_REALTIME_MODEL_PREFIX: Final = "stt-rt-" + # Default polling interval in seconds when waiting for an async transcription # to finish. Mirrors the Soniox SDK default. SONIOX_DEFAULT_POLL_INTERVAL: Final[float] = 1.0 @@ -54,6 +56,10 @@ class SonioxException(BaseLLMException): """Provider-specific exception class for Soniox.""" +def soniox_health_check_mode(model: str) -> Literal["realtime"] | None: + return "realtime" if model.startswith(SONIOX_REALTIME_MODEL_PREFIX) else None + + def get_soniox_api_key(api_key: str | None = None) -> str | None: """Resolve the Soniox API key from arg or env var.""" # Local import to avoid a circular import: litellm.secret_managers.main diff --git a/litellm/llms/soniox/realtime/__init__.py b/litellm/llms/soniox/realtime/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/litellm/llms/soniox/realtime/transformation.py b/litellm/llms/soniox/realtime/transformation.py new file mode 100644 index 00000000000..ed7590b6f39 --- /dev/null +++ b/litellm/llms/soniox/realtime/transformation.py @@ -0,0 +1,609 @@ +import asyncio +import time +from collections import deque +from collections.abc import Awaitable, Callable, Mapping +from itertools import chain, groupby +from types import MappingProxyType, TracebackType +from typing import Final +from urllib.parse import urlparse, urlunparse + +from pydantic import BaseModel, ConfigDict, JsonValue, ValidationError +from typing_extensions import NotRequired, ReadOnly, Required, Self, TypedDict + +from litellm import verbose_logger +from litellm._uuid import uuid +from litellm.constants import REALTIME_WEBSOCKET_MAX_MESSAGE_SIZE_BYTES +from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj +from litellm.llms.base_llm.realtime.transcription_protocol import ( + SESSION_UPDATE_EVENT_TYPES, + RealtimeTranscriptionProtocolError, + TranscriptionSessionUpdate, + completed_event, + decode_pcm16_append, + delta_event, + duration_usage, + error_event, + json_object, + parse_transcription_session_update, + speech_event, + transcription_session, + transcription_session_created_event, + uses_server_vad, +) +from litellm.llms.base_llm.realtime.transformation import BaseRealtimeConfig, RealtimeBackend +from litellm.llms.custom_httpx.http_handler import realtime_ssl_for_url +from litellm.llms.soniox.common_utils import SonioxException, get_soniox_api_key +from litellm.types.llms.openai import ( + OpenAIRealtimeErrorEvent, + OpenAIRealtimeEvents, + OpenAIRealtimeInputAudioTranscriptionCompleted, + OpenAIRealtimeInputAudioTranscriptionDelta, + OpenAIRealtimeTranscriptionSessionCreated, +) +from litellm.types.realtime import ( + RealtimeInputAudioTranscriptionUsage, + RealtimeResponseTransformInput, + RealtimeResponseTypedDict, +) + +DEFAULT_SONIOX_REALTIME_URL: Final = "wss://stt-rt.soniox.com/transcribe-websocket" +WEBSOCKET_SCHEMES: Final = MappingProxyType({"wss": "wss", "ws": "ws", "https": "wss", "http": "ws"}) +DEFAULT_SAMPLE_RATE: Final = 24_000 +KEEPALIVE_INTERVAL_SECONDS: Final = 5.0 +_MAX_PENDING_FRAMES: Final = 64 +_FINALIZE: Final = '{"type":"finalize"}' +_KEEPALIVE: Final = '{"type":"keepalive"}' +_END_STREAM: Final = "" +_HEALTH_CHECK_SILENCE: Final = bytes(DEFAULT_SAMPLE_RATE // 5) +_CONTROL_FRAMES: Final = frozenset((_FINALIZE, _KEEPALIVE, _END_STREAM)) +SESSION_STARTED_FRAME: Final = '{"type":"litellm.soniox.session_started"}' +_ENDPOINT_TOKEN: Final = "" +_FINALIZED_TOKEN: Final = "" + + +class SonioxProtocolError(RealtimeTranscriptionProtocolError): + pass + + +class SonioxRealtimeOptions(BaseModel): + model_config = ConfigDict(frozen=True, extra="ignore") + + language_hints: tuple[str, ...] | None = None + language_hints_strict: bool | None = None + enable_language_identification: bool | None = None + enable_speaker_diarization: bool | None = None + context: JsonValue | None = None + translation: JsonValue | None = None + client_reference_id: str | None = None + max_endpoint_delay_ms: int | None = None + endpoint_sensitivity: float | None = None + endpoint_latency_adjustment_level: int | None = None + + +class SonioxStartRequest(SonioxRealtimeOptions): + model: str + audio_format: str + sample_rate: int + num_channels: int + enable_endpoint_detection: bool + + +class SonioxSpeechLabels(TypedDict, total=False): + speaker: ReadOnly[str] + language: ReadOnly[str] + + +class SonioxTranscriptSegment(SonioxSpeechLabels): + text: Required[ReadOnly[str]] + + +class SonioxTranscriptionDelta(OpenAIRealtimeInputAudioTranscriptionDelta, SonioxSpeechLabels): + pass + + +class SonioxTranscriptionCompleted(OpenAIRealtimeInputAudioTranscriptionCompleted): + segments: NotRequired[ReadOnly[tuple[SonioxTranscriptSegment, ...]]] + + +class _SonioxToken(BaseModel): + model_config = ConfigDict(frozen=True) + + text: str + is_final: bool = False + translation_status: str | None = None + speaker: str | None = None + language: str | None = None + + @property + def labelled(self) -> bool: + return self.speaker is not None or self.language is not None + + +class _SonioxResponse(BaseModel): + model_config = ConfigDict(frozen=True) + + tokens: tuple[_SonioxToken, ...] = () + total_audio_proc_ms: int | None = None + error_code: int | None = None + error_message: str | None = None + finished: bool = False + + +def _sample_rate(update: TranscriptionSessionUpdate | None) -> int: + audio_format: Final = None if update is None else update.audio_format + if audio_format is None: + return DEFAULT_SAMPLE_RATE + if not audio_format.is_pcm16: + raise SonioxProtocolError("Soniox realtime requires pcm16 input audio") + if audio_format.channels not in (None, 1): + raise SonioxProtocolError("Soniox realtime requires mono input audio") + if audio_format.rate is None: + return DEFAULT_SAMPLE_RATE + if audio_format.rate <= 0: + raise SonioxProtocolError("sample rate must be positive") + return audio_format.rate + + +def _language_hints( + language: str | None, configured: tuple[str, ...] | None, strict: bool | None +) -> tuple[str, ...] | None: + if not language or (strict and configured): + return configured + return (language, *(hint for hint in configured or () if hint != language)) + + +def _uses_endpoint_detection(update: TranscriptionSessionUpdate, drop_params: bool) -> bool: + return uses_server_vad(update, "Soniox realtime", drop_params, SonioxProtocolError) + + +def _ignored_settings(update: TranscriptionSessionUpdate) -> tuple[str, ...]: + vad_tuning: Final = (f"turn_detection.{key}" for key in sorted(update.turn_detection or {}) if key != "type") + return (*update.unsupported_transcription_keys, *vad_tuning) + + +def build_start_request( + model: str, update: TranscriptionSessionUpdate | None, options: SonioxRealtimeOptions, drop_params: bool = False +) -> SonioxStartRequest: + return SonioxStartRequest( + **options.model_dump(exclude={"language_hints"}), + language_hints=_language_hints( + None if update is None else update.language, options.language_hints, options.language_hints_strict + ), + model=model, + audio_format="pcm_s16le", + sample_rate=_sample_rate(update), + num_channels=1, + enable_endpoint_detection=update is None or _uses_endpoint_detection(update, drop_params), + ) + + +def _session_created(session_id: str, start_request: SonioxStartRequest) -> OpenAIRealtimeTranscriptionSessionCreated: + session: Final = transcription_session( + session_id=session_id, + model=start_request.model, + sample_rate=start_request.sample_rate, + language=start_request.language_hints[0] if start_request.language_hints else None, + server_vad=start_request.enable_endpoint_detection, + ) + return transcription_session_created_event(session) + + +def _changes_started_rate(update: TranscriptionSessionUpdate, started: SonioxStartRequest) -> bool: + audio_format: Final = update.audio_format + if audio_format is None: + return False + requested_rate: Final = _sample_rate(update) + return audio_format.rate is not None and requested_rate != started.sample_rate + + +def _changes_started_stream(update: TranscriptionSessionUpdate, started: SonioxStartRequest, drop_params: bool) -> bool: + turn_detection_set: Final = update.turn_detection is not None or update.turn_detection_disabled + return ( + _changes_started_rate(update, started) + or ( + update.language is not None + and _language_hints(update.language, started.language_hints, started.language_hints_strict) + != started.language_hints + ) + or (turn_detection_set and _uses_endpoint_detection(update, drop_params) != started.enable_endpoint_detection) + ) + + +def build_soniox_realtime_url(api_base: str | None) -> str: + if api_base is None: + return DEFAULT_SONIOX_REALTIME_URL + parsed: Final = urlparse(api_base) + scheme: Final = WEBSOCKET_SCHEMES.get(parsed.scheme) + if scheme is None: + raise ValueError("Soniox realtime api_base must be a wss://, ws://, https://, or http:// URL") + return urlunparse(parsed._replace(scheme=scheme)) + + +def _labels(token: _SonioxToken) -> SonioxSpeechLabels: + speaker: Final[SonioxSpeechLabels] = {} if token.speaker is None else {"speaker": token.speaker} + language: Final[SonioxSpeechLabels] = {} if token.language is None else {"language": token.language} + return {**speaker, **language} + + +def _delta(item_id: str, token: _SonioxToken) -> SonioxTranscriptionDelta: + return {**delta_event(item_id, token.text), **_labels(token)} + + +def _segment(run: tuple[_SonioxToken, ...]) -> SonioxTranscriptSegment: + return {**_labels(run[0]), "text": "".join(token.text for token in run).strip()} + + +def _segments(tokens: tuple[_SonioxToken, ...]) -> tuple[SonioxTranscriptSegment, ...]: + return tuple(_segment(tuple(run)) for _, run in groupby(tokens, key=lambda token: (token.speaker, token.language))) + + +def _new_item_id() -> str: + return f"item_{uuid.uuid4().hex}" + + +def _completed( + item_id: str, tokens: tuple[_SonioxToken, ...], usage: RealtimeInputAudioTranscriptionUsage | None +) -> OpenAIRealtimeInputAudioTranscriptionCompleted: + completed: Final = completed_event(item_id, "".join(token.text for token in tokens).strip(), usage) + if not any(token.labelled for token in tokens): + return completed + labelled: Final[SonioxTranscriptionCompleted] = {**completed, "segments": _segments(tokens)} + return labelled + + +def _soniox_error(response: _SonioxResponse) -> SonioxException | None: + if response.error_code is None and response.error_message is None: + return None + return SonioxException( + status_code=response.error_code or 500, + message=f"Soniox realtime error {response.error_code}: {response.error_message}", + ) + + +class SonioxEventTransformer: + def __init__(self, *, drops_originals: bool) -> None: + self._drops_originals: Final = drops_originals + self._item_id: str | None = None + self._final_tokens: Final[list[_SonioxToken]] = [] # mutable-ok: a turn's tokens append in O(1) each + self._finalizes: Final[deque[bool]] = deque() # mutable-ok: pending finalizes queue and pop in O(1) each + self._oldest_commit_answered: bool = False + self._processed_ms: int = 0 + self._billed_ms: int = 0 + self._refusal: SonioxException | None = None + + @property + def refusal(self) -> SonioxException | None: + return self._refusal + + def transform(self, payload: str) -> tuple[OpenAIRealtimeEvents, ...]: + try: + response: Final = _SonioxResponse.model_validate_json(payload) + except ValidationError: + raise SonioxProtocolError("Soniox returned an invalid realtime response") from None + self._bill(response.total_audio_proc_ms) + error: Final = _soniox_error(response) + if error is not None: + return (self._report(error),) + return tuple(chain.from_iterable(self._token_events(token) for token in response.tokens)) + + def queue_finalize(self, *, discards_turn: bool) -> None: + if discards_turn and not self._finalizes: + self._item_id = None + self._final_tokens.clear() + self._finalizes.append(discards_turn) + + def take_unbilled_usage(self, stream_ms: int = 0) -> RealtimeInputAudioTranscriptionUsage | None: + billable_ms: Final = max(self._processed_ms, stream_ms) + unbilled_ms: Final = billable_ms - self._billed_ms + if unbilled_ms <= 0: + return None + self._billed_ms = billable_ms + return duration_usage(unbilled_ms / 1000) + + def _report(self, error: SonioxException) -> OpenAIRealtimeErrorEvent: + if self._processed_ms == 0: + self._refusal = error + return error_event(error.message) + + def _bill(self, total_audio_proc_ms: int | None) -> None: + if total_audio_proc_ms is not None and total_audio_proc_ms > self._processed_ms: + self._processed_ms = total_audio_proc_ms + + def _token_events(self, token: _SonioxToken) -> tuple[OpenAIRealtimeEvents, ...]: + if token.text == _FINALIZED_TOKEN: + return self._finalized() + if self._discarding: + return () + if token.text == _ENDPOINT_TOKEN: + return self._end_turn() + started: Final = self._start() + if not token.is_final or (self._drops_originals and token.translation_status == "original"): + return started + self._final_tokens.append(token) + return (*started, _delta(self._current_item_id(), token)) + + @property + def _discarding(self) -> bool: + return bool(self._finalizes) and self._finalizes[0] + + def _finalized(self) -> tuple[OpenAIRealtimeEvents, ...]: + discarded: Final = self._finalizes.popleft() if self._finalizes else None + answered: Final = self._oldest_commit_answered + self._oldest_commit_answered = False + if discarded: + return () + if self._item_id is not None: + return self._complete(speech_stopped=False) + if discarded is None or answered: + return () + return (_completed(_new_item_id(), (), self.take_unbilled_usage()),) + + def _end_turn(self) -> tuple[OpenAIRealtimeEvents, ...]: + events: Final = self._complete(speech_stopped=True) + if events and self._finalizes: + self._oldest_commit_answered = True + return events + + def _start(self) -> tuple[OpenAIRealtimeEvents, ...]: + if self._item_id is not None: + return () + item_id: Final = _new_item_id() + self._item_id = item_id + return (speech_event("input_audio_buffer.speech_started", item_id),) + + def _current_item_id(self) -> str: + assert self._item_id is not None + return self._item_id + + def _complete(self, *, speech_stopped: bool) -> tuple[OpenAIRealtimeEvents, ...]: + item_id: Final = self._item_id + if item_id is None: + return () + tokens: Final = tuple(self._final_tokens) + self._item_id = None + self._final_tokens.clear() + stopped: Final[tuple[OpenAIRealtimeEvents, ...]] = ( + (speech_event("input_audio_buffer.speech_stopped", item_id),) if speech_stopped else () + ) + return (*stopped, _completed(item_id, tokens, self.take_unbilled_usage())) + + +class SonioxRealtimeBackend: + def __init__( + self, + inner: RealtimeBackend, + *, + keepalive_interval: float = KEEPALIVE_INTERVAL_SECONDS, + clock: Callable[[], float] = time.monotonic, + sleep: Callable[[float], Awaitable[None]] = asyncio.sleep, + ) -> None: + self._inner: Final = inner + self._interval: Final = keepalive_interval + self._clock: Final = clock + self._sleep: Final = sleep + self._frames: Final[asyncio.Queue[str | bytes | Exception]] = asyncio.Queue(maxsize=_MAX_PENDING_FRAMES) + self._last_sent: float = clock() + self._started: bool = False + self._ended: bool = False + self._tasks: tuple[asyncio.Task[None], ...] = () + + async def __aenter__(self) -> Self: + await self._inner.__aenter__() + self._tasks = (asyncio.create_task(self._pump()), asyncio.create_task(self._keepalive())) + return self + + async def __aexit__( + self, + exc_type: type[BaseException] | None, + exc_value: BaseException | None, + traceback: TracebackType | None, + ) -> None: + self._stop() + await self._inner.__aexit__(exc_type, exc_value, traceback) + + async def send(self, message: str | bytes) -> None: + self._last_sent = self._clock() + self._ended = self._ended or message == _END_STREAM + await self._inner.send(message) + if not self._started and isinstance(message, str) and message not in _CONTROL_FRAMES: + self._started = True + await self._frames.put(SESSION_STARTED_FRAME) + + async def recv(self, decode: bool | None = None) -> str | bytes: + frame: Final = await self._frames.get() + if isinstance(frame, Exception): + raise frame + return frame + + async def close(self) -> None: + self._stop() + await self._inner.close() + + def _stop(self) -> None: + for task in self._tasks: + task.cancel() + + async def _pump(self) -> None: + while True: + try: + frame = await self._inner.recv() + except Exception as e: # noqa: BLE001 # recv() re-raises it, so an upstream failure never hangs the reader + await self._frames.put(e) + return + await self._frames.put(frame) + + async def _keepalive(self) -> None: + while not self._ended: + await self._sleep(self._interval) + if self._ended or self._clock() - self._last_sent < self._interval: + continue + try: + await self.send(_KEEPALIVE) + except Exception as e: # noqa: BLE001 # stops only keepalives; the pump reports the broken socket + verbose_logger.debug("Soniox realtime: stopping keepalive after send failed: %s", e) + return + + +class SonioxRealtimeConfig(BaseRealtimeConfig): + def __init__( + self, + *, + options: SonioxRealtimeOptions | None = None, + wrap: Callable[[RealtimeBackend], RealtimeBackend] = SonioxRealtimeBackend, + clock: Callable[[], float] = time.monotonic, + drop_params: bool = False, + ) -> None: + self._options: Final = options or SonioxRealtimeOptions() + self._drop_params: Final = drop_params + self._wrap: Final = wrap + self._clock: Final = clock + self._transformer: Final = SonioxEventTransformer(drops_originals=self._options.translation is not None) + self._start_request: SonioxStartRequest | None = None + self._session_id: str = f"sess_{uuid.uuid4().hex}" + self._started_at: float | None = None + + @classmethod + def from_litellm_params(cls, litellm_params: Mapping[str, object], *, drop_params: bool = False) -> Self: + return cls(options=SonioxRealtimeOptions.model_validate(dict(litellm_params)), drop_params=drop_params) + + def requires_session_configuration(self) -> bool: + return True + + def validate_environment( + self, + headers: dict[str, str], # mutable-ok: BaseRealtimeConfig contract + model: str, + api_key: str | None = None, + ) -> dict[str, str]: # mutable-ok: BaseRealtimeConfig contract + resolved_key: Final = get_soniox_api_key(api_key) + if not resolved_key: + raise ValueError("Missing Soniox API key. Set SONIOX_API_KEY or api_key in the deployment litellm_params") + return {**headers, "Authorization": f"Bearer {resolved_key}"} + + def get_complete_url(self, api_base: str | None, model: str, api_key: str | None = None) -> str: + return build_soniox_realtime_url(api_base) + + def wrap_backend(self, backend: RealtimeBackend) -> RealtimeBackend: + return self._wrap(backend) + + def transform_session_created_event( + self, + model: str, + logging_session_id: str, + session_configuration_request: str | None = None, + ) -> OpenAIRealtimeTranscriptionSessionCreated: + self._session_id = logging_session_id + return _session_created(logging_session_id, build_start_request(model, None, self._options)) + + def transform_realtime_request( + self, + message: str, + model: str, + session_configuration_request: str | None = None, + ) -> tuple[str | bytes, ...]: + request: Final = json_object(message, SonioxProtocolError) + event_type: Final = request.get("type") + if event_type is None: + raise SonioxProtocolError("Soniox realtime takes OpenAI realtime events, and this message has no type") + if event_type in SESSION_UPDATE_EVENT_TYPES: + update: Final = parse_transcription_session_update(message, SonioxProtocolError) + ignored: Final = _ignored_settings(update) + if ignored: + verbose_logger.warning("Soniox realtime: ignoring unsupported session settings %s", ignored) + return self._start(model, update) + if event_type == "input_audio_buffer.append": + audio: Final = decode_pcm16_append(request.get("audio"), error=SonioxProtocolError) + return (*self._start(model, None), audio) + if event_type == "input_audio_buffer.commit": + return self._finalize(model, discards_turn=False) + if event_type == "input_audio_buffer.clear": + return () if self._start_request is None else self._finalize(model, discards_turn=True) + if event_type == "input_audio_buffer.end": + return (*self._start(model, None), _END_STREAM) + verbose_logger.debug("Soniox realtime: dropping unsupported client event %s", event_type) + return () + + def _finalize(self, model: str, *, discards_turn: bool) -> tuple[str, ...]: + start: Final = self._start(model, None) + self._transformer.queue_finalize(discards_turn=discards_turn) + return (*start, _FINALIZE) + + def session_refusal(self) -> Exception | None: + return self._transformer.refusal + + def unbilled_usage_on_session_close(self, model: str) -> RealtimeInputAudioTranscriptionUsage | None: + started_at: Final = self._started_at + if self._transformer.refusal is not None: + return None + if started_at is None: + return self._transformer.take_unbilled_usage() + return self._transformer.take_unbilled_usage(int((self._clock() - started_at) * 1000)) + + def transform_realtime_response( + self, + message: str | bytes, + model: str, + logging_obj: LiteLLMLoggingObj, + realtime_response_transform_input: RealtimeResponseTransformInput, + ) -> RealtimeResponseTypedDict: + payload: Final = message.decode("utf-8") if isinstance(message, bytes) else message + result: Final[RealtimeResponseTypedDict] = { + "response": list(self._backend_events(payload)), + "current_output_item_id": realtime_response_transform_input.get("current_output_item_id"), + "current_response_id": realtime_response_transform_input.get("current_response_id"), + "current_delta_chunks": realtime_response_transform_input.get("current_delta_chunks"), + "current_conversation_id": realtime_response_transform_input.get("current_conversation_id"), + "current_item_chunks": realtime_response_transform_input.get("current_item_chunks"), + "current_delta_type": realtime_response_transform_input.get("current_delta_type"), + "session_configuration_request": realtime_response_transform_input.get("session_configuration_request"), + } + return result + + def _backend_events(self, payload: str) -> tuple[OpenAIRealtimeEvents, ...]: + if payload != SESSION_STARTED_FRAME: + return self._transformer.transform(payload) + assert self._start_request is not None + return (_session_created(self._session_id, self._start_request),) + + def _start(self, model: str, update: TranscriptionSessionUpdate | None) -> tuple[str, ...]: + started: Final = self._start_request + if started is not None: + if update is not None and _changes_started_stream(update, started, self._drop_params): + raise SonioxProtocolError("Soniox realtime can't change the session once the stream has started") + return () + start_request: Final = build_start_request(model, update, self._options, self._drop_params) + self._start_request = start_request + self._started_at = self._clock() + return (start_request.model_dump_json(exclude_none=True),) + + +def _finishes_stream(frame: str | bytes) -> bool: + response: Final = _SonioxResponse.model_validate_json(frame) + error: Final = _soniox_error(response) + if error is not None: + raise error + return response.finished + + +async def soniox_realtime_health_check( + model: str, api_base: str | None, api_key: str | None, options: SonioxRealtimeOptions +) -> bool: + import websockets + + config: Final = SonioxRealtimeConfig() + url: Final = config.get_complete_url(api_base, model) + headers: Final = config.validate_environment({}, model, api_key) + start_request: Final = build_start_request(model, None, options) + async with websockets.connect( + url, + additional_headers=headers, + max_size=REALTIME_WEBSOCKET_MAX_MESSAGE_SIZE_BYTES, + ssl=realtime_ssl_for_url(url), + ) as backend: + await backend.send(start_request.model_dump_json(exclude_none=True)) + await backend.send(_HEALTH_CHECK_SILENCE) + await backend.send(_END_STREAM) + async for frame in backend: + if _finishes_stream(frame): + return True + raise SonioxException(status_code=502, message="Soniox closed the realtime stream without finishing it") diff --git a/litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/messages/transformation.py b/litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/messages/transformation.py index 1f0ee728cd1..bfa94396ea7 100644 --- a/litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/messages/transformation.py +++ b/litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/messages/transformation.py @@ -211,6 +211,12 @@ class VertexAIPartnerModelsAnthropicMessagesConfig(AnthropicMessagesConfig, Vert if messages_carry_output_config(messages): beta_values.add(ANTHROPIC_BETA_HEADER_VALUES.PER_TURN_CONTROL_2026_07_01.value) + beta_values.update( + anthropic_model_info.thinking_betas( + optional_params.get("thinking"), # pyright: ignore[reportUnknownMemberType, reportUnknownArgumentType] # optional_params is the base config's bare dict + ) + ) + if beta_values: request_headers["anthropic-beta"] = ",".join(beta_values) @@ -245,7 +251,9 @@ class VertexAIPartnerModelsAnthropicMessagesConfig(AnthropicMessagesConfig, Vert headers=headers, ) - self._normalize_system_role_messages(anthropic_messages_request, model=model) + self._normalize_system_role_messages( + anthropic_messages_request, model=model, drop_params=getattr(litellm_params, "drop_params", None) + ) self._remove_scope_from_cache_control(anthropic_messages_request) diff --git a/litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/transformation.py b/litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/transformation.py index 363e5596cb9..79fd9f9e8b1 100644 --- a/litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/transformation.py +++ b/litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/transformation.py @@ -145,6 +145,7 @@ class VertexAIAnthropicConfig(AnthropicConfig): file_id_used=self.is_file_id_used(messages), mcp_server_used=self.is_mcp_server_used(optional_params.get("mcp_servers")), custom_llm_provider="vertex_ai", + thinking=optional_params.get("thinking"), # pyright: ignore[reportUnknownMemberType, reportUnknownArgumentType] # optional_params is the base config's bare dict ) beta_set: Final = set(auto_betas) diff --git a/litellm/llms/vertex_ai/vertex_ai_partner_models/main.py b/litellm/llms/vertex_ai/vertex_ai_partner_models/main.py index bb200fd542d..a3a3427670d 100644 --- a/litellm/llms/vertex_ai/vertex_ai_partner_models/main.py +++ b/litellm/llms/vertex_ai/vertex_ai_partner_models/main.py @@ -9,6 +9,7 @@ import httpx import litellm from litellm import LlmProviders +from litellm.litellm_core_utils.prompt_templates.common_utils import messages_with_file_parts_as_image_urls from litellm.types.llms.vertex_ai import VertexPartnerProvider from litellm.utils import ModelResponse @@ -217,7 +218,7 @@ class VertexAIPartnerModels(VertexBase): return base_llm_http_handler.completion( model=model, stream=stream, - messages=messages, + messages=list(messages_with_file_parts_as_image_urls(messages)), acompletion=acompletion, api_base=api_base, model_response=model_response, diff --git a/litellm/main.py b/litellm/main.py index a3a63d5a83a..50ab274a060 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -108,6 +108,10 @@ from litellm.litellm_core_utils.mock_functions import ( from litellm.litellm_core_utils.prompt_templates.common_utils import ( get_content_from_model_response as get_content_from_model_response, # noqa: PLC0414 # public re-export ) +from litellm.litellm_core_utils.prompt_templates.common_utils import ( + messages_have_pdf_file_part, + messages_without_pdf_file_parts, +) from litellm.litellm_core_utils.provider_affinity import add_provider_affinity_header from litellm.litellm_core_utils.request_timeout_resolver import ( get_configured_request_timeout, @@ -155,6 +159,7 @@ from litellm.types.litellm_params import ControlOptions, RetryStrategy from litellm.types.llms.openai import AllMessageValues from litellm.types.router import GenericLiteLLMParams from litellm.types.utils import ( + AgenticLoopParams, BaseUrlRoutingHint, CustomPricingLiteLLMParams, ModelResponseStream, @@ -190,11 +195,13 @@ from litellm.utils import ( get_requester_metadata, get_secret, get_standard_openai_params, + is_explicitly_disabled_factory, mock_completion_streaming_obj, pre_process_non_default_params, read_config_args, should_run_mock_completion, supports_httpx_timeout, + supports_pdf_input, validate_and_fix_openai_messages, validate_and_fix_openai_tools, validate_and_fix_thinking_param, @@ -392,6 +399,125 @@ def _resolve_control_options(kwargs: Mapping[str, object], model: str) -> Contro return assert_never(control) +PROVIDERS_WITHOUT_DOCUMENT_INPUT: Final = frozenset({"ollama", "ollama_chat"}) +PROVIDERS_DROPPING_UNDECLARED_PDF_INPUT: Final = frozenset({"azure_ai"}) + + +def _cost_map_pdf_input_support(model: str, custom_llm_provider: str) -> bool | None: + if custom_llm_provider in PROVIDERS_WITHOUT_DOCUMENT_INPUT or is_explicitly_disabled_factory( + model, custom_llm_provider, "supports_pdf_input" + ): + return False + if supports_pdf_input(model, custom_llm_provider): + return True + return None + + +def _deployment_pdf_input_support(model: str, base_model: str, custom_llm_provider: str) -> bool | None: + supports: Final = ( + _cost_map_pdf_input_support(model, custom_llm_provider), + _cost_map_pdf_input_support(base_model, custom_llm_provider), + ) + if False in supports: + return False + if True in supports: + return True + return None + + +def _declared_pdf_input_support( + model: str, custom_llm_provider: str, model_info: Mapping[str, object] | None, base_model: str | None +) -> bool | None: + match model_info: + case {"supports_pdf_input": bool() as declared}: + return declared + case _ if base_model: + return _deployment_pdf_input_support(model, base_model, custom_llm_provider) + case _: + return _cost_map_pdf_input_support(model, custom_llm_provider) + + +def _pdf_input_unreadable( + model: str, + custom_llm_provider: str, + model_info: Mapping[str, object] | None, + base_model: str | None, + drop_params: bool, +) -> bool: + match _declared_pdf_input_support(model, custom_llm_provider, model_info, base_model): + case False: + return True + case None: + return drop_params and custom_llm_provider in PROVIDERS_DROPPING_UNDECLARED_PDF_INPUT + case True: + return False + + +def _messages_without_unreadable_pdfs( + messages: Sequence[AllMessageValues], + model: str, + custom_llm_provider: str, + model_info: Mapping[str, object] | None, + base_model: str | None, + drop_params: bool, +) -> tuple[AllMessageValues, ...] | None: + if not messages_have_pdf_file_part(messages) or not _pdf_input_unreadable( + model, custom_llm_provider, model_info, base_model, drop_params + ): + return None + if not drop_params: + raise litellm.BadRequestError( + message=( + f"{custom_llm_provider}/{model} does not accept PDF input. " + "Remove the PDF, or set drop_params=True to send the request without it" + ), + model=model, + llm_provider=custom_llm_provider, + ) + readable_messages: Final = messages_without_pdf_file_parts(messages) + if not any(message["role"] == "user" for message in readable_messages): + raise litellm.BadRequestError( + message=( + f"{custom_llm_provider}/{model} does not accept PDF input, " + "and no user message is left once the PDF is dropped" + ), + model=model, + llm_provider=custom_llm_provider, + ) + return readable_messages + + +def _messages_readable_by_model( + messages: Sequence[AllMessageValues], + model: str, + custom_llm_provider: str, + model_info: Mapping[str, object] | None, + base_model: str | None, + drop_params: bool, + ensure_alternating_roles: bool | None, + user_continue_message: ChatCompletionUserMessage | None, + assistant_continue_message: ChatCompletionAssistantMessage | None, +) -> tuple[AllMessageValues, ...] | None: + pdf_free_messages: Final = _messages_without_unreadable_pdfs( + messages=messages, + model=model, + custom_llm_provider=custom_llm_provider, + model_info=model_info, + base_model=base_model, + drop_params=drop_params, + ) + if pdf_free_messages is None: + return None + return tuple( + get_completion_messages( + messages=list(pdf_free_messages), + ensure_alternating_roles=ensure_alternating_roles or False, + user_continue_message=user_continue_message, + assistant_continue_message=assistant_continue_message, + ) + ) + + ####### COMPLETION ENDPOINTS ################ @@ -1272,6 +1398,16 @@ def _get_router_deployment_id(kwargs: dict) -> str | None: return None +def _record_agentic_loop_params( + logging_obj: object, requested_model: str, custom_llm_provider: str, owns_agentic_loop: bool +) -> None: + if not owns_agentic_loop or not isinstance(logging_obj, LiteLLMLoggingObj): + return + logging_obj.record_agentic_loop_params( + AgenticLoopParams(model=requested_model, custom_llm_provider=custom_llm_provider) + ) + + def _register_custom_pricing_for_request( model: str, custom_llm_provider: str, @@ -4223,7 +4359,6 @@ def _complete_sagemaker(ctx: CompletionDispatchContext) -> _CompletionDispatchRe _ADDITIONAL_DROP_PARAMS_ADAPTER: Final = TypeAdapter(list[str]) _OPTIONAL_PARAMS_ADAPTER: Final = TypeAdapter(dict[str, object]) _OLLAMA_EMBEDDING_HEADERS_ADAPTER: Final = TypeAdapter(dict[str, str]) -_REQUEST_KWARGS_ADAPTER: Final = TypeAdapter(Mapping[str, object]) _MODEL_INFO_ADAPTER: Final = TypeAdapter(Mapping[str, object]) _PROVIDER_ROUTING_HINTS: Final = TypeAdapter(ProviderRoutingHints) _BASE_URL_HINT: Final = TypeAdapter(BaseUrlRoutingHint) @@ -5547,9 +5682,15 @@ def completion( ) headers = kwargs.get("headers", None) or extra_headers - ensure_alternating_roles: Final[bool | None] = kwargs.get("ensure_alternating_roles", None) - user_continue_message: Final[ChatCompletionUserMessage | None] = kwargs.get("user_continue_message", None) - assistant_continue_message: ChatCompletionAssistantMessage | None = kwargs.get("assistant_continue_message", None) + ensure_alternating_roles: Final = cast( # cast-ok: read from untyped kwargs + bool | None, kwargs.get("ensure_alternating_roles", None) + ) + user_continue_message: Final = cast( # cast-ok: read from untyped kwargs + ChatCompletionUserMessage | None, kwargs.get("user_continue_message", None) + ) + assistant_continue_message: Final = cast( # cast-ok: read from untyped kwargs + ChatCompletionAssistantMessage | None, kwargs.get("assistant_continue_message", None) + ) headers = {} if headers is None else dict(headers) if extra_headers is not None: headers.update(extra_headers) @@ -5691,6 +5832,7 @@ def completion( **{k: kwargs[k] for k in OPTIONAL_KWARGS_KEYS | {"provider_specific_header"} if k in kwargs}, "extra_headers": extra_headers, } + requested_model: Final = model model, custom_llm_provider, dynamic_api_key, api_base = get_llm_provider( model=model, custom_llm_provider=custom_llm_provider, @@ -5702,6 +5844,12 @@ def completion( else None ), ) + _record_agentic_loop_params( + logging_obj=logging, + requested_model=requested_model, + custom_llm_provider=custom_llm_provider, + owns_agentic_loop=not skip_responses_api_bridge, + ) deployment_model_info: Final = str_keyed_mapping( cast(object, model_info) # cast-ok: model_info is the untyped dict kwarg @@ -5910,15 +6058,17 @@ def completion( def optional_params_for_model_info(deployment_info: Mapping[str, object] | None) -> dict[str, object]: return get_optional_params(**mapping_args, model_info=deployment_info, **mapping_special_params) - optional_params = optional_params_for_model_info( + validated_model_info: Final = ( _MODEL_INFO_ADAPTER.validate_python(model_info) if isinstance(model_info, dict) else None ) + deployment_base_model: Final = base_model if isinstance(base_model, str) else None + optional_params = optional_params_for_model_info(validated_model_info) fakes_stream: Final = partial( _completion_fakes_stream, model, custom_llm_provider, provider_config, - base_model=base_model if isinstance(base_model, str) else None, + base_model=deployment_base_model, bridges_to_responses_api=bridges_to_responses_api is True, ) resolve_timeout: Final = partial( @@ -5956,16 +6106,29 @@ def completion( functions_unsupported_model: Final[list[object]] = TypeAdapter(list[object]).validate_python( optional_params.pop("functions_unsupported_model") ) - request_kwargs: Final = _REQUEST_KWARGS_ADAPTER.validate_python(kwargs) messages = function_call_prompt( messages=messages, functions=functions_unsupported_model, model=model, llm_provider=custom_llm_provider, - drop_params=litellm.drop_params is True - or normalize_drop_params(request_kwargs.get("drop_params")) is True, + drop_params=drop_unsupported_reasoning, ) + readable_messages: Final = _messages_readable_by_model( + messages=cast(list[AllMessageValues], messages), # cast-ok: completion types messages as a bare List + model=model, + custom_llm_provider=custom_llm_provider, + model_info=validated_model_info, + base_model=deployment_base_model, + drop_params=drop_unsupported_reasoning, + ensure_alternating_roles=ensure_alternating_roles, + user_continue_message=user_continue_message, + assistant_continue_message=assistant_continue_message, + ) + messages = ( # rebind-ok: completion() rewrites messages step by step + messages if readable_messages is None else list(readable_messages) + ) + # For logging - save the values of the litellm-specific params passed in requested_litellm_params: Final = get_litellm_params( acompletion=acompletion, diff --git a/litellm/model_prices_and_context_window_backup.json b/litellm/model_prices_and_context_window_backup.json index aa00ea73f8c..c6bbb63b6fc 100644 --- a/litellm/model_prices_and_context_window_backup.json +++ b/litellm/model_prices_and_context_window_backup.json @@ -4049,6 +4049,7 @@ "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, "supports_parallel_function_calling": true, + "supports_pdf_input": false, "supports_response_schema": true, "supports_tool_choice": true }, @@ -12437,7 +12438,8 @@ "supported_output_modalities": [ "text" ], - "supports_native_streaming": true + "supports_native_streaming": true, + "supports_pdf_input": false }, "azure_ai/FLUX-1.1-pro": { "deprecation_date": "2027-01-05", @@ -12502,6 +12504,7 @@ "output_cost_per_token": 1.85e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -12516,6 +12519,7 @@ "output_cost_per_token": 3.828e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -12531,6 +12535,7 @@ "output_cost_per_token": 3.52e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -12546,6 +12551,7 @@ "output_cost_per_token": 4.84e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -12560,6 +12566,7 @@ "output_cost_per_token": 4.84e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -12574,6 +12581,7 @@ "output_cost_per_token": 7.26e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -12594,6 +12602,7 @@ "text" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -12616,6 +12625,7 @@ "text" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true, @@ -12638,6 +12648,7 @@ "text" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true, @@ -12660,6 +12671,7 @@ "text" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true, @@ -12687,6 +12699,7 @@ "text" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true, @@ -12703,6 +12716,7 @@ "output_cost_per_token": 1.32e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -12724,6 +12738,7 @@ "text" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true, @@ -12744,6 +12759,7 @@ "text" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true, @@ -12765,6 +12781,7 @@ "text" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -12833,6 +12850,7 @@ "text" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -12847,6 +12865,7 @@ "output_cost_per_token": 7.1e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/foundry-models/concepts/models-sold-directly-by-azure", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true }, "azure_ai/Llama-4-Maverick-17B-128E-Instruct-FP8": { @@ -12859,6 +12878,7 @@ "output_cost_per_token": 1e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": true }, @@ -12872,6 +12892,7 @@ "output_cost_per_token": 7.8e-07, "source": "https://azure.microsoft.com/en-us/blog/introducing-the-llama-4-herd-in-azure-ai-foundry-and-azure-databricks/", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": true }, @@ -12884,6 +12905,7 @@ "mode": "chat", "output_cost_per_token": 3.7e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true }, "azure_ai/Meta-Llama-3.1-70B-Instruct": { @@ -12895,6 +12917,7 @@ "mode": "chat", "output_cost_per_token": 3.54e-06, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true }, "azure_ai/Phi-3-medium-128k-instruct": { @@ -12906,6 +12929,7 @@ "mode": "chat", "output_cost_per_token": 6.8e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": false }, @@ -12918,6 +12942,7 @@ "mode": "chat", "output_cost_per_token": 6.8e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": false }, @@ -12930,6 +12955,7 @@ "mode": "chat", "output_cost_per_token": 5.2e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": false }, @@ -12942,6 +12968,7 @@ "mode": "chat", "output_cost_per_token": 5.2e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": false }, @@ -12954,6 +12981,7 @@ "mode": "chat", "output_cost_per_token": 6e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": false }, @@ -12966,6 +12994,7 @@ "mode": "chat", "output_cost_per_token": 6e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": false }, @@ -12978,6 +13007,7 @@ "mode": "chat", "output_cost_per_token": 6.4e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": false }, @@ -12990,6 +13020,7 @@ "mode": "chat", "output_cost_per_token": 5.2e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": false }, @@ -13002,6 +13033,7 @@ "mode": "chat", "output_cost_per_token": 5.2e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": true }, @@ -13015,6 +13047,7 @@ "output_cost_per_token": 5e-07, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": false }, @@ -13027,7 +13060,8 @@ "mode": "chat", "output_cost_per_token": 3e-07, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", - "supports_function_calling": true + "supports_function_calling": true, + "supports_pdf_input": false }, "azure_ai/Phi-4-multimodal-instruct": { "deprecation_date": "2027-01-17", @@ -13041,6 +13075,7 @@ "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_audio_input": true, "supports_function_calling": true, + "supports_pdf_input": false, "supports_vision": true }, "azure_ai/Phi-4-mini-reasoning": { @@ -13052,7 +13087,8 @@ "mode": "chat", "output_cost_per_token": 3e-07, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", - "supports_function_calling": true + "supports_function_calling": true, + "supports_pdf_input": false }, "azure_ai/Phi-4-reasoning": { "deprecation_date": "2027-01-17", @@ -13064,6 +13100,7 @@ "output_cost_per_token": 5e-07, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true, "supports_reasoning": true }, @@ -13106,6 +13143,7 @@ "output_cost_per_token": 1e-05, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true }, "azure_ai/doc-intelligence/prebuilt-read": { @@ -13144,6 +13182,7 @@ "mode": "chat", "output_cost_per_token": 5.4e-06, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_reasoning": true, "supports_tool_choice": true }, @@ -13202,6 +13241,7 @@ "source": "https://learn.microsoft.com/en-us/azure/foundry/foundry-models/concepts/models-sold-directly-by-azure", "supports_assistant_prefill": true, "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -13217,6 +13257,7 @@ "source": "https://learn.microsoft.com/en-us/azure/foundry/foundry-models/concepts/models-sold-directly-by-azure", "supports_assistant_prefill": true, "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -13230,6 +13271,7 @@ "mode": "chat", "output_cost_per_token": 4.56e-06, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true }, "azure_ai/deepseek-v4-pro": { @@ -13242,6 +13284,7 @@ "output_cost_per_token": 3.48e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_reasoning": true, "supports_tool_choice": true, "cache_read_input_token_cost": 1.45e-07, @@ -13257,6 +13300,7 @@ "output_cost_per_token": 5.1e-07, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_reasoning": true, "supports_tool_choice": true, "cache_read_input_token_cost": 2.8e-08, @@ -13273,6 +13317,7 @@ "output_cost_per_token": 1.32e-06, "source": "https://learn.microsoft.com/en-us/azure/foundry/foundry-models/concepts/models-sold-directly-by-azure", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -13306,6 +13351,7 @@ "output_cost_per_token": 1.5e-05, "source": "https://learn.microsoft.com/en-us/azure/foundry/foundry-models/concepts/models-sold-directly-by-azure", "supports_function_calling": true, + "supports_pdf_input": false, "supports_response_schema": true, "supports_tool_choice": true, "supports_web_search": true @@ -13323,6 +13369,7 @@ "output_cost_per_token_above_200k_tokens": 5e-06, "source": "https://learn.microsoft.com/en-us/azure/foundry/foundry-models/concepts/models-sold-directly-by-azure", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_response_schema": true, @@ -13343,6 +13390,7 @@ "output_cost_per_token_above_200k_tokens": 1.2e-05, "source": "https://azure.microsoft.com/en-us/pricing/details/ai-foundry-models/grok/", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_response_schema": true, @@ -13361,6 +13409,7 @@ "output_cost_per_token": 2.5e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_response_schema": true, "supports_tool_choice": true, @@ -13379,6 +13428,7 @@ "output_cost_per_token": 2.5e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_response_schema": true, "supports_tool_choice": true, @@ -13395,6 +13445,7 @@ "mode": "chat", "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_response_schema": true, "supports_tool_choice": true, "supports_web_search": true @@ -13409,6 +13460,7 @@ "mode": "chat", "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_reasoning": true, "supports_response_schema": true, "supports_tool_choice": true, @@ -13424,6 +13476,7 @@ "output_cost_per_token": 1.5e-06, "source": "https://learn.microsoft.com/en-us/azure/foundry/foundry-models/concepts/models-sold-directly-by-azure", "supports_function_calling": true, + "supports_pdf_input": false, "supports_response_schema": true, "supports_tool_choice": true, "supports_web_search": true @@ -13436,7 +13489,8 @@ "max_output_tokens": 8192, "mode": "chat", "output_cost_per_token": 0.00971, - "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models" + "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false }, "azure_ai/jamba-instruct": { "deprecation_date": "2025-03-01", @@ -13447,6 +13501,7 @@ "mode": "chat", "output_cost_per_token": 7e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true }, "azure_ai/kimi-k2.5": { @@ -13459,6 +13514,7 @@ "output_cost_per_token": 3e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true, "supports_video_input": true, "supports_vision": true, @@ -13482,6 +13538,7 @@ "text" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_reasoning": true, "supports_tool_choice": true, "supports_vision": true, @@ -13498,6 +13555,7 @@ "output_cost_per_token": 4e-08, "source": "https://marketplace.microsoft.com/en/marketplace/apps/000-000.ministral-3b-2410-offer?tab=Overview", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true }, "azure_ai/mistral-large": { @@ -13510,6 +13568,7 @@ "output_cost_per_token": 1.2e-05, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true }, "azure_ai/mistral-large-2407": { @@ -13522,6 +13581,7 @@ "output_cost_per_token": 6e-06, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true }, "azure_ai/mistral-large-latest": { @@ -13533,6 +13593,7 @@ "output_cost_per_token": 6e-06, "source": "https://marketplace.microsoft.com/en/marketplace/apps/000-000.mistral-ai-large-2407-offer?tab=Overview", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true }, "azure_ai/mistral-large-3": { @@ -13544,6 +13605,7 @@ "output_cost_per_token": 1.5e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": true }, @@ -13557,6 +13619,7 @@ "output_cost_per_token": 2e-06, "supports_assistant_prefill": true, "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true }, "azure_ai/mistral-nemo": { @@ -13568,7 +13631,8 @@ "mode": "chat", "output_cost_per_token": 1.5e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", - "supports_function_calling": true + "supports_function_calling": true, + "supports_pdf_input": false }, "azure_ai/mistral-small": { "deprecation_date": "2025-07-31", @@ -13580,6 +13644,7 @@ "output_cost_per_token": 3e-06, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true }, "azure_ai/mistral-small-2503": { @@ -13591,6 +13656,7 @@ "mode": "chat", "output_cost_per_token": 3e-07, "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": true }, @@ -52006,6 +52072,7 @@ ], "supports_assistant_prefill": false, "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -52026,6 +52093,7 @@ ], "supports_assistant_prefill": false, "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -52777,6 +52845,7 @@ "output_cost_per_token": 2.5e-06, "source": "https://cloud.google.com/gemini-enterprise-agent-platform/generative-ai/pricing", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true, "supports_web_search": true }, @@ -52794,6 +52863,7 @@ "global" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_reasoning": true, "supports_tool_choice": true }, @@ -52811,6 +52881,7 @@ "global" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -52989,6 +53060,7 @@ "output_cost_per_token": 3.6e-07, "output_cost_per_token_batches": 1.8e-07, "source": "https://cloud.google.com/gemini-enterprise-agent-platform/generative-ai/pricing", + "supports_pdf_input": false, "supports_reasoning": true }, "vertex_ai/openai/gpt-oss-20b-maas": { @@ -53000,6 +53072,7 @@ "mode": "chat", "output_cost_per_token": 2.5e-07, "source": "https://cloud.google.com/gemini-enterprise-agent-platform/generative-ai/pricing", + "supports_pdf_input": false, "supports_reasoning": true, "cache_read_input_token_cost": 7e-09, "input_cost_per_token_batches": 3.5e-08, @@ -53131,6 +53204,7 @@ "us-south1" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true }, "vertex_ai/qwen/qwen3-coder-480b-a35b-instruct-maas": { @@ -53149,6 +53223,7 @@ "global" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true }, "vertex_ai/qwen/qwen3-next-80b-a3b-instruct-maas": { @@ -53164,6 +53239,7 @@ "global" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true }, "vertex_ai/qwen/qwen3-next-80b-a3b-thinking-maas": { @@ -53179,6 +53255,7 @@ "global" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true }, "vertex_ai/veo-3.1-generate-preview": { @@ -61177,6 +61254,126 @@ ], "supports_audio_input": true }, + "soniox/stt-rt-v5": { + "input_cost_per_second": 3.333e-05, + "litellm_provider": "soniox", + "metadata": { + "calculation": "$0.12/3600 seconds = $0.00003333 per second", + "original_pricing_per_hour": 0.12 + }, + "mode": "audio_transcription", + "source": "https://soniox.com/pricing", + "supported_endpoints": [ + "/v1/realtime" + ], + "supported_modalities": [ + "audio" + ], + "supported_output_modalities": [ + "text" + ], + "supports_audio_input": true + }, + "soniox/stt-rt-preview": { + "input_cost_per_second": 3.333e-05, + "litellm_provider": "soniox", + "metadata": { + "calculation": "$0.12/3600 seconds = $0.00003333 per second", + "original_pricing_per_hour": 0.12 + }, + "mode": "audio_transcription", + "source": "https://soniox.com/pricing", + "supported_endpoints": [ + "/v1/realtime" + ], + "supported_modalities": [ + "audio" + ], + "supported_output_modalities": [ + "text" + ], + "supports_audio_input": true + }, + "soniox/stt-rt-preview-v2": { + "input_cost_per_second": 3.333e-05, + "litellm_provider": "soniox", + "metadata": { + "calculation": "$0.12/3600 seconds = $0.00003333 per second", + "original_pricing_per_hour": 0.12 + }, + "mode": "audio_transcription", + "source": "https://soniox.com/pricing", + "supported_endpoints": [ + "/v1/realtime" + ], + "supported_modalities": [ + "audio" + ], + "supported_output_modalities": [ + "text" + ], + "supports_audio_input": true + }, + "soniox/stt-rt-v3": { + "input_cost_per_second": 3.333e-05, + "litellm_provider": "soniox", + "metadata": { + "calculation": "$0.12/3600 seconds = $0.00003333 per second", + "original_pricing_per_hour": 0.12 + }, + "mode": "audio_transcription", + "source": "https://soniox.com/pricing", + "supported_endpoints": [ + "/v1/realtime" + ], + "supported_modalities": [ + "audio" + ], + "supported_output_modalities": [ + "text" + ], + "supports_audio_input": true + }, + "soniox/stt-rt-v3-preview": { + "input_cost_per_second": 3.333e-05, + "litellm_provider": "soniox", + "metadata": { + "calculation": "$0.12/3600 seconds = $0.00003333 per second", + "original_pricing_per_hour": 0.12 + }, + "mode": "audio_transcription", + "source": "https://soniox.com/pricing", + "supported_endpoints": [ + "/v1/realtime" + ], + "supported_modalities": [ + "audio" + ], + "supported_output_modalities": [ + "text" + ], + "supports_audio_input": true + }, + "soniox/stt-rt-v4": { + "input_cost_per_second": 3.333e-05, + "litellm_provider": "soniox", + "metadata": { + "calculation": "$0.12/3600 seconds = $0.00003333 per second", + "original_pricing_per_hour": 0.12 + }, + "mode": "audio_transcription", + "source": "https://soniox.com/pricing", + "supported_endpoints": [ + "/v1/realtime" + ], + "supported_modalities": [ + "audio" + ], + "supported_output_modalities": [ + "text" + ], + "supports_audio_input": true + }, "tensormesh/Qwen/Qwen3.5-397B-A17B-FP8": { "litellm_provider": "tensormesh", "mode": "chat", @@ -67202,6 +67399,7 @@ "text" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true, @@ -67216,6 +67414,7 @@ "mode": "chat", "output_cost_per_token": 1.2e-06, "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true, @@ -67258,6 +67457,7 @@ "output_cost_per_token": 1.5e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -67272,6 +67472,7 @@ "output_cost_per_token": 3.1e-07, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -67286,6 +67487,7 @@ "output_cost_per_token": 5.5e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -67300,6 +67502,7 @@ "output_cost_per_token": 6.25e-07, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -67316,6 +67519,7 @@ "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, "supports_parallel_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_response_schema": true, @@ -67331,6 +67535,7 @@ "output_cost_per_token": 3.2e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_reasoning": true, "supports_tool_choice": true }, @@ -67349,6 +67554,7 @@ "supported_output_modalities": [ "text" ], + "supports_pdf_input": false, "supports_response_schema": true, "supports_vision": true }, @@ -67358,7 +67564,8 @@ "output_cost_per_token": 5.4e-06, "litellm_provider": "azure_ai", "mode": "chat", - "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'" + "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", + "supports_pdf_input": false }, "azure_ai/deepseek-v3-0324": { "deprecation_date": "2026-07-13", @@ -67366,7 +67573,8 @@ "output_cost_per_token": 4.56e-06, "litellm_provider": "azure_ai", "mode": "chat", - "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'" + "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", + "supports_pdf_input": false }, "azure_ai/deepseek-v3.1": { "deprecation_date": "2026-07-13", @@ -67374,7 +67582,8 @@ "output_cost_per_token": 4.94e-06, "litellm_provider": "azure_ai", "mode": "chat", - "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'" + "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", + "supports_pdf_input": false }, "azure_ai/grok-3": { "deprecation_date": "2026-05-01", @@ -67382,7 +67591,8 @@ "output_cost_per_token": 1.5e-05, "litellm_provider": "azure_ai", "mode": "chat", - "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'" + "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", + "supports_pdf_input": false }, "azure_ai/grok-3-mini": { "deprecation_date": "2026-05-01", @@ -67390,7 +67600,8 @@ "output_cost_per_token": 1.27e-06, "litellm_provider": "azure_ai", "mode": "chat", - "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'" + "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", + "supports_pdf_input": false }, "azure_ai/grok-4-fast-non-reasoning": { "deprecation_date": "2026-05-01", @@ -67398,7 +67609,8 @@ "output_cost_per_token": 5e-07, "litellm_provider": "azure_ai", "mode": "chat", - "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'" + "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", + "supports_pdf_input": false }, "azure_ai/grok-4-fast-reasoning": { "deprecation_date": "2026-05-01", @@ -67406,7 +67618,8 @@ "output_cost_per_token": 5e-07, "litellm_provider": "azure_ai", "mode": "chat", - "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'" + "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", + "supports_pdf_input": false }, "bedrock/us-gov-west-1/nvidia.nemotron-nano-3-30b": { "input_cost_per_token": 7.2e-08, @@ -73076,6 +73289,7 @@ "source": "https://cloud.google.com/gemini-enterprise-agent-platform/generative-ai/pricing", "supported_regions": ["global"], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_response_schema": true, @@ -73691,6 +73905,7 @@ "output_cost_per_token": 3.5e-06, "source": "https://azure.microsoft.com/en-us/pricing/details/ai-foundry-models/microsoft/", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -74745,6 +74960,7 @@ "text" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": false, "supports_reasoning": true, "supports_tool_choice": true, @@ -74993,6 +75209,7 @@ "output_cost_per_token_above_200k_tokens": 1.2e-05, "source": "https://docs.x.ai/developers/models/grok-4.7", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_response_schema": true, @@ -78341,6 +78558,1254 @@ "supports_xhigh_reasoning_effort": true, "use_openai_responses_path": true }, + "kyma/claude-fable-5": { + "max_input_tokens": 1000000, + "max_output_tokens": 128000, + "input_cost_per_token": 1.35e-05, + "output_cost_per_token": 6.75e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true, + "supports_adaptive_thinking": true, + "thinking_always_on": true + }, + "kyma/claude-fable-5.1": { + "max_input_tokens": 1000000, + "max_output_tokens": 128000, + "input_cost_per_token": 1.35e-05, + "output_cost_per_token": 6.75e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true, + "supports_adaptive_thinking": true, + "thinking_always_on": true + }, + "kyma/claude-haiku-4-5": { + "max_input_tokens": 200000, + "max_output_tokens": 64000, + "input_cost_per_token": 1.35e-06, + "cache_read_input_token_cost": 1.35e-07, + "output_cost_per_token": 6.75e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false, + "prompt_cache_min_tokens": 4096 + }, + "kyma/claude-haiku-5.5": { + "max_input_tokens": 1000000, + "max_output_tokens": 128000, + "input_cost_per_token": 1.35e-07, + "cache_read_input_token_cost": 1.35e-08, + "output_cost_per_token": 6.75e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true, + "supports_adaptive_thinking": true, + "thinking_always_on": false, + "prompt_cache_min_tokens": 512 + }, + "kyma/claude-opus-4-7": { + "max_input_tokens": 1000000, + "max_output_tokens": 128000, + "input_cost_per_token": 6.75e-06, + "cache_read_input_token_cost": 6.75e-07, + "output_cost_per_token": 3.375e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false, + "supports_adaptive_thinking": true, + "prompt_cache_min_tokens": 2048 + }, + "kyma/claude-opus-5": { + "max_input_tokens": 1000000, + "max_output_tokens": 128000, + "input_cost_per_token": 6.75e-06, + "cache_read_input_token_cost": 6.75e-07, + "output_cost_per_token": 3.375e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true, + "supports_adaptive_thinking": true, + "prompt_cache_min_tokens": 512 + }, + "kyma/claude-opus-5.5": { + "max_input_tokens": 1000000, + "max_output_tokens": 128000, + "input_cost_per_token": 5.4e-06, + "cache_read_input_token_cost": 2.7e-07, + "output_cost_per_token": 2.7e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true, + "supports_adaptive_thinking": true, + "thinking_always_on": true, + "prompt_cache_min_tokens": 512 + }, + "kyma/claude-sonnet-4-6": { + "max_input_tokens": 1000000, + "max_output_tokens": 128000, + "input_cost_per_token": 4.05e-06, + "cache_read_input_token_cost": 4.05e-07, + "output_cost_per_token": 2.025e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false, + "supports_adaptive_thinking": true, + "prompt_cache_min_tokens": 1024 + }, + "kyma/claude-sonnet-5": { + "max_input_tokens": 1000000, + "max_output_tokens": 128000, + "input_cost_per_token": 2.7e-06, + "cache_read_input_token_cost": 2.7e-07, + "output_cost_per_token": 1.35e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true, + "supports_adaptive_thinking": true, + "prompt_cache_min_tokens": 1024 + }, + "kyma/claude-sonnet-5.5": { + "max_input_tokens": 1000000, + "max_output_tokens": 128000, + "input_cost_per_token": 2.7e-06, + "cache_read_input_token_cost": 1.35e-07, + "output_cost_per_token": 1.35e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true, + "supports_adaptive_thinking": true, + "thinking_always_on": true, + "prompt_cache_min_tokens": 512 + }, + "kyma/deepseek-r1": { + "max_input_tokens": 64000, + "max_output_tokens": 32768, + "input_cost_per_token": 7.425e-07, + "output_cost_per_token": 2.957e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/deepseek-v3": { + "max_input_tokens": 160000, + "max_output_tokens": 8192, + "input_cost_per_token": 5.18e-07, + "output_cost_per_token": 7.571e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/deepseek-v4-flash": { + "max_input_tokens": 1000000, + "max_output_tokens": 65536, + "input_cost_per_token": 1.23e-07, + "cache_read_input_token_cost": 2.43e-08, + "output_cost_per_token": 2.458e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/deepseek-v4-flash-vision-exp": { + "max_input_tokens": 1048576, + "max_output_tokens": 384000, + "input_cost_per_token": 3.892e-07, + "output_cost_per_token": 1.168e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/deepseek-v4.1-flash": { + "max_input_tokens": 1048576, + "max_output_tokens": 131072, + "input_cost_per_token": 2.7e-07, + "output_cost_per_token": 8.1e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/gemini-3-flash": { + "max_input_tokens": 1048576, + "max_output_tokens": 8192, + "input_cost_per_token": 6.75e-07, + "cache_read_input_token_cost": 6.75e-08, + "output_cost_per_token": 4.05e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/gemini-3.1-pro": { + "max_input_tokens": 1048576, + "max_output_tokens": 65536, + "input_cost_per_token": 2.7e-06, + "cache_read_input_token_cost": 2.7e-07, + "output_cost_per_token": 1.62e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/gemini-3.5-flash": { + "max_input_tokens": 1048576, + "max_output_tokens": 8192, + "input_cost_per_token": 2.025e-06, + "cache_read_input_token_cost": 2.025e-07, + "output_cost_per_token": 1.215e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/gemini-3.5-flash-lite": { + "max_input_tokens": 1048576, + "max_output_tokens": 8192, + "input_cost_per_token": 4.05e-07, + "cache_read_input_token_cost": 4.05e-08, + "output_cost_per_token": 3.375e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/gemini-3.6-flash": { + "max_input_tokens": 1048576, + "max_output_tokens": 8192, + "input_cost_per_token": 1.013e-06, + "cache_read_input_token_cost": 1.0125e-07, + "output_cost_per_token": 5.063e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": false, + "supports_pdf_input": false + }, + "kyma/gemini-3.7-flash": { + "max_input_tokens": 1048576, + "max_output_tokens": 65536, + "input_cost_per_token": 2.026e-06, + "cache_read_input_token_cost": 1.0125e-07, + "output_cost_per_token": 1.0126e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/gemini-3.8-flash": { + "max_input_tokens": 1048576, + "max_output_tokens": 65536, + "input_cost_per_token": 2.026e-06, + "output_cost_per_token": 1.0126e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/gemini-4-argon": { + "max_input_tokens": 1048576, + "max_output_tokens": 65536, + "input_cost_per_token": 5.4e-06, + "cache_read_input_token_cost": 2.7e-07, + "output_cost_per_token": 2.7e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/gemma-4-31b": { + "max_input_tokens": 128000, + "max_output_tokens": 8192, + "input_cost_per_token": 1.642e-07, + "cache_read_input_token_cost": 1.642e-07, + "output_cost_per_token": 4.689e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/glm-4.5-air": { + "max_input_tokens": 131072, + "max_output_tokens": 8192, + "input_cost_per_token": 1.797e-07, + "cache_read_input_token_cost": 3.375e-08, + "output_cost_per_token": 1.175e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/glm-4.7-flash": { + "max_input_tokens": 203000, + "max_output_tokens": 65536, + "input_cost_per_token": 8.1e-08, + "cache_read_input_token_cost": 1.35e-08, + "output_cost_per_token": 5.4e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/glm-5.1": { + "max_input_tokens": 203000, + "max_output_tokens": 65536, + "input_cost_per_token": 1.89e-06, + "cache_read_input_token_cost": 2.7675e-07, + "output_cost_per_token": 5.94e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/glm-5.2": { + "max_input_tokens": 1000000, + "max_output_tokens": 131072, + "input_cost_per_token": 9.029e-07, + "cache_read_input_token_cost": 2.9835e-07, + "output_cost_per_token": 3.083e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/glm-5.3": { + "max_input_tokens": 1310720, + "max_output_tokens": 131072, + "input_cost_per_token": 1.178e-06, + "output_cost_per_token": 3.702e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/glm-5.3-flash": { + "max_input_tokens": 1310720, + "max_output_tokens": 65536, + "input_cost_per_token": 1.72e-07, + "output_cost_per_token": 5.733e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/gpt-5.6-luna": { + "max_input_tokens": 1050000, + "max_output_tokens": 128000, + "input_cost_per_token": 2.944e-07, + "cache_read_input_token_cost": 2.7e-08, + "output_cost_per_token": 1.767e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/gpt-5.6-luna-pro": { + "max_input_tokens": 1050000, + "max_output_tokens": 128000, + "input_cost_per_token": 2.703e-07, + "cache_read_input_token_cost": 2.7e-08, + "output_cost_per_token": 1.621e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/gpt-5.6-sol": { + "max_input_tokens": 1050000, + "max_output_tokens": 128000, + "input_cost_per_token": 5.4e-06, + "cache_read_input_token_cost": 6.75e-07, + "output_cost_per_token": 2.7e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/gpt-5.6-sol-pro": { + "max_input_tokens": 1050000, + "max_output_tokens": 128000, + "input_cost_per_token": 5.4e-06, + "cache_read_input_token_cost": 6.75e-07, + "output_cost_per_token": 2.7e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/gpt-5.6-terra": { + "max_input_tokens": 1050000, + "max_output_tokens": 8192, + "input_cost_per_token": 2.7e-06, + "cache_read_input_token_cost": 2.7e-07, + "output_cost_per_token": 1.62e-05, + "input_cost_per_token_above_272k_tokens": 5.4e-06, + "output_cost_per_token_above_272k_tokens": 2.43e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/gpt-5.6-terra-pro": { + "max_input_tokens": 1050000, + "max_output_tokens": 128000, + "input_cost_per_token": 2.7e-06, + "cache_read_input_token_cost": 2.7e-07, + "output_cost_per_token": 1.62e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/gpt-6-astra": { + "max_input_tokens": 1050000, + "max_output_tokens": 128000, + "input_cost_per_token": 1.35e-05, + "cache_read_input_token_cost": 1.35e-06, + "output_cost_per_token": 6.75e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/gpt-6-luna": { + "max_input_tokens": 1050000, + "max_output_tokens": 128000, + "input_cost_per_token": 1.639e-07, + "cache_read_input_token_cost": 1.35e-08, + "output_cost_per_token": 8.195e-07, + "input_cost_per_token_above_272k_tokens": 2.7e-07, + "output_cost_per_token_above_272k_tokens": 1.0125e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/gpt-6.1-sol": { + "max_input_tokens": 1050000, + "max_output_tokens": 128000, + "input_cost_per_token": 2.7e-06, + "cache_read_input_token_cost": 1.35e-07, + "output_cost_per_token": 1.35e-05, + "input_cost_per_token_above_272k_tokens": 5.4e-06, + "output_cost_per_token_above_272k_tokens": 2.025e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/gpt-oss-120b": { + "max_input_tokens": 128000, + "max_output_tokens": 8192, + "input_cost_per_token": 4.73e-08, + "output_cost_per_token": 2.303e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/grok-4.20": { + "max_input_tokens": 2000000, + "max_output_tokens": 8192, + "input_cost_per_token": 1.763e-06, + "output_cost_per_token": 3.528e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/grok-4.20-multi-agent": { + "max_input_tokens": 2000000, + "max_output_tokens": 8192, + "input_cost_per_token": 1.767e-06, + "output_cost_per_token": 3.534e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": false, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/grok-4.3": { + "max_input_tokens": 1000000, + "max_output_tokens": 32768, + "input_cost_per_token": 1.92e-06, + "cache_read_input_token_cost": 2.7e-07, + "output_cost_per_token": 3.838e-06, + "input_cost_per_token_above_200k_tokens": 3.375e-06, + "output_cost_per_token_above_200k_tokens": 6.75e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/grok-4.5": { + "max_input_tokens": 500000, + "max_output_tokens": 8192, + "input_cost_per_token": 2.73e-06, + "output_cost_per_token": 8.189e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/grok-4.6": { + "max_input_tokens": 500000, + "max_output_tokens": 8192, + "input_cost_per_token": 2.758e-06, + "output_cost_per_token": 8.274e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/grok-4.7": { + "max_input_tokens": 500000, + "max_output_tokens": 450000, + "input_cost_per_token": 2.16e-06, + "cache_read_input_token_cost": 5.4e-07, + "output_cost_per_token": 6.48e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/grok-build": { + "max_input_tokens": 256000, + "max_output_tokens": 32768, + "input_cost_per_token": 1.389e-06, + "cache_read_input_token_cost": 2.7e-07, + "output_cost_per_token": 2.777e-06, + "input_cost_per_token_above_200k_tokens": 2.7e-06, + "output_cost_per_token_above_200k_tokens": 5.4e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/hy3": { + "max_input_tokens": 262144, + "max_output_tokens": 8192, + "input_cost_per_token": 1.594e-07, + "output_cost_per_token": 6.576e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/hy4-preview": { + "max_input_tokens": 1048576, + "max_output_tokens": 64000, + "input_cost_per_token": 1.1259e-06, + "output_cost_per_token": 3.3764e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/kimi-k2.6": { + "max_input_tokens": 262144, + "max_output_tokens": 32768, + "input_cost_per_token": 5.909e-07, + "output_cost_per_token": 2.758e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/kimi-k2.7-code": { + "max_input_tokens": 262144, + "max_output_tokens": 16384, + "input_cost_per_token": 1.009e-06, + "output_cost_per_token": 4.774e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/kimi-k3": { + "max_input_tokens": 1048576, + "max_output_tokens": 32768, + "input_cost_per_token": 3.052e-06, + "cache_read_input_token_cost": 4.05e-07, + "output_cost_per_token": 1.5258e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/kimi-k3.1-pro": { + "max_input_tokens": 1048576, + "max_output_tokens": 943718, + "input_cost_per_token": 2.7e-06, + "cache_read_input_token_cost": 2.7e-07, + "output_cost_per_token": 1.35e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/llama-3.3-70b": { + "max_input_tokens": 128000, + "max_output_tokens": 8192, + "input_cost_per_token": 1.35e-07, + "output_cost_per_token": 4.32e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": false, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/llama-4-maverick": { + "max_input_tokens": 1048576, + "max_output_tokens": 16384, + "input_cost_per_token": 3.344e-07, + "output_cost_per_token": 1.337e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": false, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/mimo-v2.5": { + "max_input_tokens": 1050000, + "max_output_tokens": 16384, + "input_cost_per_token": 1.859e-07, + "output_cost_per_token": 3.718e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/mimo-v2.6-flash": { + "max_input_tokens": 1050000, + "max_output_tokens": 131072, + "input_cost_per_token": 1.89e-07, + "output_cost_per_token": 3.78e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/mimo-v2.6-pro": { + "max_input_tokens": 1050000, + "max_output_tokens": 131072, + "input_cost_per_token": 5.805e-07, + "output_cost_per_token": 1.1745e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/minimax-m2.5": { + "max_input_tokens": 196608, + "max_output_tokens": 32768, + "input_cost_per_token": 4.188e-07, + "output_cost_per_token": 1.675e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/minimax-m2.7": { + "max_input_tokens": 204800, + "max_output_tokens": 32768, + "input_cost_per_token": 4.05e-07, + "output_cost_per_token": 1.62e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/minimax-m3": { + "max_input_tokens": 1048576, + "max_output_tokens": 32768, + "input_cost_per_token": 3.956e-07, + "cache_read_input_token_cost": 7.56e-08, + "output_cost_per_token": 1.582e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/minimax-m3.1-flash": { + "max_input_tokens": 1048576, + "max_output_tokens": 524288, + "input_cost_per_token": 2.025e-07, + "cache_read_input_token_cost": 2.025e-08, + "output_cost_per_token": 8.1e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/muse-glimmer-30b": { + "max_input_tokens": 131072, + "max_output_tokens": 8192, + "input_cost_per_token": 4.554e-07, + "output_cost_per_token": 1.821e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/muse-spark-1.1": { + "max_input_tokens": 1048576, + "max_output_tokens": 8192, + "input_cost_per_token": 1.542e-06, + "output_cost_per_token": 5.243e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/muse-spark-1.2": { + "max_input_tokens": 1048576, + "max_output_tokens": 8192, + "input_cost_per_token": 1.773e-06, + "output_cost_per_token": 6.025e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/muse-spark-1.3": { + "max_input_tokens": 1048576, + "max_output_tokens": 8192, + "input_cost_per_token": 1.688e-06, + "output_cost_per_token": 5.738e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/muse-spark-open": { + "max_input_tokens": 1048576, + "max_output_tokens": 943718, + "input_cost_per_token": 1.688e-06, + "output_cost_per_token": 5.738e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/muse-wonder": { + "max_input_tokens": 1048576, + "max_output_tokens": 943718, + "input_cost_per_token": 5.4e-06, + "output_cost_per_token": 2.7e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/nemotron-3-ultra-550b": { + "max_input_tokens": 1000000, + "max_output_tokens": 32768, + "input_cost_per_token": 6.75e-07, + "cache_read_input_token_cost": 1.35e-07, + "output_cost_per_token": 3.375e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/nemotron-3.5-super-vl": { + "max_input_tokens": 1048576, + "max_output_tokens": 235929, + "input_cost_per_token": 8.1e-08, + "cache_read_input_token_cost": 1.35e-08, + "output_cost_per_token": 4.725e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/qwen-3-32b": { + "max_input_tokens": 32768, + "max_output_tokens": 8192, + "input_cost_per_token": 1.08e-07, + "output_cost_per_token": 3.78e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/qwen-3-coder": { + "max_input_tokens": 131072, + "max_output_tokens": 32768, + "input_cost_per_token": 3.546e-07, + "cache_read_input_token_cost": 1.35e-07, + "output_cost_per_token": 1.612e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": false, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/qwen-3.6-plus": { + "max_input_tokens": 1000000, + "max_output_tokens": 65536, + "input_cost_per_token": 4.388e-07, + "output_cost_per_token": 2.633e-06, + "input_cost_per_token_above_256k_tokens": 1.755e-06, + "output_cost_per_token_above_256k_tokens": 5.265e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/qwen-3.7-max": { + "max_input_tokens": 1000000, + "max_output_tokens": 32768, + "input_cost_per_token": 2.071e-06, + "cache_read_input_token_cost": 1.6875e-07, + "output_cost_per_token": 6.207e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/qwen-3.7-plus": { + "max_input_tokens": 1000000, + "max_output_tokens": 32768, + "input_cost_per_token": 4.325e-07, + "cache_read_input_token_cost": 8.64e-08, + "output_cost_per_token": 1.731e-06, + "input_cost_per_token_above_256k_tokens": 1.296e-06, + "output_cost_per_token_above_256k_tokens": 5.184e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/qwen-3.8-max": { + "max_input_tokens": 1000000, + "max_output_tokens": 131072, + "input_cost_per_token": 2.228e-06, + "cache_read_input_token_cost": 2.781e-07, + "output_cost_per_token": 6.684e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/qwen3.7-flash": { + "max_input_tokens": 1000000, + "max_output_tokens": 8192, + "input_cost_per_token": 4.35e-08, + "cache_read_input_token_cost": 8.1e-09, + "output_cost_per_token": 1.886e-07, + "input_cost_per_token_above_32k_tokens": 1.35e-07, + "output_cost_per_token_above_32k_tokens": 5.4e-07, + "input_cost_per_token_above_256k_tokens": 2.7e-07, + "output_cost_per_token_above_256k_tokens": 1.08e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/qwen3.8-27b": { + "max_input_tokens": 1000000, + "max_output_tokens": 131072, + "input_cost_per_token": 3.826e-07, + "output_cost_per_token": 2.732e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/qwen3.8-flash": { + "max_input_tokens": 1000000, + "max_output_tokens": 131072, + "input_cost_per_token": 2.045e-07, + "output_cost_per_token": 6.41e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/sonar": { + "max_input_tokens": 127072, + "max_output_tokens": 4096, + "input_cost_per_token": 1.35e-06, + "output_cost_per_token": 1.35e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": false, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": false, + "supports_response_schema": false, + "supports_pdf_input": false + }, + "kyma/sonar-pro": { + "max_input_tokens": 200000, + "max_output_tokens": 8000, + "input_cost_per_token": 4.05e-06, + "output_cost_per_token": 2.025e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": false, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": false, + "supports_response_schema": false, + "supports_pdf_input": false + }, + "kyma/step-3.7-flash": { + "max_input_tokens": 256000, + "max_output_tokens": 8192, + "input_cost_per_token": 2.229e-07, + "cache_read_input_token_cost": 5.4e-08, + "output_cost_per_token": 1.282e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/step-5-preview": { + "max_input_tokens": 1048576, + "max_output_tokens": 64000, + "input_cost_per_token": 1.35e-06, + "cache_read_input_token_cost": 1.35e-07, + "output_cost_per_token": 3.645e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, "databricks/databricks-claude-haiku-5-5": { "cache_creation_input_token_cost": 1.2502e-07, "cache_creation_input_token_cost_above_100k_tokens": 6.2503e-07, diff --git a/litellm/provider_endpoints_support_backup.json b/litellm/provider_endpoints_support_backup.json index 7b0234e28d7..1d487f33835 100644 --- a/litellm/provider_endpoints_support_backup.json +++ b/litellm/provider_endpoints_support_backup.json @@ -3724,7 +3724,7 @@ "ocr": false, "search": false, "moderations": false, - "realtime": false, + "realtime": true, "video_generations": false, "batches": false, "messages": false, @@ -4910,6 +4910,33 @@ "vector_stores_search": true } }, + "opensearch": { + "display_name": "Amazon OpenSearch (`opensearch`)", + "url": "https://docs.litellm.ai/docs/providers/opensearch_vector_stores", + "endpoints": { + "chat_completions": false, + "responses": false, + "text_completion": false, + "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": false, + "interactions": false, + "systemone": false, + "a2a": false, + "generateContent": false, + "vector_stores_search": true + } + }, "helicone": { "display_name": "Helicone (`helicone`)", "url": "https://docs.litellm.ai/docs/providers/helicone", @@ -5327,6 +5354,32 @@ "generateContent": false } }, + "kyma": { + "display_name": "Kyma API (`kyma`)", + "url": "https://docs.litellm.ai/docs/providers/kyma", + "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 + } + }, "bigmodel": { "display_name": "BigModel (Zhipu AI) (`bigmodel`)", "url": "https://docs.bigmodel.cn/cn/guide/develop/responses/introduction", diff --git a/litellm/proxy/_types.py b/litellm/proxy/_types.py index dd873ae49dd..4047fa3f0ad 100644 --- a/litellm/proxy/_types.py +++ b/litellm/proxy/_types.py @@ -3748,6 +3748,7 @@ class SpendLogsMetadata(TypedDict): internal_call_origin: InternalCallOrigin | None billed_retry_attempt: ReadOnly[NotRequired[bool | None]] litellm_roi_estimator: ReadOnly[NotRequired[bool | None]] + is_silent_experiment: ReadOnly[NotRequired[bool | None]] guardrail_information: list[StandardLoggingGuardrailInformation] | None eval_information: Any | None status: StandardLoggingPayloadStatus diff --git a/litellm/proxy/anthropic_endpoints/endpoints.py b/litellm/proxy/anthropic_endpoints/endpoints.py index 1d88e133cf9..1a2e610d5c4 100644 --- a/litellm/proxy/anthropic_endpoints/endpoints.py +++ b/litellm/proxy/anthropic_endpoints/endpoints.py @@ -2,7 +2,7 @@ Unified /v1/messages endpoint - (Anthropic Spec) """ -from collections.abc import Mapping +from collections.abc import AsyncGenerator, Mapping from types import MappingProxyType from typing import Final @@ -19,12 +19,10 @@ from litellm.anthropic_interface.exceptions import ( ) from litellm.integrations.custom_guardrail import ModifyResponseException from litellm.litellm_core_utils.tool_fields import first_validation_error +from litellm.llms.anthropic.chat.guardrail_translation.handler import AnthropicMessagesHandler from litellm.llms.anthropic.messages.context_management import ( AnthropicContextManagementError, ) -from litellm.llms.base_llm.guardrail_translation.utils import ( - blocked_response_usage as _blocked_response_usage, -) from litellm.proxy._types import * from litellm.proxy.auth.user_api_key_auth import user_api_key_auth from litellm.proxy.common_request_processing import ( @@ -44,6 +42,7 @@ from litellm.proxy.common_utils.http_parsing_utils import ( # noqa: F401 # leg from litellm.proxy.common_utils.openai_error_payload import ( LITELLM_CALL_ID_HEADER, error_status_code, + litellm_call_id_headers, openai_error_param, openai_error_type, with_litellm_call_id, @@ -58,6 +57,8 @@ from litellm.router import Router router: Final = APIRouter() +_NO_HEADERS: Final[Mapping[str, str]] = MappingProxyType({}) + def _with_provider_specific_fields(exc: ProxyException, detail: AnthropicErrorDetail) -> AnthropicErrorDetail: if not exc.provider_specific_fields: @@ -229,44 +230,22 @@ async def anthropic_response( request_data=_data, ) - # Create Anthropic-formatted response with violation message - import uuid + block_headers: Final = litellm_call_id_headers(base_llm_response_processor.litellm_call_id) or _NO_HEADERS + block_handler: Final = AnthropicMessagesHandler() - from litellm.types.utils import AnthropicMessagesResponse + if data.get("stream") is True: + block_chunks: Final = block_handler.build_block_sse_chunks(e) - # Report the blocked LLM response's real token usage (carried on the - # exception) instead of discarding it; zero for pre-call blocks. - _usage: Final = _blocked_response_usage(e.original_response) - - _anthropic_response: Final = AnthropicMessagesResponse( - id=f"msg_{uuid.uuid4()}", - type="message", - role="assistant", - content=[{"type": "text", "text": e.message}], - model=e.model, - stop_reason="end_turn", - usage=_usage, - ) - - if data.get("stream", None) is not None and data["stream"] is True: - # For streaming, use the standard SSE data generator - async def _passthrough_stream_generator(): - yield _anthropic_response - - selected_data_generator: Final = ProxyBaseLLMRequestProcessing.async_sse_data_generator( - response=_passthrough_stream_generator(), - user_api_key_dict=user_api_key_dict, - request_data=_data, - proxy_logging_obj=proxy_logging_obj, - ) + async def _blocked_stream() -> AsyncGenerator[str, None]: + for chunk in block_chunks: + yield chunk.decode() return await create_response( - generator=selected_data_generator, - media_type="text/event-stream", - headers={}, + generator=_blocked_stream(), media_type="text/event-stream", headers=block_headers ) - return _anthropic_response + fastapi_response.headers.update(block_headers) + return block_handler.build_block_response(e) except AnthropicContextManagementError as e: if e.status_code >= 500: # Server-side polyfill failures hit the failure hook for spend/alert diff --git a/litellm/proxy/batches_endpoints/litellm_executed_batches.py b/litellm/proxy/batches_endpoints/litellm_executed_batches.py index 098dd2f9816..67728e0fd63 100644 --- a/litellm/proxy/batches_endpoints/litellm_executed_batches.py +++ b/litellm/proxy/batches_endpoints/litellm_executed_batches.py @@ -35,7 +35,7 @@ from litellm.repositories.managed_batch_repository import ManagedBatchRepository from litellm.types.llms.base import LiteLLMBaseModel from litellm.types.llms.openai import LiteLLMBatchCreateRequest, OpenAIFileObject, OpenAIFilesPurpose from litellm.types.utils import LITELLM_EXECUTED_BATCH_PROVIDERS, ExtractedFileData, LiteLLMBatch, LlmProviders -from litellm.utils import resolve_api_key_secret_reference +from litellm.utils import aresolve_api_key_secret_reference if TYPE_CHECKING: from prisma import types as prisma_types @@ -227,7 +227,7 @@ async def upstream_lacks_files_api(api_base: str, api_key: str | None, http_clie return response.status_code == httpx.codes.NOT_FOUND -def _upstream_of(credentials: Mapping[str, object], provider: str) -> tuple[str, str | None] | None: +async def _upstream_of(credentials: Mapping[str, object], provider: str) -> tuple[str, str | None] | None: model: Final = credentials.get("model") api_base: Final = credentials.get("api_base") api_key: Final = credentials.get("api_key") @@ -244,7 +244,7 @@ def _upstream_of(credentials: Mapping[str, object], provider: str) -> tuple[str, return None if resolved_api_base is None: return None - return resolved_api_base, resolve_api_key_secret_reference( + return resolved_api_base, await aresolve_api_key_secret_reference( resolved_api_key, model=model, llm_provider=provider, upstream=credentials ) @@ -255,7 +255,7 @@ async def litellm_executed_provider_for( provider: Final = litellm_executed_provider_of(credentials) if provider is None: return None - upstream: Final = _upstream_of(credentials, provider) + upstream: Final = await _upstream_of(credentials, provider) if upstream is None: return None return provider if await lacks_files_api(*upstream) else None diff --git a/litellm/proxy/common_request_processing.py b/litellm/proxy/common_request_processing.py index fc29a6e3a08..7e487ded569 100644 --- a/litellm/proxy/common_request_processing.py +++ b/litellm/proxy/common_request_processing.py @@ -129,6 +129,7 @@ from litellm.proxy.guardrails.auto_router_compression import arm_pre_call as _ar from litellm.proxy.hooks.parallel_request_limiter_v3 import get_or_create_request_stash from litellm.proxy.native_compaction import with_proxy_compaction_executor from litellm.proxy.route_llm_request import ( + endpoint_unsupported_by_model, route_request, ) from litellm.proxy.telemetry.request_context import bind_call_id as bind_telemetry_call_id @@ -871,6 +872,17 @@ _DECODED_JSON: Final = TypeAdapter(object) _ROUTER_SETTINGS_ADAPTER: Final[TypeAdapter[Mapping[str, object] | None]] = TypeAdapter(Mapping[str, object] | None) +def _fallback_probe_data( + pristine: object, fallback_model: str, key_aliases: object, group_alias: object +) -> Mapping[str, object]: + return { + **_REQUEST_FIELDS.validate_python(pristine), + "model": resolve_served_model( + fallback_model, _DECODED_JSON.validate_python(litellm.model_alias_map), key_aliases, group_alias + ), + } + + def _request_model(data: Mapping[str, object]) -> str | list[str] | None: try: return _REQUEST_MODEL.validate_python(data.get("model"), strict=True) @@ -2676,6 +2688,17 @@ class ProxyBaseLLMRequestProcessing: original_model, fallback_models, ) + raw_router_settings: Final = _DECODED_JSON.validate_python( + _REQUEST_FIELDS.validate_python(rate_limited_data).get("router_settings_override") + ) + group_alias: Final = _DECODED_JSON.validate_python( + _REQUEST_FIELDS.validate_python(raw_router_settings).get("model_group_alias") + if isinstance(raw_router_settings, Mapping) + else None + ) + key_aliases: Final[object] = cast( # cast-ok: auth aliases are dynamically typed at runtime + object, user_api_key_dict.aliases + ) try: for fallback_model in fallback_models: if fallback_model == original_model: @@ -2691,6 +2714,13 @@ class ProxyBaseLLMRequestProcessing: self.data = independent_snapshot(pristine) self.data["model"] = fallback_model try: + if endpoint_unsupported_by_model( + route_type, + _fallback_probe_data(pristine, fallback_model, key_aliases, group_alias), + llm_router, + user_api_key_dict.team_id, + ): + continue return await self.common_processing_pre_call_logic( request=request, general_settings=general_settings, @@ -4029,13 +4059,13 @@ class ProxyBaseLLMRequestProcessing: (assembled_response, cache_hit); the closure also runs non-apply_guardrail post-call hooks via _run_deferred_stream_guardrails. - - Every other anthropic_messages/aresponses stream gets a closure - that dispatches on the stored args shape, because the arming site - cannot tell the producers apart: native iterators store a single - ready-made logging coroutine to enqueue, while bridged streams - (LiteLLMCompletionStreamingIterator, and the plain SSE generator - AnthropicStreamWrapper returns for bridged /v1/messages) share - their inner CustomStreamWrapper's logging_obj and so store + - Every other anthropic_messages/aresponses/atext_completion stream + gets a closure that dispatches on the stored args shape, because the + arming site cannot tell the producers apart: native iterators store + a single ready-made logging coroutine to enqueue, while bridged + streams (LiteLLMCompletionStreamingIterator, TextCompletionStreamWrapper, + and the plain SSE generator AnthropicStreamWrapper returns for bridged + /v1/messages) share their inner CustomStreamWrapper's logging_obj and so store (assembled_response, cache_hit); for those the closure only dispatches success logging, matching the route's pre-existing hook surface. @@ -4072,7 +4102,11 @@ class ProxyBaseLLMRequestProcessing: logging_obj._on_deferred_stream_complete = _on_deferred_stream_complete return - if route_type not in ("anthropic_messages", "aresponses") or not self._is_streaming_response(response): + if route_type not in ( + "anthropic_messages", + "aresponses", + "atext_completion", + ) or not self._is_streaming_response(response): return from litellm.litellm_core_utils.logging_worker import GLOBAL_LOGGING_WORKER diff --git a/litellm/proxy/common_utils/callback_utils.py b/litellm/proxy/common_utils/callback_utils.py index f889748c504..d1787f3ff8b 100644 --- a/litellm/proxy/common_utils/callback_utils.py +++ b/litellm/proxy/common_utils/callback_utils.py @@ -436,10 +436,7 @@ def get_model_group_from_litellm_kwargs(kwargs: dict) -> str | None: def get_model_group_from_request_data(data: dict) -> str | None: _metadata: Final = data.get("metadata", None) or {} _model_group: Final = _metadata.get("model_group", None) - if _model_group is not None: - return _model_group - - return None + return _model_group if isinstance(_model_group, str) else None def get_remaining_tokens_and_requests_from_request_data(data: dict) -> dict[str, str]: diff --git a/litellm/proxy/db/db_spend_update_writer.py b/litellm/proxy/db/db_spend_update_writer.py index 8ac2597c99b..16dbe2b4131 100644 --- a/litellm/proxy/db/db_spend_update_writer.py +++ b/litellm/proxy/db/db_spend_update_writer.py @@ -1900,8 +1900,6 @@ class DBSpendUpdateWriter: proxy_logging_obj=proxy_logging_obj, ) - # NOTE: Daily tag spend is committed by a separate scheduler job. - ################## Daily End-User Spend Update Transactions ################## # Aggregate all in memory daily end-user spend transactions and commit to db await self._flush_daily_spend_queue( diff --git a/litellm/proxy/db/prisma_client.py b/litellm/proxy/db/prisma_client.py index 3d783d8aba1..be5de040fdf 100644 --- a/litellm/proxy/db/prisma_client.py +++ b/litellm/proxy/db/prisma_client.py @@ -1127,16 +1127,16 @@ class PrismaManager: return ProxyExtrasDBManager.build_request_log_indexes() @staticmethod - def migration_data_check_report(use_migrate: bool = True) -> str | None: - """The read-only pre-migration row check on its own, for `--check_migrations_against_existing_data`: the - refusal the migration step would print, or None when the pending schema changes - can be applied to the rows the database has.""" + def migration_dry_run_summary(use_migrate: bool = True) -> tuple[bool, str]: + """`--check_migrations_against_existing_data` on its own: the pending migrations applied inside a + transaction that is rolled back, as (whether they all applied, what the operator reads).""" from litellm_proxy_extras.utils import ( ProxyExtrasDBManager, # pyright: ignore[reportUnknownVariableType] # litellm-proxy-extras is an optional install ) + from pydantic import TypeAdapter - refusal: Final[object] = ProxyExtrasDBManager.migration_data_check_report(use_migrate=use_migrate) # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType] # litellm-proxy-extras is an optional install - return refusal if isinstance(refusal, str) else None + summary: Final[object] = ProxyExtrasDBManager.migration_dry_run_summary(use_migrate=use_migrate) # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType] # litellm-proxy-extras is an optional install + return TypeAdapter(tuple[bool, str]).validate_python(summary) @staticmethod def start_request_log_index_build() -> None: diff --git a/litellm/proxy/guardrails/guardrail_hooks/grayswan/grayswan.py b/litellm/proxy/guardrails/guardrail_hooks/grayswan/grayswan.py index 7ff5aa2466b..e57d6e78378 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/grayswan/grayswan.py +++ b/litellm/proxy/guardrails/guardrail_hooks/grayswan/grayswan.py @@ -82,6 +82,22 @@ def _as_plain_dict(item: object) -> Mapping[str, object]: return cast("Mapping[str, object]", item) # cast-ok: wire rows are message/tool-call dicts +_STR_KEYED_MAPPING: Final = TypeAdapter(Mapping[str, object]) + + +def _str_keyed(value: object) -> Mapping[str, object]: + return _STR_KEYED_MAPPING.validate_python(value) if isinstance(value, Mapping) else {} + + +def _request_route(request_data: Mapping[str, object]) -> str | None: + litellm_metadata: Final = request_data.get("litellm_metadata") + metadata_bucket: Final = _str_keyed( + litellm_metadata if litellm_metadata is not None else request_data.get("metadata") + ) + route: Final = metadata_bucket.get("user_api_key_request_route") + return route if isinstance(route, str) else None + + class GraySwanGuardrailMissingSecrets(Exception): """Raised when the Gray Swan API key is missing.""" @@ -625,11 +641,8 @@ class GraySwanGuardrail(CustomGuardrail): from litellm.litellm_core_utils.api_route_to_call_types import get_call_types_for_route from litellm.llms import load_guardrail_translation_mappings - litellm_metadata: Final = request_data.get("litellm_metadata") - request_route: Final = ( - litellm_metadata.get("user_api_key_request_route") if isinstance(litellm_metadata, Mapping) else None - ) - route_call_types: Final = get_call_types_for_route(request_route) if isinstance(request_route, str) else None + request_route: Final = _request_route(request_data) + route_call_types: Final = get_call_types_for_route(request_route) if request_route is not None else None call_type: Final = ( (route_call_types[0].value if route_call_types else None) or (logging_obj.call_type if logging_obj is not None else None) @@ -698,13 +711,12 @@ class GraySwanGuardrail(CustomGuardrail): inbound_headers: Final = self._extract_inbound_headers(request_data, logging_obj) - litellm_metadata: Final = request_data.get("litellm_metadata") - cleaned_litellm_metadata: Final = dict(litellm_metadata) if isinstance(litellm_metadata, dict) else {} - if inbound_headers: - existing_headers: Final = cleaned_litellm_metadata.get("headers") - cleaned_litellm_metadata["headers"] = ( - {**existing_headers, **inbound_headers} if isinstance(existing_headers, dict) else inbound_headers - ) + litellm_metadata: Final = _str_keyed(request_data.get("litellm_metadata")) + cleaned_litellm_metadata: Final = ( + {**litellm_metadata, "headers": {**_str_keyed(litellm_metadata.get("headers")), **inbound_headers}} + if inbound_headers + else litellm_metadata + ) if cleaned_litellm_metadata: sanitized: Final = _DECODED_JSON.validate_python( safe_json_loads(safe_dumps(cleaned_litellm_metadata), default={}) diff --git a/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/content_filter.py b/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/content_filter.py index f58c8705815..71b8976bebf 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/content_filter.py +++ b/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/content_filter.py @@ -12,11 +12,11 @@ import json import os import re import time -from collections.abc import AsyncGenerator, Coroutine, Mapping, Sequence +from collections.abc import AsyncGenerator, AsyncIterable, Callable, Coroutine, Mapping, Sequence from dataclasses import dataclass, replace from datetime import datetime from re import Pattern -from typing import TYPE_CHECKING, Any, Final, Literal, Optional, TypedDict, cast +from typing import TYPE_CHECKING, Any, Final, Literal, Optional, TypedDict, TypeVar, cast import yaml from fastapi import HTTPException @@ -40,6 +40,7 @@ from litellm.proxy.guardrails.content_filter_data import ( ) from litellm.types.utils import ( CallTypes, + Delta, Function, GenericGuardrailAPIInputs, GuardrailStatus, @@ -47,6 +48,9 @@ from litellm.types.utils import ( Message, ModelResponse, ModelResponseStream, + StreamingChoices, + TextChoices, + TextCompletionResponse, ) if TYPE_CHECKING: @@ -78,6 +82,7 @@ from .patterns import PATTERN_EXTRA_CONFIG, get_compiled_pattern MAX_KEYWORD_VALUE_GAP_WORDS: Final = 1 _DECODED_ARGUMENTS: Final = TypeAdapter(object) +_StreamItemT: Final = TypeVar("_StreamItemT") _JSON_ENTRIES: Final = TypeAdapter(list[object]) GAP_WORD_TOKENIZER: Final = re.compile(r"\b\w+\b") SENTENCE_TERMINATORS: Final = re.compile(r"[.!?]+") @@ -139,6 +144,26 @@ class _CategoryConfigView(TypedDict): category_file: str | None +def _streamed_choice_content(choice: StreamingChoices | TextChoices) -> str | None: + if isinstance(choice, TextChoices): + return choice.text if isinstance(choice.text, str) else "" + delta: Final = getattr(choice, "delta", None) + if not delta: + return None + content: Final = getattr(delta, "content", None) + return content if isinstance(content, str) else "" + + +def _streamed_choices(item: object) -> Sequence[StreamingChoices | TextChoices]: + if isinstance(item, (ModelResponseStream, TextCompletionResponse)): + return item.choices + return () + + +def _is_choiceless_chunk(item: object) -> bool: + return isinstance(item, (ModelResponseStream, TextCompletionResponse)) and not item.choices + + @dataclass(frozen=True, slots=True) class _StreamedChoiceState: buffered_text: str = "" @@ -146,6 +171,52 @@ class _StreamedChoiceState: committed_detections: tuple[ContentFilterDetection, ...] = () latest_detections: tuple[ContentFilterDetection, ...] = () next_trim_len: int = 0 + finished: bool = False + + +def _end_of_stream_chunk( + last_item: object, state_by_choice: Mapping[int, _StreamedChoiceState] +) -> ModelResponseStream | TextCompletionResponse | None: + unfinished: Final = tuple(index for index, state in state_by_choice.items() if not state.finished) + if not unfinished: + return None + if isinstance(last_item, TextCompletionResponse): + return TextCompletionResponse( + id=last_item.id, + created=last_item.created, + model=last_item.model, + object="text_completion", + choices=[TextChoices(index=index, text="") for index in unfinished], + ).model_copy(update={"usage": None}) + if isinstance(last_item, ModelResponseStream): + return ModelResponseStream( + id=last_item.id, + created=last_item.created, + model=last_item.model, + choices=[StreamingChoices(index=index, delta=Delta(content="")) for index in unfinished], + ) + return None + + +async def _stream_then_end_of_stream_chunk( + response: AsyncIterable[_StreamItemT], end_of_stream_chunk: Callable[[_StreamItemT], _StreamItemT | None] +) -> AsyncGenerator[tuple[_StreamItemT, bool], None]: + last_item: _StreamItemT | None = None # rebind-ok: the end-of-stream chunk copies the last chunk's id and model + held_items: tuple[_StreamItemT, ...] = () # rebind-ok: the usage chunk must stay after the end-of-stream chunk + async for item in response: + last_item = item + if _is_choiceless_chunk(item): + held_items = (*held_items, item) + continue + for held_item in held_items: + yield held_item, False + held_items = () + yield item, False + closing_item: Final = None if last_item is None else end_of_stream_chunk(last_item) + if closing_item is not None: + yield closing_item, True + for held_item in held_items: + yield held_item, False @dataclass(frozen=True, slots=True) @@ -2104,72 +2175,75 @@ class ContentFilterGuardrail(CustomGuardrail): ) try: - async for item in response: - if isinstance(item, ModelResponseStream) and item.choices: - for choice in item.choices: - if not (hasattr(choice, "delta") and choice.delta): - continue + async for item, stream_ended in _stream_then_end_of_stream_chunk( + response, lambda last_item: _end_of_stream_chunk(last_item, state_by_choice) + ): + for choice in _streamed_choices(item): + new_content = _streamed_choice_content(choice) + if new_content is None: + continue - choice_index = getattr(choice, "index", 0) - if not isinstance(choice_index, int): - choice_index = 0 + choice_index = getattr(choice, "index", 0) + if not isinstance(choice_index, int): + choice_index = 0 - content = getattr(choice.delta, "content", None) - is_final = bool(getattr(choice, "finish_reason", None)) - new_content = content if isinstance(content, str) else "" - if not new_content and not is_final: - continue + is_final = stream_ended or bool(getattr(choice, "finish_reason", None)) + if not new_content and not is_final: + continue - previous_state = state_by_choice.get(choice_index, _StreamedChoiceState()) - buffered_text = previous_state.buffered_text + new_content - if not buffered_text: - continue + previous_state = state_by_choice.get(choice_index, _StreamedChoiceState()) + buffered_text = previous_state.buffered_text + new_content + if not buffered_text: + continue - # Add a space at the end if it's the final chunk to trigger word boundaries (\b) - text_to_scan = buffered_text + (" " if is_final else "") - choice_detections: list[ContentFilterDetection] = [] - scan_started = time.perf_counter() + # Add a space at the end if it's the final chunk to trigger word boundaries (\b) + text_to_scan = buffered_text + (" " if is_final else "") + choice_detections: list[ContentFilterDetection] = [] + scan_started = time.perf_counter() - try: - masked_text = self._filter_single_text(text_to_scan, detections=choice_detections) - if is_final and masked_text.endswith(" "): - masked_text = masked_text[:-1] - latest_detections = tuple(choice_detections) - except HTTPException: - state_by_choice[choice_index] = replace( - previous_state, latest_detections=tuple(choice_detections) - ) - raise - except Exception as e: - verbose_proxy_logger.error("ContentFilterGuardrail: Error in masking: %s", e) - masked_text = text_to_scan # Fallback to current text - latest_detections = previous_state.latest_detections - finally: - scan_seconds += time.perf_counter() - scan_started - - safe_to_yield_len = max( - previous_state.yielded_masked_text_len, - len(masked_text) - (0 if is_final else CONTENT_FILTER_STREAMING_HOLDBACK_CHARS), + try: + masked_text = self._filter_single_text(text_to_scan, detections=choice_detections) + if is_final and masked_text.endswith(" "): + masked_text = masked_text[:-1] + latest_detections = tuple(choice_detections) + except HTTPException: + state_by_choice[choice_index] = replace( + previous_state, latest_detections=tuple(choice_detections) ) - choice.delta.content = masked_text[previous_state.yielded_masked_text_len : safe_to_yield_len] - next_state = replace( - previous_state, - buffered_text=buffered_text, - yielded_masked_text_len=safe_to_yield_len, - latest_detections=latest_detections, - ) - if is_final: - state_by_choice[choice_index] = next_state - continue + raise + except Exception as e: + verbose_proxy_logger.error("ContentFilterGuardrail: Error in masking: %s", e) + masked_text = text_to_scan # Fallback to current text + latest_detections = previous_state.latest_detections + finally: + scan_seconds += time.perf_counter() - scan_started - trim_started = time.perf_counter() - state_by_choice[choice_index] = self._trim_streamed_choice_buffer(next_state, masked_text, plan) - scan_seconds += time.perf_counter() - trim_started + safe_to_yield_len = max( + previous_state.yielded_masked_text_len, + len(masked_text) - (0 if is_final else CONTENT_FILTER_STREAMING_HOLDBACK_CHARS), + ) + released_text = masked_text[previous_state.yielded_masked_text_len : safe_to_yield_len] + if isinstance(choice, TextChoices): + choice.text = released_text + else: + choice.delta.content = released_text + next_state = replace( + previous_state, + buffered_text=buffered_text, + yielded_masked_text_len=safe_to_yield_len, + latest_detections=latest_detections, + ) + if is_final: + state_by_choice[choice_index] = replace(next_state, finished=True) + continue - yield item - else: - # Not a ModelResponseStream or no choices - yield as is - yield item + trim_started = time.perf_counter() + state_by_choice[choice_index] = self._trim_streamed_choice_buffer(next_state, masked_text, plan) + scan_seconds += time.perf_counter() - trim_started + + if stream_ended and not any(_streamed_choice_content(choice) for choice in _streamed_choices(item)): + continue + yield item except HTTPException: status = "guardrail_intervened" raise diff --git a/litellm/proxy/guardrails/guardrail_hooks/prompt_security/prompt_security.py b/litellm/proxy/guardrails/guardrail_hooks/prompt_security/prompt_security.py index 0f66135d038..8e2fea415ef 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/prompt_security/prompt_security.py +++ b/litellm/proxy/guardrails/guardrail_hooks/prompt_security/prompt_security.py @@ -647,9 +647,10 @@ class PromptSecurityGuardrail(CustomGuardrail): async def _process_document_item(self, item: dict, user_api_key_alias: str | None) -> dict: """Process and sanitize document/file items.""" doc_data: Final = item.get("document") or item.get("file") or item + url_key: Final = "file_data" if isinstance(doc_data, dict) and not doc_data.get("url") else "url" if isinstance(doc_data, dict): - url = doc_data.get("url", "") + url = doc_data.get(url_key) or "" doc_content = doc_data.get("data", "") else: url = doc_data if isinstance(doc_data, str) else "" @@ -698,7 +699,7 @@ class PromptSecurityGuardrail(CustomGuardrail): if url.startswith("data:") and header: sanitized_url: Final = f"{header},{sanitized_encoded}" if isinstance(doc_data, dict): - doc_data["url"] = sanitized_url + doc_data[url_key] = sanitized_url elif isinstance(doc_data, dict): doc_data["data"] = sanitized_encoded diff --git a/litellm/proxy/health_check_utils/shared_health_check_manager.py b/litellm/proxy/health_check_utils/shared_health_check_manager.py index 62c973196b9..d689ab2a537 100644 --- a/litellm/proxy/health_check_utils/shared_health_check_manager.py +++ b/litellm/proxy/health_check_utils/shared_health_check_manager.py @@ -2,7 +2,10 @@ import asyncio import json import time from collections.abc import Mapping, Sequence -from typing import TYPE_CHECKING, Any, Final +from typing import TYPE_CHECKING, Final + +from pydantic import ConfigDict, TypeAdapter, with_config +from typing_extensions import NotRequired, ReadOnly, TypedDict from litellm._internal_context import with_service_target from litellm._logging import verbose_proxy_logger @@ -19,6 +22,17 @@ if TYPE_CHECKING: from litellm.router import Router +@with_config(ConfigDict(extra="allow", strict=True)) +class _CachedHealthCheckResults(TypedDict): + healthy_endpoints: NotRequired[ReadOnly[list[Mapping[str, object]]]] + unhealthy_endpoints: NotRequired[ReadOnly[list[Mapping[str, object]]]] + timestamp: NotRequired[ReadOnly[int | float]] + checked_by: NotRequired[ReadOnly[object]] + + +_CACHED_HEALTH_CHECK_RESULTS: Final = TypeAdapter(_CachedHealthCheckResults) + + class SharedHealthCheckManager: """ Manager for coordinating health checks across multiple pods using Redis. @@ -109,7 +123,7 @@ class SharedHealthCheckManager: verbose_proxy_logger.error("Error releasing health check lock: %s", str(e)) @with_service_target(HEALTH_CHECKS_TARGET) - async def get_cached_health_check_results(self) -> dict[str, Any] | None: + async def get_cached_health_check_results(self) -> _CachedHealthCheckResults | None: """ Get cached health check results from Redis. @@ -127,10 +141,9 @@ class SharedHealthCheckManager: return None # Parse the cached data - if isinstance(cached_data, str): - cached_results = json.loads(cached_data) - else: - cached_results = cached_data + cached_results: Final = _CACHED_HEALTH_CHECK_RESULTS.validate_python( + json.loads(cached_data) if isinstance(cached_data, str) else cached_data + ) # Check if the cache is still valid cache_timestamp: Final = cached_results.get("timestamp", 0) diff --git a/litellm/proxy/management_endpoints/auto_router_endpoints.py b/litellm/proxy/management_endpoints/auto_router_endpoints.py index 3dceab6b7f7..b79154bf509 100644 --- a/litellm/proxy/management_endpoints/auto_router_endpoints.py +++ b/litellm/proxy/management_endpoints/auto_router_endpoints.py @@ -865,7 +865,7 @@ async def _recorded_autorouter_savings( entity_id_field="user_id", entity_id=user_id, exclude_entity_ids=None, - api_key=api_key, + api_key=(api_key,) if api_key is not None else None, start_date=start_day, end_date=end_day, model=None, diff --git a/litellm/proxy/management_endpoints/common_daily_activity.py b/litellm/proxy/management_endpoints/common_daily_activity.py index 66f386b792d..98372b64015 100644 --- a/litellm/proxy/management_endpoints/common_daily_activity.py +++ b/litellm/proxy/management_endpoints/common_daily_activity.py @@ -23,6 +23,12 @@ from litellm.proxy.spend_tracking.key_metadata_recovery import ( ) from litellm.proxy.spend_tracking.ptu_feature_flag import is_ptu_cost_attribution_enabled from litellm.proxy.utils import PrismaClient +from litellm.repositories.daily_activity_buckets import ( + api_key_bucket, + model_bucket, + model_group_bucket, + raw_api_key_filter, +) from litellm.repositories.daily_activity_repository import DailyActivityRepository from litellm.types.proxy.management_endpoints.common_daily_activity import ( BreakdownMetrics, @@ -182,7 +188,9 @@ class _AggregatedSpendData(TypedDict): totals: ReadOnly[SpendMetrics] -def _key_metadata(api_key_metadata: Mapping[str, KeyMetadataRow], api_key: str) -> KeyMetadata: +def _key_metadata(api_key_metadata: Mapping[str, KeyMetadataRow], api_key: str | None) -> KeyMetadata: + if not api_key: + return KeyMetadata(key_alias="Unattributed", key_exists=False) meta: Final = api_key_metadata.get(api_key) return KeyMetadata( key_alias=meta.key_alias if meta is not None else None, @@ -302,13 +310,14 @@ def update_breakdown_metrics( parent bucket but never appear as an api_key row, and are kept out of the per-request provider breakdown.""" + api_key: Final = api_key_bucket(record.api_key) is_ptu_sentinel: Final = record.api_key == PTU_SENTINEL_API_KEY # A PTU sentinel row keys on the deployment id so a rename cannot move it, and carries # the operator-facing name in model_group. The breakdown key is rendered directly as a # label, so display the name; two deployments sharing one name merge here, which is # what the write path used to do by collapsing them into a single row. - model_key: Final = (record.model_group or record.model) if is_ptu_sentinel else record.model + model_key: Final = (record.model_group or record.model) if is_ptu_sentinel else model_bucket(record.model) # Update model breakdown if model_key and model_key not in breakdown.models: @@ -321,18 +330,18 @@ def update_breakdown_metrics( if not is_ptu_sentinel: # Update API key breakdown for this model - if record.api_key not in breakdown.models[model_key].api_key_breakdown: - breakdown.models[model_key].api_key_breakdown[record.api_key] = KeyMetricWithMetadata( + if api_key not in breakdown.models[model_key].api_key_breakdown: + breakdown.models[model_key].api_key_breakdown[api_key] = KeyMetricWithMetadata( metrics=SpendMetrics(), metadata=_key_metadata(api_key_metadata, record.api_key), ) - breakdown.models[model_key].api_key_breakdown[record.api_key].metrics = update_metrics( - breakdown.models[model_key].api_key_breakdown[record.api_key].metrics, + breakdown.models[model_key].api_key_breakdown[api_key].metrics = update_metrics( + breakdown.models[model_key].api_key_breakdown[api_key].metrics, record, ) # Update model group breakdown - model_group_key: Final = record.model_group or record.model + model_group_key: Final = model_group_bucket(record.model_group, record.model) if model_group_key and model_group_key not in breakdown.model_groups: breakdown.model_groups[model_group_key] = MetricWithMetadata( metrics=SpendMetrics(), @@ -345,13 +354,13 @@ def update_breakdown_metrics( if not is_ptu_sentinel: # Update API key breakdown for this model - if record.api_key not in breakdown.model_groups[model_group_key].api_key_breakdown: - breakdown.model_groups[model_group_key].api_key_breakdown[record.api_key] = KeyMetricWithMetadata( + if api_key not in breakdown.model_groups[model_group_key].api_key_breakdown: + breakdown.model_groups[model_group_key].api_key_breakdown[api_key] = KeyMetricWithMetadata( metrics=SpendMetrics(), metadata=_key_metadata(api_key_metadata, record.api_key), ) - breakdown.model_groups[model_group_key].api_key_breakdown[record.api_key].metrics = update_metrics( - breakdown.model_groups[model_group_key].api_key_breakdown[record.api_key].metrics, + breakdown.model_groups[model_group_key].api_key_breakdown[api_key].metrics = update_metrics( + breakdown.model_groups[model_group_key].api_key_breakdown[api_key].metrics, record, ) @@ -365,21 +374,20 @@ def update_breakdown_metrics( breakdown.mcp_servers[record.mcp_namespaced_tool_name].metrics, record ) - # Update API key breakdown for this MCP server - if record.api_key not in breakdown.mcp_servers[record.mcp_namespaced_tool_name].api_key_breakdown: - breakdown.mcp_servers[record.mcp_namespaced_tool_name].api_key_breakdown[record.api_key] = ( - KeyMetricWithMetadata( - metrics=SpendMetrics(), - metadata=_key_metadata(api_key_metadata, record.api_key), + if not is_ptu_sentinel: + # Update API key breakdown for this MCP server + if api_key not in breakdown.mcp_servers[record.mcp_namespaced_tool_name].api_key_breakdown: + breakdown.mcp_servers[record.mcp_namespaced_tool_name].api_key_breakdown[api_key] = ( + KeyMetricWithMetadata( + metrics=SpendMetrics(), + metadata=_key_metadata(api_key_metadata, record.api_key), + ) ) - ) - breakdown.mcp_servers[record.mcp_namespaced_tool_name].api_key_breakdown[ - record.api_key - ].metrics = update_metrics( - breakdown.mcp_servers[record.mcp_namespaced_tool_name].api_key_breakdown[record.api_key].metrics, - record, - ) + breakdown.mcp_servers[record.mcp_namespaced_tool_name].api_key_breakdown[api_key].metrics = update_metrics( + breakdown.mcp_servers[record.mcp_namespaced_tool_name].api_key_breakdown[api_key].metrics, + record, + ) if not is_ptu_sentinel: # Update provider breakdown @@ -392,13 +400,13 @@ def update_breakdown_metrics( breakdown.providers[provider].metrics = update_metrics(breakdown.providers[provider].metrics, record) # Update API key breakdown for this provider - if record.api_key not in breakdown.providers[provider].api_key_breakdown: - breakdown.providers[provider].api_key_breakdown[record.api_key] = KeyMetricWithMetadata( + if api_key not in breakdown.providers[provider].api_key_breakdown: + breakdown.providers[provider].api_key_breakdown[api_key] = KeyMetricWithMetadata( metrics=SpendMetrics(), metadata=_key_metadata(api_key_metadata, record.api_key), ) - breakdown.providers[provider].api_key_breakdown[record.api_key].metrics = update_metrics( - breakdown.providers[provider].api_key_breakdown[record.api_key].metrics, + breakdown.providers[provider].api_key_breakdown[api_key].metrics = update_metrics( + breakdown.providers[provider].api_key_breakdown[api_key].metrics, record, ) @@ -413,25 +421,26 @@ def update_breakdown_metrics( breakdown.endpoints[record.endpoint].metrics, record ) - # Update API key breakdown for this endpoint - if record.api_key not in breakdown.endpoints[record.endpoint].api_key_breakdown: - breakdown.endpoints[record.endpoint].api_key_breakdown[record.api_key] = KeyMetricWithMetadata( - metrics=SpendMetrics(), - metadata=_key_metadata(api_key_metadata, record.api_key), + if not is_ptu_sentinel: + # Update API key breakdown for this endpoint + if api_key not in breakdown.endpoints[record.endpoint].api_key_breakdown: + breakdown.endpoints[record.endpoint].api_key_breakdown[api_key] = KeyMetricWithMetadata( + metrics=SpendMetrics(), + metadata=_key_metadata(api_key_metadata, record.api_key), + ) + breakdown.endpoints[record.endpoint].api_key_breakdown[api_key].metrics = update_metrics( + breakdown.endpoints[record.endpoint].api_key_breakdown[api_key].metrics, + record, ) - breakdown.endpoints[record.endpoint].api_key_breakdown[record.api_key].metrics = update_metrics( - breakdown.endpoints[record.endpoint].api_key_breakdown[record.api_key].metrics, - record, - ) if not is_ptu_sentinel: # Update api key breakdown - if record.api_key not in breakdown.api_keys: - breakdown.api_keys[record.api_key] = KeyMetricWithMetadata( + if api_key not in breakdown.api_keys: + breakdown.api_keys[api_key] = KeyMetricWithMetadata( metrics=SpendMetrics(), metadata=_key_metadata(api_key_metadata, record.api_key), ) - breakdown.api_keys[record.api_key].metrics = update_metrics(breakdown.api_keys[record.api_key].metrics, record) + breakdown.api_keys[api_key].metrics = update_metrics(breakdown.api_keys[api_key].metrics, record) # Update entity-specific metrics if entity_id_field is provided if entity_id_field: @@ -445,13 +454,13 @@ def update_breakdown_metrics( if not is_ptu_sentinel: # Update API key breakdown for this entity - if record.api_key not in breakdown.entities[entity_value].api_key_breakdown: - breakdown.entities[entity_value].api_key_breakdown[record.api_key] = KeyMetricWithMetadata( + if api_key not in breakdown.entities[entity_value].api_key_breakdown: + breakdown.entities[entity_value].api_key_breakdown[api_key] = KeyMetricWithMetadata( metrics=SpendMetrics(), metadata=_key_metadata(api_key_metadata, record.api_key), ) - breakdown.entities[entity_value].api_key_breakdown[record.api_key].metrics = update_metrics( - breakdown.entities[entity_value].api_key_breakdown[record.api_key].metrics, + breakdown.entities[entity_value].api_key_breakdown[api_key].metrics = update_metrics( + breakdown.entities[entity_value].api_key_breakdown[api_key].metrics, record, ) @@ -566,7 +575,7 @@ def daily_activity_scope( entity_id_field: str, entity_id: str | list[str] | None, exclude_entity_ids: list[str] | None, - api_key: str | list[str] | None, + api_key: str | Sequence[str] | None, start_date: str, end_date: str, model: str | None, @@ -577,9 +586,7 @@ def daily_activity_scope( entity_ids: tuple[str, ...] | None = ( (entity_id,) if isinstance(entity_id, str) else tuple(entity_id) if entity_id is not None else None ) - api_keys: tuple[str, ...] | None = ( - None if api_key in (None, "") else (api_key,) if isinstance(api_key, str) else tuple(api_key) - ) + api_keys: Final = raw_api_key_filter(api_key) return DailyActivityScope( table=table_value, entity_id_field=entity_id_field, @@ -715,6 +722,29 @@ _GROUP_DATE_ENDPOINT: Final = 62 # 0b0111110 _GROUP_DATE_ENDPOINT_API_KEY: Final = 30 # 0b0011110 +def sum_spend_metrics(metrics: Sequence[SpendMetrics]) -> SpendMetrics: + return SpendMetrics( + spend=sum(metric.spend for metric in metrics), + flat_cost=sum(metric.flat_cost for metric in metrics), + ptu_hours=sum(metric.ptu_hours for metric in metrics), + prompt_tokens=sum(metric.prompt_tokens for metric in metrics), + completion_tokens=sum(metric.completion_tokens for metric in metrics), + cache_read_input_tokens=sum(metric.cache_read_input_tokens for metric in metrics), + cache_creation_input_tokens=sum(metric.cache_creation_input_tokens for metric in metrics), + compression_saved_tokens=sum(metric.compression_saved_tokens for metric in metrics), + compression_savings_spend=sum(metric.compression_savings_spend for metric in metrics), + prompt_caching_savings_spend=sum(metric.prompt_caching_savings_spend for metric in metrics), + gateway_injected_caching_savings_spend=sum(metric.gateway_injected_caching_savings_spend for metric in metrics), + autorouter_savings_spend=sum(metric.autorouter_savings_spend for metric in metrics), + total_tokens=sum(metric.total_tokens for metric in metrics), + successful_requests=sum(metric.successful_requests for metric in metrics), + failed_requests=sum(metric.failed_requests for metric in metrics), + api_requests=sum(metric.api_requests for metric in metrics), + total_response_time_ms=sum(metric.total_response_time_ms for metric in metrics), + timed_requests=sum(metric.timed_requests for metric in metrics), + ) + + def _record_to_spend_metrics(record: RollupMetricsRow) -> SpendMetrics: """Build a SpendMetrics directly from one already-aggregated rollup row. @@ -753,8 +783,7 @@ def _aggregate_grouping_sets_records_sync( Each row carries a `group_level` bitmask (from Postgres GROUPING()) that identifies which rollup level it belongs to. We dispatch the row's - pre-aggregated metrics straight into the matching bucket — no per-row - summing in Python and no nested update_metrics calls. + pre-aggregated metrics into the matching bucket, combining normalized names. """ total_metrics = SpendMetrics() grouped_data: Final[dict[str, GroupedData]] = {} @@ -771,20 +800,23 @@ def _aggregate_grouping_sets_records_sync( if existing is None: target[key] = MetricWithMetadata(metrics=metrics, metadata={}) else: - existing.metrics = metrics + existing.metrics = sum_spend_metrics((existing.metrics, metrics)) def assign_api_key_breakdown( target: dict[str, MetricWithMetadata], parent_key: str, - api_key: str, + api_key: str | None, metrics: SpendMetrics, ) -> None: parent = target.get(parent_key) if parent is None: parent = MetricWithMetadata(metrics=SpendMetrics(), metadata={}) target[parent_key] = parent - parent.api_key_breakdown[api_key] = KeyMetricWithMetadata( - metrics=metrics, metadata=_key_metadata(api_key_metadata, api_key) + bucket_key: Final = api_key_bucket(api_key) + previous: Final = parent.api_key_breakdown.get(bucket_key) + parent.api_key_breakdown[bucket_key] = KeyMetricWithMetadata( + metrics=sum_spend_metrics((previous.metrics, metrics)) if previous is not None else metrics, + metadata=_key_metadata(api_key_metadata, api_key), ) for record in records: @@ -803,25 +835,23 @@ def _aggregate_grouping_sets_records_sync( breakdown = ensure_date(record.date)["breakdown"] if level == _GROUP_DATE_API_KEY: - if record.api_key and not is_ptu_sentinel: - breakdown.api_keys[record.api_key] = KeyMetricWithMetadata( + if not is_ptu_sentinel: + breakdown.api_keys[api_key_bucket(record.api_key)] = KeyMetricWithMetadata( metrics=metrics, metadata=_key_metadata(api_key_metadata, record.api_key), ) elif level == _GROUP_DATE_MODEL: - if record.model: - assign_metric_with_metadata(breakdown.models, record.model, metrics) + assign_metric_with_metadata(breakdown.models, model_bucket(record.model), metrics) elif level == _GROUP_DATE_MODEL_API_KEY: - if record.model and record.api_key and not is_ptu_sentinel: - assign_api_key_breakdown(breakdown.models, record.model, record.api_key, metrics) + if not is_ptu_sentinel: + assign_api_key_breakdown(breakdown.models, model_bucket(record.model), record.api_key, metrics) elif level == _GROUP_DATE_MODEL_GROUP: - if record.model_group: - assign_metric_with_metadata(breakdown.model_groups, record.model_group, metrics) + assign_metric_with_metadata(breakdown.model_groups, model_group_bucket(record.model_group, None), metrics) elif level == _GROUP_DATE_MODEL_GROUP_API_KEY: - if record.model_group and record.api_key and not is_ptu_sentinel: + if not is_ptu_sentinel: assign_api_key_breakdown( breakdown.model_groups, - record.model_group, + model_group_bucket(record.model_group, None), record.api_key, metrics, ) @@ -836,14 +866,14 @@ def _aggregate_grouping_sets_records_sync( provider = record.custom_llm_provider or "unknown" assign_metric_with_metadata(breakdown.providers, provider, provider_metrics) elif level == _GROUP_DATE_PROVIDER_API_KEY: - if record.api_key and not is_ptu_sentinel: + if not is_ptu_sentinel: provider = record.custom_llm_provider or "unknown" assign_api_key_breakdown(breakdown.providers, provider, record.api_key, metrics) elif level == _GROUP_DATE_MCP: if record.mcp_namespaced_tool_name: assign_metric_with_metadata(breakdown.mcp_servers, record.mcp_namespaced_tool_name, metrics) elif level == _GROUP_DATE_MCP_API_KEY: - if record.mcp_namespaced_tool_name and record.api_key: + if record.mcp_namespaced_tool_name and not is_ptu_sentinel: assign_api_key_breakdown( breakdown.mcp_servers, record.mcp_namespaced_tool_name, @@ -854,7 +884,7 @@ def _aggregate_grouping_sets_records_sync( if record.endpoint: assign_metric_with_metadata(breakdown.endpoints, record.endpoint, metrics) elif level == _GROUP_DATE_ENDPOINT_API_KEY: - if record.endpoint and record.api_key: + if record.endpoint and not is_ptu_sentinel: assign_api_key_breakdown(breakdown.endpoints, record.endpoint, record.api_key, metrics) results: Final = [ @@ -1013,8 +1043,8 @@ def _fold_entity_rollups_sync( metrics = _record_to_spend_metrics(row) if row.api_key_rolled: bucket.metrics = metrics - elif row.api_key and row.api_key != PTU_SENTINEL_API_KEY: - bucket.api_key_breakdown[row.api_key] = KeyMetricWithMetadata( + elif row.api_key != PTU_SENTINEL_API_KEY: + bucket.api_key_breakdown[api_key_bucket(row.api_key)] = KeyMetricWithMetadata( metrics=metrics, metadata=_key_metadata(api_key_metadata, row.api_key) ) diff --git a/litellm/proxy/management_endpoints/daily_activity_routes.py b/litellm/proxy/management_endpoints/daily_activity_routes.py index 79e515944ce..1a3445aae52 100644 --- a/litellm/proxy/management_endpoints/daily_activity_routes.py +++ b/litellm/proxy/management_endpoints/daily_activity_routes.py @@ -22,6 +22,7 @@ from litellm.proxy.management_endpoints.common_daily_activity import ( parse_canonical_date_range, raise_public, spend_logs_window, + sum_spend_metrics, ) from litellm.proxy.management_endpoints.daily_activity_scopes import ( AGENT_RESOLVER, @@ -38,6 +39,7 @@ from litellm.proxy.management_endpoints.ptu_consumption import single_team_id, w from litellm.proxy.management_endpoints.team_endpoints import aggregated_date_range_error from litellm.proxy.management_helpers.utils import management_endpoint_wrapper from litellm.proxy.utils import PrismaClient, get_prisma_client_or_throw +from litellm.repositories.daily_activity_buckets import api_key_bucket from litellm.repositories.daily_activity_repository import DailyActivityRepository from litellm.router import Router from litellm.types.proxy.management_endpoints.common_daily_activity import ( @@ -106,29 +108,9 @@ async def _resolved_scope( return result -def _sum_metrics(metrics: Sequence[SpendMetrics]) -> SpendMetrics: - return SpendMetrics( - spend=sum(metric.spend for metric in metrics), - flat_cost=sum(metric.flat_cost for metric in metrics), - prompt_tokens=sum(metric.prompt_tokens for metric in metrics), - completion_tokens=sum(metric.completion_tokens for metric in metrics), - cache_read_input_tokens=sum(metric.cache_read_input_tokens for metric in metrics), - cache_creation_input_tokens=sum(metric.cache_creation_input_tokens for metric in metrics), - compression_saved_tokens=sum(metric.compression_saved_tokens for metric in metrics), - compression_savings_spend=sum(metric.compression_savings_spend for metric in metrics), - prompt_caching_savings_spend=sum(metric.prompt_caching_savings_spend for metric in metrics), - gateway_injected_caching_savings_spend=sum(metric.gateway_injected_caching_savings_spend for metric in metrics), - autorouter_savings_spend=sum(metric.autorouter_savings_spend for metric in metrics), - total_tokens=sum(metric.total_tokens for metric in metrics), - successful_requests=sum(metric.successful_requests for metric in metrics), - failed_requests=sum(metric.failed_requests for metric in metrics), - api_requests=sum(metric.api_requests for metric in metrics), - total_response_time_ms=sum(metric.total_response_time_ms for metric in metrics), - timed_requests=sum(metric.timed_requests for metric in metrics), - ) - - def _key_metadata(api_key: str, metadata: Mapping[str, KeyMetadataRow]) -> KeyMetadata: + if not api_key: + return KeyMetadata(key_alias="Unattributed", key_exists=False) row: Final[KeyMetadataRow | None] = metadata.get(api_key) if row is None: return KeyMetadata() @@ -143,7 +125,7 @@ def _key_metadata(api_key: str, metadata: Mapping[str, KeyMetadataRow]) -> KeyMe def _key_activity_row(row: KeySpendRow, metadata: Mapping[str, KeyMetadataRow]) -> KeySpendActivityRow: return KeySpendActivityRow( - api_key=row.api_key, + api_key=api_key_bucket(row.api_key), metrics=KeySpendMetrics( spend=row.spend, prompt_tokens=row.prompt_tokens, @@ -168,7 +150,7 @@ async def _key_activity_rows( frozenset((resolved_scope.scope.start_date, resolved_scope.scope.end_date)) ) metadata: Final[Mapping[str, KeyMetadataRow]] = await repository.key_metadata( - frozenset(row.api_key for row in rows), + frozenset(row.api_key for row in rows if row.api_key), spend_window, ) return [_key_activity_row(row, metadata) for row in rows] @@ -231,11 +213,11 @@ def _fold_key_metrics(api_key: str, response: SpendAnalyticsPaginatedResponse) - metadata: Final[KeyMetadata] = next( day.breakdown.api_keys[api_key].metadata for day in response.results if api_key in day.breakdown.api_keys ) - return KeyActivityRow(api_key=api_key, metrics=_sum_metrics(metrics), metadata=metadata) + return KeyActivityRow(api_key=api_key, metrics=sum_spend_metrics(metrics), metadata=metadata) def _search_rows(keys: Sequence[str], response: SpendAnalyticsPaginatedResponse) -> list[KeyActivityRow]: - return [row for key in keys if (row := _fold_key_metrics(key, response)) is not None] + return [row for key in keys if (row := _fold_key_metrics(api_key_bucket(key), response)) is not None] def _csv_cell(value: object) -> object: diff --git a/litellm/proxy/management_endpoints/daily_activity_scopes.py b/litellm/proxy/management_endpoints/daily_activity_scopes.py index 77246f5dda6..ca1a96f9d84 100644 --- a/litellm/proxy/management_endpoints/daily_activity_scopes.py +++ b/litellm/proxy/management_endpoints/daily_activity_scopes.py @@ -17,6 +17,7 @@ from litellm.proxy.management_endpoints.organization_endpoints import resolve_or from litellm.proxy.management_endpoints.tag_management_endpoints import get_tag_daily_activity_api_key_filter from litellm.proxy.management_endpoints.team_endpoints import resolve_team_daily_activity_scope from litellm.proxy.utils import PrismaClient +from litellm.repositories.daily_activity_buckets import raw_api_key_filter from litellm.types.repositories.daily_activity import DailyActivityScope, DailyActivityTable @@ -77,13 +78,7 @@ def _build_scope( ) -> ResolvedScope: start_date: Final[str] = query.start_date or "" end_date: Final[str] = query.end_date or "" - api_keys: Final[tuple[str, ...] | None] = ( - None - if api_key_filter is None or api_key_filter == "" - else (api_key_filter,) - if isinstance(api_key_filter, str) - else tuple(api_key_filter) - ) + api_keys: Final = raw_api_key_filter(api_key_filter) return ResolvedScope( scope=DailyActivityScope( table=resolver.table, diff --git a/litellm/proxy/management_endpoints/fallback_management_endpoints.py b/litellm/proxy/management_endpoints/fallback_management_endpoints.py index 6a9a30a7689..ec8ee5ca213 100644 --- a/litellm/proxy/management_endpoints/fallback_management_endpoints.py +++ b/litellm/proxy/management_endpoints/fallback_management_endpoints.py @@ -20,7 +20,7 @@ from litellm._logging import verbose_proxy_logger from litellm.proxy._types import UserAPIKeyAuth from litellm.proxy.auth.model_checks import get_all_fallbacks from litellm.proxy.auth.user_api_key_auth import user_api_key_auth -from litellm.proxy.utils import PrismaClient, evict_config_param, invalidate_config_param +from litellm.proxy.utils import PrismaClient, invalidate_config_param if TYPE_CHECKING: from fastapi import APIRouter, Depends, HTTPException, status @@ -48,22 +48,50 @@ FallbackRule = dict[str, list[str]] StoredFallback = Annotated[FallbackRule | dict[str, object] | str, Field(union_mode="left_to_right")] _STORED_FALLBACKS: Final = TypeAdapter(list[StoredFallback]) _JSON_RULES: Final = TypeAdapter(list[JsonValue]) +_STORED_ROUTER_SETTINGS: Final = TypeAdapter(dict[str, JsonValue]) def _rule_covers(entry: StoredFallback, model: str) -> bool: return isinstance(entry, dict) and model in entry -async def _router_settings_fresh_from_db(proxy_config: "ProxyConfig") -> dict[str, object]: - await evict_config_param(ROUTER_SETTINGS_PARAM) - config: Final = await proxy_config.get_config() - return config.get(ROUTER_SETTINGS_PARAM, {}) +async def _reload_config_file_ownership(proxy_config: "ProxyConfig") -> None: + await proxy_config.get_config() -def _reject_config_owned_rules(proxy_config: "ProxyConfig", fallback_key: str, rules: Sequence[StoredFallback]) -> None: +async def _stored_router_settings(prisma_client: PrismaClient) -> Mapping[str, JsonValue]: + try: + row: Final = await ConfigRepository(prisma_client, use_writer=True).get_param(ROUTER_SETTINGS_PARAM) + except json.JSONDecodeError: + return {} + if row is None or not isinstance(row.param_value, Mapping): + return {} + return _STORED_ROUTER_SETTINGS.validate_python(row.param_value) + + +def _editable_rules( + proxy_config: "ProxyConfig", stored_settings: Mapping[str, JsonValue], fallback_key: str +) -> list[StoredFallback]: + file_settings: Final = proxy_config.router_settings + if not file_settings.owned_by_config(fallback_key): + return _STORED_FALLBACKS.validate_python(stored_settings.get(fallback_key) or []) + file_rules: Final = file_settings.get(fallback_key) + return _STORED_FALLBACKS.validate_python(file_rules if isinstance(file_rules, list) else []) + + +async def _write_rules( + prisma_client: PrismaClient, + proxy_config: "ProxyConfig", + stored_settings: Mapping[str, JsonValue], + fallback_key: str, + rules: Sequence[StoredFallback], +) -> None: proxy_config.reject_config_owned_writes( section_name=ROUTER_SETTINGS_PARAM, changed_keys={fallback_key: _JSON_RULES.validate_python(rules)} ) + if proxy_config.router_settings.owned_by_config(fallback_key): + return + await _persist_router_settings(prisma_client, {**stored_settings, fallback_key: rules}) async def _persist_router_settings(prisma_client: PrismaClient, router_settings: Mapping[str, object]) -> None: @@ -162,7 +190,8 @@ async def create_fallback( }, ) - router_settings: Final = await _router_settings_fresh_from_db(proxy_config) + await _reload_config_file_ownership(proxy_config) + router_settings: Final = await _stored_router_settings(prisma_client) # Get the appropriate fallback list based on type fallback_key = "fallbacks" @@ -172,7 +201,7 @@ async def create_fallback( fallback_key = "content_policy_fallbacks" # Get existing fallbacks - existing_fallbacks: Final = _STORED_FALLBACKS.validate_python(router_settings.get(fallback_key) or []) + existing_fallbacks: Final = _editable_rules(proxy_config, router_settings, fallback_key) # Update or add the fallback configuration fallback_updated = False @@ -187,12 +216,7 @@ async def create_fallback( # Add new fallback existing_fallbacks.append({data.model: data.fallback_models}) - _reject_config_owned_rules(proxy_config, fallback_key, existing_fallbacks) - - # Update router settings - router_settings[fallback_key] = existing_fallbacks - - await _persist_router_settings(prisma_client, router_settings) + await _write_rules(prisma_client, proxy_config, router_settings, fallback_key, existing_fallbacks) # Update the in-memory router configuration setattr(llm_router, fallback_key, existing_fallbacks) @@ -320,7 +344,8 @@ async def delete_fallback( }, ) - router_settings: Final = await _router_settings_fresh_from_db(proxy_config) + await _reload_config_file_ownership(proxy_config) + router_settings: Final = await _stored_router_settings(prisma_client) # Get the appropriate fallback list based on type fallback_key = "fallbacks" @@ -330,7 +355,7 @@ async def delete_fallback( fallback_key = "content_policy_fallbacks" # Get existing fallbacks - existing_fallbacks: Final = _STORED_FALLBACKS.validate_python(router_settings.get(fallback_key) or []) + existing_fallbacks: Final = _editable_rules(proxy_config, router_settings, fallback_key) updated_fallbacks: Final = [rule for rule in existing_fallbacks if not _rule_covers(rule, model)] @@ -340,12 +365,7 @@ async def delete_fallback( detail={"error": f"No {fallback_type} fallbacks configured for model '{model}'"}, ) - _reject_config_owned_rules(proxy_config, fallback_key, updated_fallbacks) - - # Update router settings - router_settings[fallback_key] = updated_fallbacks - - await _persist_router_settings(prisma_client, router_settings) + await _write_rules(prisma_client, proxy_config, router_settings, fallback_key, updated_fallbacks) # Update the in-memory router configuration setattr(llm_router, fallback_key, updated_fallbacks) diff --git a/litellm/proxy/management_endpoints/tag_management_endpoints.py b/litellm/proxy/management_endpoints/tag_management_endpoints.py index c12a90ed6a2..ea7810a618c 100644 --- a/litellm/proxy/management_endpoints/tag_management_endpoints.py +++ b/litellm/proxy/management_endpoints/tag_management_endpoints.py @@ -31,6 +31,7 @@ from litellm.proxy.management_endpoints.common_daily_activity import ( get_daily_activity, ) from litellm.proxy.management_helpers.utils import handle_budget_for_entity +from litellm.repositories.daily_activity_buckets import api_key_from_bucket from litellm.repositories.model_repository import ModelRepository from litellm.repositories.table_repositories import ( DailyTagSpendRepository, @@ -198,16 +199,17 @@ async def get_tag_daily_activity_api_key_filter( user_api_key_dict: UserAPIKeyAuth, requested_api_key: str | None, ) -> str | list[str] | None: + selected_key: Final = api_key_from_bucket(requested_api_key) if requested_api_key else None user_role: Final = user_api_key_dict.user_role if user_api_key_has_admin_view(user_api_key_dict) or (user_role is None or not user_role.is_internal_user_role): - return requested_api_key + return [selected_key] if selected_key is not None else None scoped_api_keys: Final = await _get_internal_user_api_keys( prisma_client=prisma_client, user_api_key_dict=user_api_key_dict, ) - if requested_api_key is not None: - return requested_api_key if requested_api_key in scoped_api_keys else [] + if selected_key is not None: + return [selected_key] if selected_key in scoped_api_keys else [] return scoped_api_keys diff --git a/litellm/proxy/management_endpoints/team_endpoints.py b/litellm/proxy/management_endpoints/team_endpoints.py index 50b700e712d..b712ba42a6a 100644 --- a/litellm/proxy/management_endpoints/team_endpoints.py +++ b/litellm/proxy/management_endpoints/team_endpoints.py @@ -226,6 +226,7 @@ from litellm.proxy.management_helpers.utils import ( from litellm.proxy.utils import PrismaClient, ProxyLogging, handle_exception_on_proxy from litellm.repositories.budget_repository import BudgetRepository from litellm.repositories.chunked_in import delete_many_in, find_many_in +from litellm.repositories.daily_activity_buckets import api_key_from_bucket from litellm.repositories.organization_repository import OrganizationRepository from litellm.repositories.prisma_protocols import TableActions from litellm.repositories.table_repositories import ( @@ -7329,11 +7330,12 @@ async def resolve_team_daily_activity_scope( if not user_api_keys: user_api_keys = [] + selected_key: Final = api_key_from_bucket(api_key) if api_key else None final_api_key_filter: str | list[str] | None if user_api_keys is None: - final_api_key_filter = api_key - elif api_key: - final_api_key_filter = api_key if api_key in user_api_keys else [] + final_api_key_filter = [selected_key] if selected_key is not None else None + elif selected_key is not None: + final_api_key_filter = [selected_key] if selected_key in user_api_keys else [] else: final_api_key_filter = user_api_keys diff --git a/litellm/proxy/mcp_server/oauth_issuer_stamp_backfill.py b/litellm/proxy/mcp_server/oauth_issuer_stamp_backfill.py index c417187396f..6c6be219a92 100644 --- a/litellm/proxy/mcp_server/oauth_issuer_stamp_backfill.py +++ b/litellm/proxy/mcp_server/oauth_issuer_stamp_backfill.py @@ -43,6 +43,7 @@ from litellm._logging import verbose_proxy_logger from litellm.proxy.db.db_span import db_span from litellm.proxy.mcp_server.oauth_utils import canonicalize_url_identity from litellm.proxy.utils import PrismaClient +from litellm.repositories.table_repositories import MCPServerRepository # The actor the removed discovery write-back stamped rows with. _DISCOVERY_ACTOR: Final = "mcp_oauth_discovery" @@ -106,7 +107,7 @@ def _is_stamped_issuer_row(row: _MCPServerRow) -> bool: async def backfill_discovery_stamped_issuers(prisma_client: PrismaClient) -> int: """Clear gateway-written issuer stamps, returning the number of rows healed.""" - candidate_rows: Final[list[_MCPServerRow]] = await prisma_client.db.litellm_mcpservertable.find_many( + candidate_rows: Final = await MCPServerRepository(prisma_client).table.find_many( where={ "updated_by": _DISCOVERY_ACTOR, "auth_type": {"in": list(_AUTH_TYPES_WITH_ISSUER_ANCHORING)}, diff --git a/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py b/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py index 972ec18d487..f96fab75bd1 100644 --- a/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py +++ b/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py @@ -68,6 +68,7 @@ from litellm.llms.deepgram.common_utils import ( from litellm.llms.fal_ai.cost_calculator import fal_ai_passthrough_cost, fal_ai_queue_base from litellm.llms.nvidia_nim.passthrough.transformation import nvidia_nim_model_group_in_path from litellm.llms.openai.common_utils import OpenAIError as LiteLLMOpenAIError +from litellm.llms.openai.common_utils import is_openai_agents_api_path, openai_api_resource from litellm.llms.openai.workload_identity import get_workload_identity_bearer_token_for_api_base from litellm.llms.oss_decision import OssDecisionProvider, oss_connection, validate_oss_request from litellm.llms.vertex_ai.vertex_llm_base import VertexBase @@ -3530,6 +3531,10 @@ async def deepgram_listen_websocket_route( ) +_OPENAI_BETA_HEADER: Final = "openai-beta" +_CLIENT_OPENAI_BETA_RESOURCES: Final = frozenset({"agents", "vaults", "chatkit"}) + + class BaseOpenAIPassThroughHandler: @staticmethod async def base_openai_pass_through_handler( @@ -3540,7 +3545,7 @@ class BaseOpenAIPassThroughHandler: base_target_url: str, api_key: str | None, custom_llm_provider: litellm.LlmProviders, - extra_headers: dict | None = None, + extra_headers: Mapping[str, str] | None = None, ): encoded_endpoint = httpx.URL(endpoint).path # Ensure endpoint starts with '/' for proper URL construction @@ -3565,7 +3570,11 @@ class BaseOpenAIPassThroughHandler: endpoint=endpoint, target=str(updated_url), custom_headers=BaseOpenAIPassThroughHandler._assemble_headers( - api_key=api_key, request=request, extra_headers=extra_headers + api_key=api_key, + endpoint=endpoint, + request=request, + custom_llm_provider=custom_llm_provider, + extra_headers=extra_headers, ), is_streaming_request=is_streaming_request, custom_llm_provider=( @@ -3587,27 +3596,41 @@ class BaseOpenAIPassThroughHandler: _base_openai_pass_through_handler = base_openai_pass_through_handler @staticmethod - def _append_openai_beta_header(headers: dict, request: Request) -> dict: - """ - Appends the OpenAI-Beta header to the headers if the request is an OpenAI Assistants API request - """ - if RouteChecks.is_assistants_api_request(request) is True and "OpenAI-Beta" not in headers: - headers["OpenAI-Beta"] = "assistants=v2" - return headers + def _openai_beta(endpoint: str, request: Request, custom_llm_provider: litellm.LlmProviders) -> str | None: + if ( + custom_llm_provider == litellm.LlmProviders.OPENAI + and openai_api_resource(endpoint) in _CLIENT_OPENAI_BETA_RESOURCES + ): + return request.headers.get(_OPENAI_BETA_HEADER) or ( + "agents=v1" if is_openai_agents_api_path(endpoint) else None + ) + return "assistants=v2" if RouteChecks.is_assistants_api_request(request) else None @staticmethod - def _assemble_headers(api_key: str | None, request: Request, extra_headers: dict | None = None) -> dict: - base_headers = {} - if api_key is not None: - base_headers = { - "authorization": f"Bearer {api_key}", - "api-key": f"{api_key}", - } - if extra_headers is not None: - base_headers.update(extra_headers) + def _append_openai_beta_header( + headers: Mapping[str, str], endpoint: str, request: Request, custom_llm_provider: litellm.LlmProviders + ) -> dict[str, str]: + if any(name.lower() == _OPENAI_BETA_HEADER for name in headers): + return {**headers} + openai_beta: Final = BaseOpenAIPassThroughHandler._openai_beta(endpoint, request, custom_llm_provider) + return {**headers} if openai_beta is None else {**headers, _OPENAI_BETA_HEADER: openai_beta} + + @staticmethod + def _assemble_headers( + api_key: str | None, + endpoint: str, + request: Request, + custom_llm_provider: litellm.LlmProviders, + extra_headers: Mapping[str, str] | None = None, + ) -> dict[str, str]: + api_key_headers: Final[Mapping[str, str]] = ( + {} if api_key is None else {"authorization": f"Bearer {api_key}", "api-key": api_key} + ) return BaseOpenAIPassThroughHandler._append_openai_beta_header( - headers=base_headers, + headers={**api_key_headers, **(extra_headers or {})}, + endpoint=endpoint, request=request, + custom_llm_provider=custom_llm_provider, ) diff --git a/litellm/proxy/pass_through_endpoints/llm_provider_handlers/cohere_passthrough_logging_handler.py b/litellm/proxy/pass_through_endpoints/llm_provider_handlers/cohere_passthrough_logging_handler.py index a11b0be3983..4eeb9b26174 100644 --- a/litellm/proxy/pass_through_endpoints/llm_provider_handlers/cohere_passthrough_logging_handler.py +++ b/litellm/proxy/pass_through_endpoints/llm_provider_handlers/cohere_passthrough_logging_handler.py @@ -1,10 +1,12 @@ from datetime import datetime from typing import Final +from urllib.parse import urlparse import httpx import litellm from litellm import stream_chunk_builder +from litellm._logging import verbose_proxy_logger from litellm.litellm_core_utils.hidden_params import set_hidden_param from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj from litellm.litellm_core_utils.litellm_logging import ( @@ -16,7 +18,7 @@ from litellm.llms.cohere.chat.v2_transformation import CohereV2ChatConfig from litellm.llms.cohere.common_utils import ( ModelResponseIterator as CohereModelResponseIterator, ) -from litellm.llms.cohere.embed.v1_transformation import CohereEmbeddingConfig +from litellm.llms.cohere.embed.transformation import CohereEmbeddingConfig, CohereEmbeddingRowShape from litellm.proxy._types import PassThroughEndpointLoggingTypedDict from litellm.types.passthrough_endpoints.pass_through_endpoints import ( PassthroughStandardLoggingPayload, @@ -29,6 +31,12 @@ from litellm.types.utils import ( from .base_passthrough_logging_handler import BasePassthroughLoggingHandler +COHERE_EMBED_PATHS: Final = ("/v1/embed", "/v2/embed") + + +def is_cohere_embed_route(url_route: str) -> bool: + return urlparse(url_route).path.rstrip("/").endswith(COHERE_EMBED_PATHS) + class CoherePassthroughLoggingHandler(BasePassthroughLoggingHandler): @property @@ -82,28 +90,24 @@ class CoherePassthroughLoggingHandler(BasePassthroughLoggingHandler): """ Handle Cohere passthrough logging with route detection and cost tracking. """ - # Check if this is an embed endpoint - if "/v1/embed" in url_route and "/v1/embeddings" not in url_route: + if is_cohere_embed_route(url_route): model: Final = request_body.get("model", response_body.get("model", "")) try: - cohere_embed_config: Final = CohereEmbeddingConfig() - litellm_model_response = litellm.EmbeddingResponse() handler_instance: Final = CoherePassthroughLoggingHandler() input_texts = request_body.get("texts", []) if not input_texts: input_texts = request_body.get("input", []) - # Transform the response - litellm_model_response = cohere_embed_config.transform_response( + litellm_model_response: Final = CohereEmbeddingConfig().transform_native_embed_response( response=httpx_response, api_key="", logging_obj=logging_obj, data=request_body, - model_response=litellm_model_response, model=model, encoding=litellm.encoding, input=input_texts, + row_shape=CohereEmbeddingRowShape.TYPED, ) # Calculate cost using LiteLLM's cost calculator @@ -153,20 +157,11 @@ class CoherePassthroughLoggingHandler(BasePassthroughLoggingHandler): "result": litellm_model_response, "kwargs": kwargs, } - except Exception: - # For other routes (e.g., /v2/chat), fall back to chat handler - return super().passthrough_chat_handler( - httpx_response=httpx_response, - response_body=response_body, - logging_obj=logging_obj, - url_route=url_route, - result=result, - start_time=start_time, - end_time=end_time, - cache_hit=cache_hit, - request_body=request_body, - **kwargs, + except Exception as e: + verbose_proxy_logger.warning( + "Could not price the Cohere passthrough embed response, logging it without a cost: %s", e ) + return {"result": None, "kwargs": kwargs} # For non-embed routes (e.g., /v2/chat), fall back to chat handler return super().passthrough_chat_handler( diff --git a/litellm/proxy/pass_through_endpoints/llm_provider_handlers/openai_passthrough_logging_handler.py b/litellm/proxy/pass_through_endpoints/llm_provider_handlers/openai_passthrough_logging_handler.py index c410150df70..b98dd42e3de 100644 --- a/litellm/proxy/pass_through_endpoints/llm_provider_handlers/openai_passthrough_logging_handler.py +++ b/litellm/proxy/pass_through_endpoints/llm_provider_handlers/openai_passthrough_logging_handler.py @@ -10,7 +10,7 @@ from typing import Final, Literal, TypeAlias from urllib.parse import urlparse import httpx -from pydantic import TypeAdapter +from pydantic import BaseModel, ConfigDict, TypeAdapter, ValidationError import litellm from litellm._logging import verbose_proxy_logger @@ -21,6 +21,8 @@ from litellm.litellm_core_utils.litellm_logging import ( get_standard_logging_object_payload, ) from litellm.litellm_core_utils.token_counter import high_detail_image_token_upper_bound +from litellm.llms.base_llm.base_model_iterator import BaseModelResponseIterator +from litellm.llms.openai.common_utils import is_openai_agents_api_path from litellm.llms.openai.openai import OpenAIConfig from litellm.llms.openai.openai import OpenAIConfig as OpenAIConfigType from litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfig @@ -38,7 +40,13 @@ from litellm.types.passthrough_endpoints.pass_through_endpoints import ( EndpointType, PassthroughStandardLoggingPayload, ) -from litellm.types.utils import EmbeddingResponse, ImageResponse, LlmProviders, PassthroughCallTypes +from litellm.types.utils import ( + EmbeddingResponse, + ImageResponse, + LlmProviders, + PassthroughCallTypes, + StandardPassThroughResponseObject, +) from litellm.utils import ModelResponse, TextCompletionResponse, convert_to_model_response_object # Hostnames that route to OpenAI-compatible APIs. @@ -121,6 +129,52 @@ def _responses_sub_route(path: str) -> tuple[str, ...] | None: return None if anchor is None else tuple(segments[anchor + 1 :]) +_STR_MAPPING: Final = TypeAdapter(Mapping[str, object]) + + +class _AgentConfig(BaseModel): + model_config = ConfigDict(frozen=True) + + model: str + + +class _AgentsApiBody(BaseModel): + model_config = ConfigDict(frozen=True) + + agent: _AgentConfig + + +class _AgentsApiSessionEvent(BaseModel): + model_config = ConfigDict(frozen=True) + + session: _AgentsApiBody + + +_AGENTS_API_BODY: Final = TypeAdapter(_AgentsApiBody | _AgentConfig) + + +def _agent_model(body: Mapping[str, object]) -> str | None: + try: + parsed: Final = _AGENTS_API_BODY.validate_python(body) + except ValidationError: + return None + return parsed.agent.model if isinstance(parsed, _AgentsApiBody) else parsed.model + + +def _session_event_model(chunk: str) -> str | None: + event: Final = BaseModelResponseIterator.string_to_dict_parser(str_line=chunk) + if event is None: + return None + try: + return _AgentsApiSessionEvent.model_validate(event).session.agent.model + except ValidationError: + return None + + +def _streamed_agent_model(all_chunks: Sequence[str]) -> str | None: + return next((model for chunk in all_chunks if (model := _session_event_model(chunk)) is not None), None) + + def _is_remote_high_detail_image(part: object) -> bool: if not isinstance(part, Mapping) or part.get("type") != "image_url": return False @@ -220,14 +274,44 @@ class OpenAIPassthroughLoggingHandler(BasePassthroughLoggingHandler): return "billed" @staticmethod - def _label_unbilled_responses_call( + def is_openai_agents_route(url_route: str) -> bool: + return is_openai_compatible_url(url_route) and is_openai_agents_api_path(urlparse(url_route).path) + + @staticmethod + def _unbilled_call_model( + url_route: str, request_body: Mapping[str, object], response_body: Mapping[str, object] + ) -> str | None: + if OpenAIPassthroughLoggingHandler.is_openai_agents_route(url_route): + return _agent_model(response_body) or _agent_model(request_body) + response_model: Final = response_body.get("model") + if OpenAIPassthroughLoggingHandler.is_openai_responses_route(url_route) and isinstance(response_model, str): + return response_model + return None + + @staticmethod + def _label_model(logging_obj: LiteLLMLoggingObj, model: str | None) -> None: + if model is not None: + logging_obj.model_call_details["model"] = model # rebind-ok: spend rows read the model from here + + @staticmethod + def _label_unbilled_call( logging_obj: LiteLLMLoggingObj, url_route: str, + request_body: Mapping[str, object], response_body: Mapping[str, object], ) -> None: - model: Final = response_body.get("model") - if OpenAIPassthroughLoggingHandler.is_openai_responses_route(url_route) and isinstance(model, str): - logging_obj.model_call_details["model"] = model # rebind-ok: spend rows read the model from here + OpenAIPassthroughLoggingHandler._label_model( + logging_obj, OpenAIPassthroughLoggingHandler._unbilled_call_model(url_route, request_body, response_body) + ) + + @staticmethod + def _log_agents_stream( + litellm_logging_obj: LiteLLMLoggingObj, request_body: Mapping[str, object], all_chunks: Sequence[str] + ) -> PassThroughEndpointLoggingTypedDict: + OpenAIPassthroughLoggingHandler._label_model( + litellm_logging_obj, _streamed_agent_model(all_chunks) or _agent_model(request_body) + ) + return {"result": StandardPassThroughResponseObject(response="\n".join(all_chunks)), "kwargs": {}} @staticmethod def is_openai_embeddings_route(url_route: str) -> bool: @@ -397,7 +481,12 @@ class OpenAIPassthroughLoggingHandler(BasePassthroughLoggingHandler): is_responses: Final = responses_billing != "unbilled" if not (is_chat_completions or is_embeddings or is_image_generation or is_image_editing or is_responses): - OpenAIPassthroughLoggingHandler._label_unbilled_responses_call(logging_obj, url_route, response_body) + OpenAIPassthroughLoggingHandler._label_unbilled_call( + logging_obj, + url_route, + request_body=_STR_MAPPING.validate_python(request_body), + response_body=_STR_MAPPING.validate_python(response_body), + ) return { "result": None, "kwargs": kwargs, @@ -676,6 +765,10 @@ class OpenAIPassthroughLoggingHandler(BasePassthroughLoggingHandler): """ Handle logging for collected OpenAI streaming chunks with cost tracking. """ + if OpenAIPassthroughLoggingHandler.is_openai_agents_route(url_route): + return OpenAIPassthroughLoggingHandler._log_agents_stream( + litellm_logging_obj, _STR_MAPPING.validate_python(request_body), all_chunks + ) try: is_responses: Final = OpenAIPassthroughLoggingHandler.is_openai_responses_route(url_route) terminal_response: Final = ( diff --git a/litellm/proxy/pass_through_endpoints/passthrough_endpoint_router.py b/litellm/proxy/pass_through_endpoints/passthrough_endpoint_router.py index 1d0b3d00434..739d4b3a01a 100644 --- a/litellm/proxy/pass_through_endpoints/passthrough_endpoint_router.py +++ b/litellm/proxy/pass_through_endpoints/passthrough_endpoint_router.py @@ -12,6 +12,7 @@ from litellm.integrations.vector_store_integrations.vector_store_pre_call_hook i ) from litellm.litellm_core_utils.credential_accessor import CredentialAccessor from litellm.secret_managers.main import ( + GOOGLE_OIDC_SECRET_PREFIX, UnresolvableSecretReference, get_secret_str, resolve_local_oidc_reference, @@ -47,6 +48,14 @@ def _credential_identity(credentials: VERTEX_CREDENTIALS_TYPES | None) -> str | def _resolve_oidc_reference(credential: str | None) -> str | None: if credential is None: return None + if credential.startswith(GOOGLE_OIDC_SECRET_PREFIX): + raise HTTPException( + status_code=401, + detail=( + "pass-through credential oidc/google secret reference could not be resolved: pass-through routes " + "do not fetch Google ID tokens. Use a static key, oidc/file/ or oidc/env/." + ), + ) match resolve_local_oidc_reference(credential): case UnresolvableSecretReference(kind=kind, reason=reason): raise HTTPException( diff --git a/litellm/proxy/pass_through_endpoints/success_handler.py b/litellm/proxy/pass_through_endpoints/success_handler.py index 9c6c3111209..27e31c8f74d 100644 --- a/litellm/proxy/pass_through_endpoints/success_handler.py +++ b/litellm/proxy/pass_through_endpoints/success_handler.py @@ -22,6 +22,7 @@ from .llm_provider_handlers.assembly_passthrough_logging_handler import ( ) from .llm_provider_handlers.cohere_passthrough_logging_handler import ( CoherePassthroughLoggingHandler, + is_cohere_embed_route, ) from .llm_provider_handlers.cursor_passthrough_logging_handler import ( CursorPassthroughLoggingHandler, @@ -91,7 +92,7 @@ class PassThroughEndpointLogging: self.TRACKED_ANTHROPIC_ROUTES = ["/messages", "/v1/messages/batches"] # Cohere - self.TRACKED_COHERE_ROUTES = ["/v2/chat", "/v1/embed"] + self.TRACKED_COHERE_ROUTES = ["/v2/chat"] self.assemblyai_passthrough_logging_handler = AssemblyAIPassthroughLoggingHandler() # Langfuse @@ -536,13 +537,7 @@ class PassThroughEndpointLogging: return False def is_cohere_route(self, url_route: str) -> bool: - for route in self.TRACKED_COHERE_ROUTES: - if route not in url_route: - continue - if route == "/v1/embed" and "/v1/embeddings" in url_route: - continue - return True - return False + return is_cohere_embed_route(url_route) or any(route in url_route for route in self.TRACKED_COHERE_ROUTES) def is_assemblyai_route(self, url_route: str): parsed_url: Final = urlparse(url_route) @@ -645,6 +640,7 @@ class PassThroughEndpointLogging: or OpenAIPassthroughLoggingHandler.is_openai_image_generation_route(url_route) or OpenAIPassthroughLoggingHandler.is_openai_image_editing_route(url_route) or OpenAIPassthroughLoggingHandler.is_openai_responses_route(url_route) + or OpenAIPassthroughLoggingHandler.is_openai_agents_route(url_route) ) def _set_cost_per_request( diff --git a/litellm/proxy/plugin_routes.py b/litellm/proxy/plugin_routes.py index 1dfe5cbc6bb..cfbcfefe9e8 100644 --- a/litellm/proxy/plugin_routes.py +++ b/litellm/proxy/plugin_routes.py @@ -31,6 +31,8 @@ from typing import Final from cryptography.fernet import Fernet, InvalidToken from fastapi import APIRouter, Depends, HTTPException, Request, Response +from pydantic import ConfigDict, TypeAdapter, with_config +from typing_extensions import NotRequired, ReadOnly, TypedDict from litellm.llms.custom_httpx.http_handler import get_async_httpx_client from litellm.proxy._types import PluginConfig, SpecialHeaders, UserAPIKeyAuth @@ -39,6 +41,18 @@ from litellm.types.llms.custom_http import httpxSpecialProvider router: Final = APIRouter() + +@with_config(ConfigDict(extra="allow", strict=True)) +class PluginSessionClaim(TypedDict): + plugin: NotRequired[ReadOnly[object]] + user_id: NotRequired[ReadOnly[str]] + user_role: NotRequired[ReadOnly[str]] + exp: NotRequired[ReadOnly[int | float | str]] + + +_PLUGIN_SESSION_CLAIM: Final = TypeAdapter(PluginSessionClaim) + + # Hop-by-hop headers (RFC 7230) and the litellm session cookie — never forwarded # to a plugin backend. Credential headers are added on top per-request from the # canonical SpecialHeaders set so the plugin only ever authenticates via its own @@ -155,7 +169,7 @@ def issue_plugin_session_claim(plugin_name: str, user_id: str | None, user_role: return _plugin_fernet(plugin_name).encrypt(json.dumps(claim).encode()).decode() -def verify_plugin_session_claim(plugin_name: str, ciphertext: str) -> dict: +def verify_plugin_session_claim(plugin_name: str, ciphertext: str) -> PluginSessionClaim: """Verify and decode a plugin session claim. Raises ValueError if the HMAC is invalid, the audience is wrong, or @@ -163,7 +177,7 @@ def verify_plugin_session_claim(plugin_name: str, ciphertext: str) -> dict: """ try: raw: Final = _plugin_fernet(plugin_name).decrypt(ciphertext.encode(), ttl=_CLAIM_TTL_SECONDS) - claim: Final = json.loads(raw) + claim: Final = _PLUGIN_SESSION_CLAIM.validate_python(json.loads(raw)) except (InvalidToken, Exception) as exc: raise ValueError("Invalid, tampered, or expired plugin session claim") from exc diff --git a/litellm/proxy/proxy_cli.py b/litellm/proxy/proxy_cli.py index 0d8ab98a0b1..f71d1960e24 100644 --- a/litellm/proxy/proxy_cli.py +++ b/litellm/proxy/proxy_cli.py @@ -276,18 +276,19 @@ class ProxyInitializationHelpers: @staticmethod def run_migration_data_check(use_migrate: bool) -> int: - """`--check_migrations_against_existing_data`: the pre-migration row check against DATABASE_URL, nothing applied. - 0 when the pending schema changes fit the rows, 2 with the refusal when they do not.""" + """`--check_migrations_against_existing_data`: the pending migrations applied to DATABASE_URL inside one + transaction that is rolled back, so Postgres reports what the upgrade would hit. 0 when they all apply, + 2 with Postgres's error when one does not.""" if os.getenv("DATABASE_URL") is None: raise click.UsageError("--check_migrations_against_existing_data requires DATABASE_URL") from litellm.proxy.db.prisma_client import PrismaManager - refusal: Final[str | None] = PrismaManager.migration_data_check_report(use_migrate=use_migrate) # pyright: ignore[reportUnknownVariableType] # litellm-proxy-extras is an optional install - if refusal is None: - print("LiteLLM Proxy: the pending migrations and indexes can be applied to the rows this database has.") + ok, summary = PrismaManager.migration_dry_run_summary(use_migrate=use_migrate) + if ok: + print(f"LiteLLM Proxy: {summary}") return 0 print( - f"\033[1;31mLiteLLM Proxy: Database migration cannot proceed. {refusal}\033[0m", file=sys.stderr, flush=True + f"\033[1;31mLiteLLM Proxy: Database migration cannot proceed. {summary}\033[0m", file=sys.stderr, flush=True ) return 2 @@ -1059,8 +1060,9 @@ class ProxyInitializationHelpers: "--check_migrations_against_existing_data", is_flag=True, default=False, - help="Check, read-only, that the rows in DATABASE_URL can take this version's pending migrations and indexes, " - "print what would block them, then exit without applying anything. Exit code 2 when something blocks.", + help="Dry-run this version's pending migrations on DATABASE_URL: the real SQL inside one transaction that is " + "rolled back, so Postgres reports the row or schema that would make the upgrade fail, then exit. Holds the same " + "locks the upgrade will. Exit code 2 when a migration fails.", ) @click.option( "--keepalive_timeout", diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 7eeb138ce99..79e50ab0d0f 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -79,6 +79,7 @@ from litellm.constants import ( DEFAULT_SHARED_HEALTH_CHECK_LOCK_TTL, DEFAULT_SHARED_HEALTH_CHECK_TTL, DEFAULT_SLACK_ALERTING_THRESHOLD, + GRACEFUL_SHUTDOWN_TEARDOWN_HEADROOM_SECONDS, INTERNAL_CALLBACK_NAMES, LANGFUSE_SHUTDOWN_FLUSH_TIMEOUT_MILLIS, LITELLM_EMBEDDING_PROVIDERS_SUPPORTING_INPUT_ARRAY_OF_TOKENS, @@ -98,6 +99,7 @@ from litellm.litellm_core_utils.litellm_logging import ( _init_custom_logger_compatible_class, initialize_sentry, ) +from litellm.litellm_core_utils.logging_worker import GLOBAL_LOGGING_WORKER, LoggingWorker from litellm.litellm_core_utils.safe_json_dumps import safe_dumps from litellm.litellm_core_utils.safe_json_loads import safe_json_loads from litellm.litellm_core_utils.token_counter import ( @@ -207,7 +209,7 @@ from litellm.types.utils import ( is_server_derived_pricing_key, pricing_override_fields, ) -from litellm.utils import cost_map_omits_token_price, load_credentials_from_list, with_named_credential +from litellm.utils import acredential_updates, cost_map_omits_token_price, with_named_credential if TYPE_CHECKING: from aiohttp import ClientSession @@ -1274,15 +1276,44 @@ async def _flush_spend_logs_queue_on_shutdown() -> None: async def _flush_spend_on_shutdown() -> None: + from litellm.proxy.utils import update_daily_tag_spend + await flush_spend_counters_on_shutdown() + if prisma_client is not None: + await update_daily_tag_spend(prisma_client=prisma_client, proxy_logging_obj=proxy_logging_obj) await _flush_spend_logs_queue_on_shutdown() async def _flush_spend_around_logging_drain_on_shutdown( flush_spend: Callable[[], Awaitable[None]] = _flush_spend_on_shutdown, + *, + worker: LoggingWorker = GLOBAL_LOGGING_WORKER, + clock: Callable[[], float] = time.monotonic, + drain_tasks: Callable[[float], Awaitable[None]] = drain_logging_tasks, ) -> None: await flush_spend() - await drain_logging_tasks() + remaining: Final = GracefulShutdownManager.remaining(clock=clock) + callback_timeout: Final = max(0.0, remaining - GRACEFUL_SHUTDOWN_TEARDOWN_HEADROOM_SECONDS) + verbose_proxy_logger.info( + "Draining success callbacks before the final spend flush remaining_s=%.3f callback_timeout_s=%.3f", + remaining, + callback_timeout, + ) + + async def drain_callbacks() -> None: + await drain_tasks(callback_timeout) + await worker.flush() + + try: + await asyncio.wait_for(drain_callbacks(), timeout=callback_timeout) + except asyncio.TimeoutError: + verbose_proxy_logger.warning( + "Success callback shutdown budget exhausted with %d unfinished callbacks; accounting may be incomplete", + worker.pending_count, + ) + finally: + await worker.stop(discard_pending=True, timeout=GracefulShutdownManager.remaining(2.0, clock=clock)) + await drain_tasks(GracefulShutdownManager.remaining(5.0, clock=clock)) await flush_spend() @@ -1445,6 +1476,13 @@ async def _connect_to_count_stored_values() -> SupportsRawQueries: @asynccontextmanager async def proxy_startup_event(app: FastAPI) -> AsyncGenerator[ProxyLifespanState, None]: + with GracefulShutdownManager.capture_sigterm(): + async with _proxy_startup_event(app) as state: + yield state + + +@asynccontextmanager +async def _proxy_startup_event(app: FastAPI) -> AsyncGenerator[ProxyLifespanState, None]: global \ prisma_client, \ master_key, \ @@ -14779,10 +14817,10 @@ def _provider_count_outcome( return result -def _with_resolved_api_key(litellm_params: object) -> Mapping[str, object]: +async def _with_resolved_api_key(litellm_params: object) -> Mapping[str, object]: call_params: Final = _LITELLM_PARAMS_COPY.validate_python(litellm_params) try: - load_credentials_from_list(call_params) + return {**call_params, **(await acredential_updates(call_params))} except litellm.AuthenticationError as e: raise ProxyException( message=e.message, @@ -14790,13 +14828,19 @@ def _with_resolved_api_key(litellm_params: object) -> Mapping[str, object]: param="model", code=401, ) from e - return call_params + except litellm.ServiceUnavailableError as e: + raise ProxyException( + message=e.message, + type="service_unavailable", + param="model", + code=503, + ) from e -def _with_resolved_credentials(deployment: Mapping[str, object] | None) -> dict[str, object] | None: +async def _with_resolved_credentials(deployment: Mapping[str, object] | None) -> dict[str, object] | None: if deployment is None: return None - return {**deployment, "litellm_params": _with_resolved_api_key(deployment.get("litellm_params", {}))} + return {**deployment, "litellm_params": await _with_resolved_api_key(deployment.get("litellm_params", {}))} _DEPLOYMENT_LITELLM_PARAMS: Final = TypeAdapter(Mapping[str, object]) @@ -14859,7 +14903,7 @@ async def _try_provider_token_count( model_to_use=model_to_use or "", messages=messages, contents=contents, - deployment=_with_resolved_credentials(deployment_in_region), + deployment=await _with_resolved_credentials(deployment_in_region), request_model=request_model, tools=tools, system=system, @@ -14893,7 +14937,7 @@ async def _try_native_responses_count( model_to_use=model_to_use, request=responses_request, policy=policy, - deployment=_with_resolved_credentials(deployment), + deployment=await _with_resolved_credentials(deployment), request_model=request_model, ) ) diff --git a/litellm/proxy/public_endpoints/provider_create_fields.json b/litellm/proxy/public_endpoints/provider_create_fields.json index fd17079b15f..04e6c5d8ba9 100644 --- a/litellm/proxy/public_endpoints/provider_create_fields.json +++ b/litellm/proxy/public_endpoints/provider_create_fields.json @@ -2023,6 +2023,34 @@ ], "default_model_placeholder": "jina_ai/" }, + { + "provider": "KYMA", + "provider_display_name": "Kyma API", + "litellm_provider": "kyma", + "credential_fields": [ + { + "key": "api_base", + "label": "API Base", + "placeholder": "https://kymaapi.com/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": "kyma/qwen3.7-flash" + }, { "provider": "Laya", "provider_display_name": "Laya", @@ -3584,7 +3612,7 @@ "key": "api_key", "label": "Soniox API Key", "placeholder": null, - "tooltip": "Currently only the async Speech-to-Text REST API (api.soniox.com) is supported. Realtime STT (stt-rt.soniox.com) and TTS (tts-rt.soniox.com) are not yet available.", + "tooltip": "Supports async Speech-to-Text (api.soniox.com) on /v1/audio/transcriptions and realtime Speech-to-Text (stt-rt.soniox.com) on /v1/realtime. TTS (tts-rt.soniox.com) is not yet available.", "required": true, "field_type": "password", "options": null, diff --git a/litellm/proxy/rag_endpoints/endpoints.py b/litellm/proxy/rag_endpoints/endpoints.py index a7146f13035..2d81dc32181 100644 --- a/litellm/proxy/rag_endpoints/endpoints.py +++ b/litellm/proxy/rag_endpoints/endpoints.py @@ -54,6 +54,7 @@ from litellm.proxy.rag_endpoints.upload_security import ( from litellm.proxy.vector_store_endpoints.endpoints import ( build_request_data_from_managed_vector_store, reject_caller_embedding_selection_params, + reject_caller_opensearch_index_name, ) from litellm.proxy.vector_store_endpoints.utils import ( assert_user_can_access_vector_store_id, @@ -913,6 +914,11 @@ async def rag_query( **retrieval_config, **store_data, } + reject_caller_opensearch_index_name( + caller_body=retrieval_config, + request_fields=merged_retrieval_config, + source="retrieval_config", + ) effective_vector_store_config: Final = ( managed_store_params if managed_store is not None diff --git a/litellm/proxy/response_api_endpoints/endpoints.py b/litellm/proxy/response_api_endpoints/endpoints.py index 9e642a02d49..772bb3e53d2 100644 --- a/litellm/proxy/response_api_endpoints/endpoints.py +++ b/litellm/proxy/response_api_endpoints/endpoints.py @@ -34,6 +34,7 @@ from litellm._logging import verbose_proxy_logger from litellm.anthropic_interface.exceptions.exception_mapping_utils import AnthropicExceptionMapping from litellm.caching.dual_cache import DualCache from litellm.constants import EMPTY_MAPPING, MANAGED_BACKGROUND_RESPONSE_METADATA_KEY, TERMINAL_RESPONSE_STATUSES +from litellm.exceptions import EndpointNotSupportedByModelError from litellm.integrations.custom_guardrail import ModifyResponseException from litellm.litellm_core_utils.core_helpers import as_str_mapping, get_metadata_variable_name_from_kwargs from litellm.litellm_core_utils.internal_call_metadata import is_background_response, requests_background @@ -2086,6 +2087,14 @@ def _routing_hints_from_first_ws_frame(first_message: str) -> Mapping[str, objec def _responses_ws_failure_frame(failure: Exception) -> str: + if isinstance(failure, EndpointNotSupportedByModelError): + return json.dumps( + { + "type": "error", + "status": 400, + "error": {"type": "invalid_request_error", "message": failure.message.split("\n\n", 1)[0]}, + } + ) proxy_exception: Final = handle_exception_on_proxy(failure) status: Final = int(proxy_exception.code) if str(proxy_exception.code).isdigit() else 500 error_type: Final = ( @@ -2444,7 +2453,12 @@ async def _responses_websocket_session( with contextlib.suppress(Exception): await websocket.send_text(_responses_ws_failure_frame(e)) with contextlib.suppress(Exception): - await websocket.close(code=1011, reason="Internal server error") + await websocket.close( + code=1008 if isinstance(e, EndpointNotSupportedByModelError) else 1011, + reason="Pre-call error" + if isinstance(e, EndpointNotSupportedByModelError) + else "Internal server error", + ) return crash_row return bill_socket_failure(outcome) if isinstance(outcome, Exception) else None diff --git a/litellm/proxy/route_llm_request.py b/litellm/proxy/route_llm_request.py index db1951ec9e2..fea4f342b91 100644 --- a/litellm/proxy/route_llm_request.py +++ b/litellm/proxy/route_llm_request.py @@ -431,6 +431,28 @@ def raise_if_required_body_param_missing( ) +def endpoint_unsupported_by_model( + route_type: str, + data: Mapping[str, object], + llm_router: LitellmRouter | None, + team_id: str | None, +) -> bool: + model: Final = data.get("model") + if not isinstance(model, str) or llm_router is None: + return False + metadata: Final = data.get("metadata") + request_kwargs: Final = { + **data, + "metadata": { + **(metadata if isinstance(metadata, Mapping) else {}), + "user_api_key_team_id": team_id, + }, + } + return bool(llm_router.get_routable_deployments(model, request_kwargs)) and not llm_router.get_routable_deployments( + model, request_kwargs, call_type=route_type + ) + + class MockTestingParamsDisabledError(HTTPException): def __init__(self, params: tuple[str, ...]): super().__init__( diff --git a/litellm/proxy/shutdown/graceful_shutdown_manager.py b/litellm/proxy/shutdown/graceful_shutdown_manager.py index d7a07ec34e5..9aeee0453ff 100644 --- a/litellm/proxy/shutdown/graceful_shutdown_manager.py +++ b/litellm/proxy/shutdown/graceful_shutdown_manager.py @@ -5,7 +5,8 @@ Kubernetes terminates a pod by sending ``SIGTERM`` and, after ``terminationGracePeriodSeconds``, ``SIGKILL``. On ``SIGTERM`` uvicorn stops accepting connections and waits up to ``GRACEFUL_SHUTDOWN_TIMEOUT`` for open ones, cancelling whatever is still running, before the lifespan teardown that -runs this drain. +runs this drain. The lifespan registers a SIGTERM wrapper before serving +requests so callback draining can subtract the preceding connection drain. A fixed ``preStop`` sleep can not solve this: it has to be sized for the *worst-case* request, so it either wastes time on every routine shutdown or is @@ -20,8 +21,12 @@ granularity of ``InFlightRequestsMiddleware``. import asyncio import os +import signal +import threading import time -from collections.abc import Callable +from collections.abc import Callable, Generator +from contextlib import contextmanager +from types import FrameType from typing import Final from litellm._logging import verbose_proxy_logger @@ -44,6 +49,7 @@ class GracefulShutdownManager: _is_shutting_down: bool = False _shutdown_started_at: float | None = None _drain_performed: bool = False + _sigterm_handler: Callable[[int, FrameType | None], None] | None = None @classmethod def is_shutting_down(cls) -> bool: @@ -71,7 +77,7 @@ class GracefulShutdownManager: return DEFAULT_GRACEFUL_SHUTDOWN_TIMEOUT @classmethod - def start_shutdown(cls) -> None: + def start_shutdown(cls, clock: Callable[[], float] = time.monotonic) -> None: """ Mark the worker as draining. Idempotent — repeated calls (e.g. SIGTERM followed by a preStop hit on /health/drain) do not reset the clock. @@ -79,12 +85,42 @@ class GracefulShutdownManager: if cls._is_shutting_down: return cls._is_shutting_down = True - cls._shutdown_started_at = time.monotonic() + cls._shutdown_started_at = clock() verbose_proxy_logger.info( "graceful_shutdown_started in_flight_requests=%s", get_in_flight_requests(), ) + @classmethod + def remaining(cls, cap: float | None = None, *, clock: Callable[[], float] = time.monotonic) -> float: + elapsed: Final = 0.0 if cls._shutdown_started_at is None else clock() - cls._shutdown_started_at + remaining: Final = max(0.0, cls.get_timeout() - elapsed) + return remaining if cap is None else min(max(0.0, cap), remaining) + + @classmethod + @contextmanager + def capture_sigterm(cls, clock: Callable[[], float] = time.monotonic) -> Generator[None, None, None]: + if threading.current_thread() is not threading.main_thread(): + yield + return + previous: Final = signal.getsignal(signal.SIGTERM) + if not callable(previous) or previous is cls._sigterm_handler: + yield + return + + def handle_sigterm(signum: int, frame: FrameType | None) -> None: + cls.start_shutdown(clock) + previous(signum, frame) + + cls._sigterm_handler = handle_sigterm + signal.signal(signal.SIGTERM, handle_sigterm) + try: + yield + finally: + if signal.getsignal(signal.SIGTERM) is handle_sigterm: + signal.signal(signal.SIGTERM, previous) + cls._sigterm_handler = None + @classmethod async def wait_for_drain( cls, @@ -93,13 +129,14 @@ class GracefulShutdownManager: count_fn: Callable[[], int] | None = None, poll_interval: float = _DRAIN_POLL_INTERVAL, log_interval: float = _DRAIN_LOG_INTERVAL, + clock: Callable[[], float] = time.monotonic, ) -> int: """ Poll the in-flight request counter until it reaches the drain target or ``timeout`` seconds elapse. Args: - timeout: Max seconds to wait. Defaults to ``get_timeout()``. + timeout: Max seconds to wait. Defaults to the remaining shutdown budget. exclude_self: When the caller is itself an in-flight HTTP request (the /health/drain endpoint), set this so the caller's own request is not counted as outstanding work. @@ -107,6 +144,7 @@ class GracefulShutdownManager: live ``InFlightRequestsMiddleware`` counter; injectable for tests. poll_interval: Seconds between counter polls. log_interval: Minimum seconds between ``drain_waiting`` log lines. + clock: Monotonic clock shared with the shutdown timestamp. Returns: Number of requests that drained while waiting (>= 0). @@ -120,7 +158,7 @@ class GracefulShutdownManager: cls._drain_performed = True if timeout is None: - timeout = cls.get_timeout() + timeout = cls.remaining(clock=clock) if count_fn is None: count_fn = get_in_flight_requests @@ -128,7 +166,7 @@ class GracefulShutdownManager: # and so counts itself; treat <=1 as "drained" in that case. target: Final = 1 if exclude_self else 0 - start: Final = time.monotonic() + start: Final = clock() initial: Final = count_fn() last_log = start @@ -142,11 +180,11 @@ class GracefulShutdownManager: verbose_proxy_logger.info( "graceful_shutdown_complete drained_requests=%s elapsed_s=%.2f", drained, - time.monotonic() - start, + clock() - start, ) return drained - elapsed = time.monotonic() - start + elapsed = clock() - start if elapsed >= timeout: verbose_proxy_logger.warning( "graceful_shutdown_timeout in_flight_requests=%s elapsed_s=%.2f " @@ -157,7 +195,7 @@ class GracefulShutdownManager: ) return max(0, initial - current) - now = time.monotonic() + now = clock() if now - last_log >= log_interval: verbose_proxy_logger.info( "drain_waiting in_flight_requests=%s elapsed_s=%.2f", diff --git a/litellm/proxy/spend_tracking/spend_tracking_utils.py b/litellm/proxy/spend_tracking/spend_tracking_utils.py index 94b079163e9..df9ed611a47 100644 --- a/litellm/proxy/spend_tracking/spend_tracking_utils.py +++ b/litellm/proxy/spend_tracking/spend_tracking_utils.py @@ -80,7 +80,7 @@ from litellm.types.passthrough_endpoints.pass_through_endpoints import ( SETTLES_BACKGROUND_RESPONSE_KWARG, BackgroundResponseSettlement, ) -from litellm.types.router import DeploymentTypedDict, LiteLLM_Params +from litellm.types.router import DeploymentTypedDict, LiteLLM_Params, SilentExperimentFlag from litellm.types.utils import ( BACKGROUND_RESPONSE_COST_POLL_CALL_ORIGIN, PROMPT_CARRYING_GUARDRAIL_FIELDS, @@ -194,6 +194,7 @@ _STAMPED_METADATA_KEYS: Final = frozenset( "used_client_oauth_token", KUBERNETES_POD_ROUTING_KEY, "litellm_roi_estimator", + "is_silent_experiment", BILLED_RETRY_ATTEMPT_METADATA_KEY, "user_api_key_user_email", ) @@ -227,6 +228,7 @@ def _get_spend_logs_metadata( azure_spillover: AzureSpillover | None = None, used_client_oauth_token: bool | None = None, kubernetes_pod_routing: StandardLoggingKubernetesPodRouting | None = None, + is_silent_experiment: bool = False, ) -> SpendLogsMetadata: if metadata is None: return SpendLogsMetadata( @@ -294,6 +296,7 @@ def _get_spend_logs_metadata( used_client_oauth_token=used_client_oauth_token, kubernetes_pod_routing=kubernetes_pod_routing, litellm_roi_estimator=metadata.get("litellm_roi_estimator") is True, + **(SilentExperimentFlag(is_silent_experiment=True) if is_silent_experiment else SilentExperimentFlag()), billed_retry_attempt=billed_retry_attempt, user_api_key_user_email=_stored_user_email(metadata), ) @@ -461,7 +464,11 @@ def _sl_attribution_fallback( ) -> str: if standard_logging_payload is None: return "" - return standard_logging_payload.get(field) or "" + return _string_or_empty(standard_logging_payload.get(field)) + + +def _string_or_empty(value: object) -> str: + return value if isinstance(value, str) else "" def _deployment_provider(deployment: DeploymentTypedDict) -> str | None: @@ -679,7 +686,7 @@ def get_logging_payload( _model_id: Final = metadata.get("model_info", {}).get("id", "") or _sl_attribution_fallback( standard_logging_payload, "model_id" ) - _model_group: Final = metadata.get("model_group", "") or _sl_attribution_fallback( + _model_group: Final = _string_or_empty(metadata.get("model_group")) or _sl_attribution_fallback( standard_logging_payload, "model_group" ) _api_base: Final = litellm_params.get("api_base", "") or _sl_attribution_fallback( @@ -738,6 +745,11 @@ def get_logging_payload( # clean up litellm metadata clean_metadata = _get_spend_logs_metadata( persisted_metadata, + is_silent_experiment=( + standard_logging_payload is not None + and "is_silent_experiment" in standard_logging_payload["metadata"] + and standard_logging_payload["metadata"]["is_silent_experiment"] + ), applied_guardrails=( standard_logging_payload["metadata"].get("applied_guardrails", None) if standard_logging_payload is not None diff --git a/litellm/proxy/vector_store_endpoints/endpoints.py b/litellm/proxy/vector_store_endpoints/endpoints.py index 1b54dddcf4f..b6a0ebe3c58 100644 --- a/litellm/proxy/vector_store_endpoints/endpoints.py +++ b/litellm/proxy/vector_store_endpoints/endpoints.py @@ -17,6 +17,7 @@ from litellm.proxy._types import CommonProxyErrors, UserAPIKeyAuth from litellm.proxy.auth.user_api_key_auth import user_api_key_auth from litellm.proxy.common_request_processing import ProxyBaseLLMRequestProcessing from litellm.proxy.common_utils.http_parsing_utils import read_raw_json_body +from litellm.proxy.route_llm_request import JSON_OBJECT_ADAPTER from litellm.proxy.utils import jsonify_object from litellm.proxy.vector_store_endpoints.utils import ( assert_proxy_admin_for_vector_store_index_management, @@ -24,6 +25,7 @@ from litellm.proxy.vector_store_endpoints.utils import ( get_litellm_managed_vector_store, ) from litellm.repositories.table_repositories import ManagedVectorStoreIndexRepository +from litellm.types.utils import LlmProviders from litellm.types.vector_stores import IndexCreateRequest, IndexListResponse from litellm.vector_stores.utils import VectorStoreWriteBody, callers_metadata_snapshot from litellm.vector_stores.vector_store_registry import VectorStoreIndexRegistry @@ -52,6 +54,24 @@ def reject_caller_embedding_selection_params(payload: Mapping[str, object], sour ) +def reject_caller_opensearch_index_name( + caller_body: Mapping[str, object], request_fields: Mapping[str, object], source: str +) -> None: + provider: Final = request_fields.get("custom_llm_provider") + if ( + isinstance(provider, str) + and provider.split("/", 1)[0] == LlmProviders.OPENSEARCH.value + and "opensearch_index_name" in caller_body + ): + raise HTTPException( + status_code=400, + detail={ + "error": f"'opensearch_index_name' cannot be set in {source}. " + "The index comes from the vector store's server-side registration." + }, + ) + + ######################################################## # OpenAI Compatible Endpoints ######################################################## @@ -138,11 +158,17 @@ async def vector_store_search( data = await read_request_body(request=request) # rebind-ok: pre-existing rebinding on a rename-only line reject_caller_embedding_selection_params(payload=data, source="the search request body") data["vector_store_id"] = vector_store_id + caller_body: Final = JSON_OBJECT_ADAPTER.validate_python(data) # Check for legacy vector store registry (non-managed vector stores) data = await _update_request_data_with_litellm_managed_vector_store_registry( data=data, vector_store_id=vector_store_id, user_api_key_dict=user_api_key_dict ) + reject_caller_opensearch_index_name( + caller_body=caller_body, + request_fields=JSON_OBJECT_ADAPTER.validate_python(data), + source="the search request body", + ) # The managed_vector_stores pre-call hook will handle: # 1. Decoding managed vector store IDs diff --git a/litellm/realtime_api/README.md b/litellm/realtime_api/README.md index d810de2f24f..987e8be1ed1 100644 --- a/litellm/realtime_api/README.md +++ b/litellm/realtime_api/README.md @@ -4,6 +4,6 @@ Supported endpoints: - WebSocket: `/v1/realtime` (with `intent=transcription` for transcription-only sessions) - HTTP: `/v1/realtime/client_secrets`, `/v1/realtime/transcription_sessions` -Supported providers: OpenAI, Azure OpenAI, Bedrock, Vertex AI, xAI. +Supported providers: OpenAI, Azure OpenAI, Bedrock, Vertex AI, xAI, Gemini, Meta, Soniox. For user-facing documentation and usage examples, see the litellm-docs repo. \ No newline at end of file diff --git a/litellm/realtime_api/main.py b/litellm/realtime_api/main.py index e9b534bdd89..e03cb44efda 100644 --- a/litellm/realtime_api/main.py +++ b/litellm/realtime_api/main.py @@ -7,6 +7,7 @@ from dataclasses import dataclass from types import MappingProxyType from typing import TYPE_CHECKING, Any, Final, Literal, NoReturn, cast +from pydantic import TypeAdapter from typing_extensions import ReadOnly, TypedDict, assert_never import litellm @@ -34,8 +35,8 @@ from litellm.types.utils import CallTypes, LlmProviders from litellm.utils import ( ModelInfoLookupMiss, ProviderConfigManager, + aresolve_api_key_secret_reference, lookup_model_info_for_registration, - resolve_api_key_secret_reference, ) from ..litellm_core_utils.core_helpers import as_str_mapping, normalize_drop_params @@ -55,6 +56,7 @@ from ..llms.openai.realtime.handler import ( confirm_session_started, ) from ..llms.openai.workload_identity import openai_static_api_key, resolve_openai_bearer_token_async +from ..llms.soniox.realtime.transformation import SonioxRealtimeOptions, soniox_realtime_health_check from ..llms.vertex_ai.audio_transcription.realtime_transformation import is_vertex_speech_to_text_model from ..llms.vertex_ai.realtime.transformation import VertexAIRealtimeConfig, vertex_realtime_config from ..llms.vertex_ai.vertex_llm_base import VertexBase @@ -75,6 +77,7 @@ vertex_llm_base: Final = VertexBase() base_llm_http_handler = BaseLLMHTTPHandler() _EMPTY_MODEL_PARAMS: Final[Mapping[str, object]] = MappingProxyType({}) _EMPTY_AUTH_HEADERS: Final[Mapping[str, str]] = MappingProxyType({}) +_DEPLOYMENT_PARAMS: Final = TypeAdapter(Mapping[str, object]) def _model_params_with_stored_credentials(model_params: Mapping[str, object]) -> Mapping[str, object]: @@ -527,6 +530,7 @@ async def _arealtime( custom_llm_provider=_custom_llm_provider, ) + deployment_params: Final = _DEPLOYMENT_PARAMS.validate_python(kwargs) if live_session_start is not None: await _alive_session( model=model, @@ -537,7 +541,7 @@ async def _arealtime( api_base=dynamic_api_base or litellm_params.api_base, api_key=dynamic_api_key, litellm_metadata=_build_litellm_metadata(kwargs), - litellm_params=kwargs, + litellm_params=deployment_params, ) return @@ -548,6 +552,7 @@ async def _arealtime( model=model, provider=LlmProviders(_custom_llm_provider), drop_params=drop_params, + litellm_params=deployment_params, ) if provider_config is not None: await base_llm_http_handler.async_realtime( @@ -840,7 +845,7 @@ async def realtime_health_check( resolved_api_base: Final = cast( # cast-ok: provider parameters expose optional string endpoints str | None, api_base or resolved_params.get("api_base") ) - resolved_api_key: Final = resolve_api_key_secret_reference( + resolved_api_key: Final = await aresolve_api_key_secret_reference( cast( # cast-ok: provider parameters expose optional string credentials str | None, api_key or resolved_params.get("api_key") ), @@ -882,6 +887,13 @@ async def realtime_health_check( ) elif custom_llm_provider == "openai": url = openai_realtime.construct_url(api_base=openai_api_base, query_params={"model": model}) + elif custom_llm_provider == "soniox": + return await soniox_realtime_health_check( + model=model, + api_base=resolved_api_base, + api_key=health_check_api_key, + options=SonioxRealtimeOptions.model_validate(dict(resolved_params)), + ) elif custom_llm_provider == "xai": url = xai_realtime.construct_url( api_base=resolved_api_base or "https://api.x.ai/v1", query_params={"model": model} diff --git a/litellm/repositories/daily_activity_buckets.py b/litellm/repositories/daily_activity_buckets.py new file mode 100644 index 00000000000..ec6dd0f8c84 --- /dev/null +++ b/litellm/repositories/daily_activity_buckets.py @@ -0,0 +1,39 @@ +from collections.abc import Sequence +from typing import Final + +from litellm.constants import UNKNOWN_MODEL_SPEND_LOG_MODEL + +UNKNOWN_MODEL_GROUP: Final = "unknown-model-group" +UNATTRIBUTED_API_KEY: Final = "unattributed" +_RAW_API_KEY_PREFIX: Final = "raw:" + + +def model_bucket(model: str | None) -> str: + return model or UNKNOWN_MODEL_SPEND_LOG_MODEL + + +def model_group_bucket(model_group: str | None, model: str | None) -> str: + return model_group or model or UNKNOWN_MODEL_GROUP + + +def api_key_bucket(api_key: str | None) -> str: + if not api_key: + return UNATTRIBUTED_API_KEY + if api_key == UNATTRIBUTED_API_KEY or api_key.startswith(_RAW_API_KEY_PREFIX): + return _RAW_API_KEY_PREFIX + api_key + return api_key + + +def api_key_from_bucket(api_key: str) -> str: + if api_key == UNATTRIBUTED_API_KEY: + return "" + return api_key.removeprefix(_RAW_API_KEY_PREFIX) + + +def raw_api_key_filter(api_key: str | Sequence[str] | None) -> tuple[str, ...] | None: + """String filters are reporting IDs; sequences are raw keys resolved by authorization.""" + if api_key is None or api_key == "": + return None + if isinstance(api_key, str): + return (api_key_from_bucket(api_key),) + return tuple(api_key) diff --git a/litellm/repositories/daily_activity_repository.py b/litellm/repositories/daily_activity_repository.py index 4ca23e4e89e..321468c4976 100644 --- a/litellm/repositories/daily_activity_repository.py +++ b/litellm/repositories/daily_activity_repository.py @@ -10,6 +10,7 @@ from typing_extensions import assert_never from litellm import constants from litellm._logging import verbose_proxy_logger +from litellm.constants import UNKNOWN_MODEL_SPEND_LOG_MODEL from litellm.repositories.chunked_in import find_many_in from litellm.repositories.daily_activity_sql import ( ExportCursor, @@ -344,7 +345,13 @@ class DailyActivityRepository: "date": {"gte": adjusted_start, "lte": adjusted_end}, **({scope.entity_id_field: {"in": list(scope.entity_ids)}} if scope.entity_ids is not None else {}), **exclusion_filter, - **({"model": scope.model} if scope.model else {}), + **( + {"AND": [{"OR": [{"model": None}, {"model": ""}, {"model": scope.model}]}]} + if scope.model == UNKNOWN_MODEL_SPEND_LOG_MODEL + else {"model": scope.model} + if scope.model + else {} + ), **({"api_key": {"in": list(scope.api_keys)}} if scope.api_keys is not None else {}), } count, rows = await asyncio.gather( diff --git a/litellm/repositories/daily_activity_sql.py b/litellm/repositories/daily_activity_sql.py index 7ec9fa67a67..4bb52767de6 100644 --- a/litellm/repositories/daily_activity_sql.py +++ b/litellm/repositories/daily_activity_sql.py @@ -8,11 +8,18 @@ from typing import Final from typing_extensions import assert_never from litellm import constants -from litellm.constants import PTU_SENTINEL_API_KEY +from litellm.constants import PTU_SENTINEL_API_KEY, UNKNOWN_MODEL_SPEND_LOG_MODEL +from litellm.repositories.daily_activity_buckets import UNATTRIBUTED_API_KEY, UNKNOWN_MODEL_GROUP from litellm.types.repositories.daily_activity import DailyActivityScope, DailyActivityTable, ExportType _API_KEY_ROLLED_UP_BIT: Final = 32 -_MODEL_GROUP_EXPR: Final = "COALESCE(NULLIF(model_group, ''), model)" +_MODEL_EXPR: Final = f"COALESCE(NULLIF(model, ''), '{UNKNOWN_MODEL_SPEND_LOG_MODEL}')" +_MODEL_GROUP_EXPR: Final = f"COALESCE(NULLIF(model_group, ''), NULLIF(model, ''), '{UNKNOWN_MODEL_GROUP}')" + +_API_KEY_EXPR: Final = ( + f"CASE WHEN api_key = '' THEN '{UNATTRIBUTED_API_KEY}' " + f"WHEN api_key = '{UNATTRIBUTED_API_KEY}' OR api_key LIKE 'raw:%' THEN 'raw:' || api_key ELSE api_key END" +) @dataclass(frozen=True, slots=True) @@ -86,7 +93,11 @@ def build_where_clause(scope: DailyActivityScope, *, start_index: int = 1) -> tu if scope.exclude_entity_ids else () ), - *((f"model = ${model_index}",) if scope.model else ()), + *( + (f"{_MODEL_EXPR if scope.model == UNKNOWN_MODEL_SPEND_LOG_MODEL else 'model'} = ${model_index}",) + if scope.model + else () + ), *( ("FALSE",) if scope.api_keys == () @@ -143,7 +154,7 @@ def _top_api_keys_sql(pg_table: str, where_clause: str, *, sentinel_param: int, FROM "{pg_table}" WHERE {where_clause} AND api_key <> ${sentinel_param} GROUP BY api_key - ORDER BY SUM(spend::numeric) DESC, api_key + ORDER BY SUM(spend::numeric) DESC, {_API_KEY_EXPR} LIMIT ${limit_param} """ @@ -162,13 +173,13 @@ def build_aggregated_sql(scope: DailyActivityScope, *, api_key_limit: int) -> Sq (SELECT date, NULL::text AS api_key, - model, + {_MODEL_EXPR} AS model, {_MODEL_GROUP_EXPR} AS model_group, custom_llm_provider, mcp_namespaced_tool_name, endpoint, (GROUPING(date) << 6) | {_API_KEY_ROLLED_UP_BIT} - | GROUPING(model, {_MODEL_GROUP_EXPR}, + | GROUPING({_MODEL_EXPR}, {_MODEL_GROUP_EXPR}, custom_llm_provider, mcp_namespaced_tool_name, endpoint) AS group_level, NULL::bigint AS distinct_api_keys,{metric_select} @@ -176,7 +187,7 @@ def build_aggregated_sql(scope: DailyActivityScope, *, api_key_limit: int) -> Sq WHERE {where_clause} GROUP BY GROUPING SETS ( (date), - (date, model), + (date, {_MODEL_EXPR}), (date, {_MODEL_GROUP_EXPR}), (date, custom_llm_provider), (date, mcp_namespaced_tool_name), @@ -190,12 +201,12 @@ def build_aggregated_sql(scope: DailyActivityScope, *, api_key_limit: int) -> Sq SELECT date, api_key, - model, + {_MODEL_EXPR} AS model, {_MODEL_GROUP_EXPR} AS model_group, custom_llm_provider, mcp_namespaced_tool_name, endpoint, - GROUPING(date, api_key, model, {_MODEL_GROUP_EXPR}, + GROUPING(date, api_key, {_MODEL_EXPR}, {_MODEL_GROUP_EXPR}, custom_llm_provider, mcp_namespaced_tool_name, endpoint) AS group_level, MAX(top_api_keys.distinct_api_keys) AS distinct_api_keys,{metric_select} @@ -203,7 +214,7 @@ def build_aggregated_sql(scope: DailyActivityScope, *, api_key_limit: int) -> Sq WHERE {where_clause} GROUP BY GROUPING SETS ( (date, api_key), - (date, model, api_key), + (date, {_MODEL_EXPR}, api_key), (date, {_MODEL_GROUP_EXPR}, api_key), (date, custom_llm_provider, api_key), (date, mcp_namespaced_tool_name, api_key), @@ -313,7 +324,7 @@ def build_key_page_sql(scope: DailyActivityScope, *, offset: int, limit: int) -> FROM (SELECT 1) AS one LEFT JOIN LATERAL ( SELECT * FROM ranked - ORDER BY rank_spend DESC, api_key + ORDER BY rank_spend DESC, {_API_KEY_EXPR} LIMIT ${limit_param} OFFSET ${offset_param} ) AS page ON TRUE """ @@ -376,7 +387,7 @@ def build_key_search_sql(scope: DailyActivityScope, *, search: str, limit: int) WHERE {where_clause} AND api_key <> ${sentinel_param} AND ( - api_key ILIKE ${search_param} ESCAPE '\\' + {_API_KEY_EXPR} ILIKE ${search_param} ESCAPE '\\' OR api_key IN ( SELECT v.token FROM "LiteLLM_VerificationToken" v LEFT JOIN "LiteLLM_UserTable" u ON u.user_id = v.user_id @@ -392,7 +403,7 @@ def build_key_search_sql(scope: DailyActivityScope, *, search: str, limit: int) ) ) GROUP BY api_key - ORDER BY SUM(spend::numeric) DESC, api_key + ORDER BY SUM(spend::numeric) DESC, {_API_KEY_EXPR} LIMIT ${limit_param} """ return SqlQuery(sql=sql, params=(*where_params, f"%{escaped}%", PTU_SENTINEL_API_KEY, limit)) @@ -406,15 +417,13 @@ def build_model_top_keys_sql( model_param: Final = len(where_params) + 1 sentinel_param: Final = model_param + 1 limit_param: Final = sentinel_param + 1 - model_clause: Final = ( - f"COALESCE(NULLIF(model_group, ''), model) = ${model_param}" if by_model_group else f"model = ${model_param}" - ) + model_clause: Final = f"{_MODEL_GROUP_EXPR if by_model_group else _MODEL_EXPR} = ${model_param}" sql: Final = f""" SELECT api_key,{_key_spend_select()} FROM "{PRISMA_TO_PG_TABLE[scope.table]}" WHERE {where_clause} AND {model_clause} AND api_key <> ${sentinel_param} GROUP BY api_key - ORDER BY SUM(spend::numeric) DESC, api_key + ORDER BY SUM(spend::numeric) DESC, {_API_KEY_EXPR} LIMIT ${limit_param} """ return SqlQuery(sql=sql, params=(*where_params, model_group, PTU_SENTINEL_API_KEY, limit)) @@ -431,7 +440,7 @@ def build_cache_leakage_keys_sql(scope: DailyActivityScope, *, limit: int) -> Sq WHERE {where_clause} AND api_key <> ${sentinel_param} GROUP BY api_key HAVING SUM(prompt_tokens) - SUM(cache_read_input_tokens) > 0 - ORDER BY SUM(prompt_tokens) - SUM(cache_read_input_tokens) DESC, api_key + ORDER BY SUM(prompt_tokens) - SUM(cache_read_input_tokens) DESC, {_API_KEY_EXPR} LIMIT ${limit_param} """ return SqlQuery(sql=sql, params=(*where_params, PTU_SENTINEL_API_KEY, limit)) @@ -483,7 +492,7 @@ def build_export_sql( {alias_expression} AS entity_alias, {output_key} AS api_key, {user_fields}, - {"NULLIF(COALESCE(scoped.model, ''), '')" if export_type is ExportType.DAILY_WITH_MODELS else "NULL::text"} AS model, + {_MODEL_EXPR if export_type is ExportType.DAILY_WITH_MODELS else "NULL::text"} AS model, COALESCE(SUM(scoped.spend), 0)::float AS spend, {flat_cost} AS flat_cost, COALESCE(SUM(scoped.prompt_tokens), 0)::bigint AS prompt_tokens, @@ -521,25 +530,26 @@ def _export_grouping(export_type: ExportType) -> tuple[str, str, str, tuple[str, ) if export_type is ExportType.DAILY_WITH_KEYS: return ( - "scoped.api_key", - "NULLIF(scoped.api_key, '')", - "MAX(COALESCE(vt.key_alias, dvt.key_alias)) AS key_alias, " + _API_KEY_EXPR, + _API_KEY_EXPR, + "MAX(CASE WHEN scoped.api_key = '' THEN 'Unattributed' " + "ELSE COALESCE(vt.key_alias, dvt.key_alias) END) AS key_alias, " "MAX(COALESCE(vt.user_id, dvt.user_id)) AS user_id, MAX(u.user_email) AS user_email", ( - 'LEFT JOIN "LiteLLM_VerificationToken" vt ON vt.token = scoped.api_key', + "LEFT JOIN \"LiteLLM_VerificationToken\" vt ON vt.token = scoped.api_key AND scoped.api_key <> ''", """LEFT JOIN LATERAL ( SELECT key_alias, user_id FROM "LiteLLM_DeletedVerificationToken" WHERE token = scoped.api_key ORDER BY deleted_at DESC LIMIT 1 - ) dvt ON vt.token IS NULL""", + ) dvt ON vt.token IS NULL AND scoped.api_key <> ''""", 'LEFT JOIN "LiteLLM_UserTable" u ON u.user_id = COALESCE(vt.user_id, dvt.user_id)', ), ) if export_type is ExportType.DAILY_WITH_MODELS: return ( - "COALESCE(scoped.model, '')", + _MODEL_EXPR, "NULL::text", "NULL::text AS key_alias, NULL::text AS user_id, NULL::text AS user_email", (), diff --git a/litellm/rerank_api/main.py b/litellm/rerank_api/main.py index 65d9b89cf79..e344ce46be7 100644 --- a/litellm/rerank_api/main.py +++ b/litellm/rerank_api/main.py @@ -19,6 +19,12 @@ from litellm.types.rerank import RerankResponse from litellm.types.router import * from litellm.utils import ProviderConfigManager, client, exception_type +_PROVIDERS_RESOLVING_THEIR_OWN_RERANK_BASE: Final = ( + litellm.LlmProviders.DASHSCOPE, + litellm.LlmProviders.QWENCLOUD, + litellm.LlmProviders.QWEN_AI_PLATFORM, +) + ####### ENVIRONMENT VARIABLES ################### # Initialize any necessary instances or variables here together_rerank: Final = TogetherAIRerank() @@ -543,7 +549,11 @@ def rerank( logging_obj=litellm_logging_obj, timeout=optional_params.timeout, api_key=dynamic_api_key or optional_params.api_key, - api_base=dynamic_api_base or optional_params.api_base, + api_base=( + optional_params.api_base or dynamic_api_base + if _custom_llm_provider in _PROVIDERS_RESOLVING_THEIR_OWN_RERANK_BASE + else dynamic_api_base or optional_params.api_base + ), _is_async=_is_async, headers=headers or litellm.headers or {}, client=client, diff --git a/litellm/responses/main.py b/litellm/responses/main.py index 5bed6037fa4..98356e9a5d4 100644 --- a/litellm/responses/main.py +++ b/litellm/responses/main.py @@ -90,9 +90,9 @@ from litellm.types.responses.streaming_websocket import ResponsesWebSocketReques from litellm.types.router import GenericLiteLLMParams from litellm.types.utils import ( ADDRESSED_RESPONSE_ID_FIELD, + AgenticLoopParams, CallTypes, CostPerToken, - CustomPricingLiteLLMParams, all_litellm_params, ) from litellm.utils import ( @@ -565,12 +565,6 @@ async def aresponses_api_with_mcp( return response -def _per_call_custom_pricing(litellm_params: GenericLiteLLMParams) -> Mapping[str, float]: - return MappingProxyType( - litellm_params.model_dump(include=set(CustomPricingLiteLLMParams.model_fields), exclude_none=True) - ) - - class _DeploymentModelInfo(BaseModel): model_config = ConfigDict(frozen=True) id: object = None @@ -1580,6 +1574,7 @@ def responses( local_vars=local_vars, use_chat_completions_api=use_chat_completions_api, ) + litellm_logging_obj.record_agentic_loop_params(AgenticLoopParams(model=model)) from litellm.litellm_core_utils.core_helpers import attribute_or_none @@ -1798,7 +1793,7 @@ def responses( optional_params=dict(responses_api_request_params), litellm_params={ **responses_api_request_params, - **_per_call_custom_pricing(litellm_params), + **litellm_params.custom_pricing_overrides(), "aresponses": _is_async, "litellm_call_id": litellm_call_id, "model_info": kwargs.get("model_info"), diff --git a/litellm/responses/streaming_iterator.py b/litellm/responses/streaming_iterator.py index 8959c32fff2..6a82304b76a 100644 --- a/litellm/responses/streaming_iterator.py +++ b/litellm/responses/streaming_iterator.py @@ -227,7 +227,7 @@ async def drain_logging_tasks(timeout: float = 5.0) -> None: return _, pending = await asyncio.wait(tuple(_PENDING_LOGGING_TASKS), timeout=timeout) if pending: - verbose_logger.warning("Timed out draining %d logging tasks on shutdown, dropping them", len(pending)) + verbose_logger.warning("Timed out draining logging tasks on shutdown: %d tasks still alive", len(pending)) _ERROR_CODE_HTTP_STATUS: Final[Mapping[str, int]] = MappingProxyType( diff --git a/litellm/responses/utils.py b/litellm/responses/utils.py index 05d6a36c1f6..b0db0516658 100644 --- a/litellm/responses/utils.py +++ b/litellm/responses/utils.py @@ -131,14 +131,14 @@ def _provider_mapped_params( params: ResponsesAPIOptionalRequestParams, model: str, drop_params: bool, - declared_reasoning_efforts: Collection[str], + declared_reasoning_efforts: Collection[str] | None, ) -> Mapping[str, object]: from litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfig if ( - declared_reasoning_efforts + declared_reasoning_efforts is not None and isinstance(config, OpenAIResponsesAPIConfig) - and type(config).map_openai_params is OpenAIResponsesAPIConfig.map_openai_params + and config.honors_declared_reasoning_efforts() ): return config.map_openai_params_for_deployment( params, model, drop_params, declared_reasoning_efforts=declared_reasoning_efforts @@ -287,7 +287,7 @@ class ResponsesAPIRequestUtils: response_api_optional_params: ResponsesAPIOptionalRequestParams, allowed_openai_params: list[str] | None = None, drop_params: bool | None = None, - declared_reasoning_efforts: Collection[str] = (), + declared_reasoning_efforts: Collection[str] | None = None, ) -> dict: """ Get optional parameters for the responses API. diff --git a/litellm/router.py b/litellm/router.py index e25d6dfab3d..139327e7c5a 100644 --- a/litellm/router.py +++ b/litellm/router.py @@ -34,7 +34,6 @@ import asyncio import contextlib import copy import enum -import hashlib import inspect import json import logging @@ -75,7 +74,7 @@ from typing_extensions import assert_never, overload import litellm import litellm.litellm_core_utils.exception_mapping_utils from litellm import get_secret_str -from litellm._internal_context import service_target, with_service_target +from litellm._internal_context import in_mirrored_call, mirrored_call, service_target, with_service_target from litellm._logging import verbose_router_logger from litellm._uuid import uuid from litellm.caching.caching import ( @@ -185,6 +184,20 @@ from litellm.llms.openai_like.model_info import ( ) from litellm.responses.bridge_check import keep_registered_mode from litellm.responses.retrieve_stream_query import requests_streamed_retrieval, without_retrieve_stream_defaults +from litellm.router_api import ( + SETTING_FIELDS, + ApplyResult, + ChangeSet, + RouterHealth, + RouterRegistry, + RouterSettings, + RoutingObservers, + SelectKind, + aapply_changes, + apply_changes, +) +from litellm.router_api import deployment_id as _deployment_id +from litellm.router_deprecated import DeprecatedRouterMembers from litellm.router_strategy.auto_router.auto_router_model_naming import ( AUTO_ROUTER_MODEL_PREFIX, STRATEGY_ROUTER_PARAM_FIELDS, @@ -287,6 +300,11 @@ from litellm.router_utils.embedding_batch_size_check import ( embedding_deployment_selection, log_rejection_before_provider_call, ) +from litellm.router_utils.endpoint_eligibility import ( + eligible_deployments, + endpoint_selection, + warn_on_unenforced_endpoint_flag, +) from litellm.router_utils.fallback_event_handlers import ( MID_STREAM_FALLBACK_CONTROLS_KEY, AttemptedFallbackTargets, @@ -482,6 +500,7 @@ from litellm.utils import ( ProviderConfigManager, Rules, TextCompletionResponse, + aresolve_api_key_secret_reference, function_setup, get_llm_provider, get_non_default_completion_params, @@ -492,7 +511,6 @@ from litellm.utils import ( lookup_model_info_for_registration, model_info_with_pricing_overrides, provider_rejectable_params, - resolve_api_key_secret_reference, set_live_deployment_replay, token_counter, ) @@ -506,6 +524,7 @@ if TYPE_CHECKING: from litellm.responses.streaming_iterator import ( BaseResponsesAPIStreamingIterator, ) + from litellm.router_api import UntypedDict from litellm.router_strategy.adaptive_router.adaptive_router import ( AdaptiveRouter, ) @@ -754,7 +773,7 @@ def _every_deployment_refused( def _silent_experiment_targets(silent_model: object) -> tuple[str, ...]: - if silent_model is None: + if silent_model is None or in_mirrored_call(): return () try: targets: Final = _SILENT_MODEL_ADAPTER.validate_python(silent_model) @@ -1259,17 +1278,162 @@ def _base_model_widened_window( return MappingProxyType({"max_input_tokens": base_max_input_tokens}) -class Router: - @property - def _routing_groups(self) -> dict[str, RoutingGroup]: - return self.routing_groups +_SETTING_ATTRIBUTES: Final = frozenset("_core_" + name for name in SETTING_FIELDS) - @_routing_groups.setter - def _routing_groups(self, value: dict[str, RoutingGroup]) -> None: - self.routing_groups = value - model_names: set[str] = set() - cache_responses: bool | None = False +class Router(DeprecatedRouterMembers): + _core_model_names: set[str] = set() + _core_cache_responses: bool | None = False + _registry_version: int = 0 + + # -- entry points: everything outside code needs besides the endpoint methods --------------- + # See litellm/router_api.py. The members outside code used before these live in + # DeprecatedRouterMembers (litellm/router_deprecated.py). + + def registry(self) -> RouterRegistry: + """Deployment lookups: by id, group, alias, wildcard and team, plus configured plugins.""" + cached: Final = self.__dict__.get("_registry_view") + if isinstance(cached, RouterRegistry): + return cached + view: Final = RouterRegistry(self) + self.__dict__["_registry_view"] = view + return view + + def settings(self) -> RouterSettings: + """ + A frozen snapshot of the router's configuration. It is built once and reused until a + setting attribute is rebound or deleted (``__setattr__``/``__delattr__`` drop it), so the + request path can read it cheaply. + """ + cached: Final = self.__dict__.get("_settings_snapshot") + if isinstance(cached, RouterSettings): + return cached + snapshot: Final = RouterSettings.of(self) + self.__dict__["_settings_snapshot"] = snapshot + return snapshot + + def __setattr__(self, name: str, value: object) -> None: + if name in _SETTING_ATTRIBUTES: + self.__dict__.pop("_settings_snapshot", None) + elif name == "_core_model_list" and "_core_model_list" in self.__dict__: + # A whole new deployment list (``router.model_list = ...``) is a deployment change too. + self.__dict__["_registry_version"] = self._registry_version + 1 + super().__setattr__(name, value) + + def __delattr__(self, name: str) -> None: + if name in _SETTING_ATTRIBUTES: + self.__dict__.pop("_settings_snapshot", None) + super().__delattr__(name) + + def health(self) -> RouterHealth: + """Cooldowns, blocked deployments, health checks and usage.""" + cached: Final = self.__dict__.get("_health_view") + if isinstance(cached, RouterHealth): + return cached + view: Final = RouterHealth(self) + self.__dict__["_health_view"] = view + return view + + @overload + async def select( + self, + model: str, + request_kwargs: dict[str, object] | None = None, # mutable-ok: the selection cores take a dict + *, + kind: Literal["pick", "pass_through"] = "pick", + messages: list[dict[str, str]] | None = None, # mutable-ok: the selection cores take a list + input: str | list[object] | None = None, # mutable-ok: the selection cores take a list + specific_deployment: bool | None = False, + parent_otel_span: Span | None = None, + health_check_probe: bool = False, + ) -> "UntypedDict": ... # a mutable dict, as before: the cores' untyped deployment dict, unchanged + + @overload + async def select( + self, + model: str, + request_kwargs: dict[str, object] | None = None, # mutable-ok: the selection cores take a dict + *, + kind: Literal["healthy"], + messages: list[dict[str, str]] | None = None, # mutable-ok: the selection cores take a list + input: str | list[object] | None = None, # mutable-ok: the selection cores take a list + specific_deployment: bool | None = False, + parent_otel_span: Span | None = None, + health_check_probe: bool = False, + ) -> "list[UntypedDict] | UntypedDict": ... # mutable-ok: the core's type (list[dict] | dict), unchanged + + async def select( # pyright: ignore[reportUnknownParameterType] # the selection cores return untyped deployment dicts + self, + model: str, + request_kwargs: dict[str, object] | None = None, # mutable-ok: the selection cores take a dict + *, + kind: SelectKind = "pick", + messages: list[dict[str, str]] | None = None, # mutable-ok: the selection cores take a list + input: str | list[object] | None = None, # mutable-ok: the selection cores take a list + specific_deployment: bool | None = False, + parent_otel_span: Span | None = None, + health_check_probe: bool = False, + ): + """ + Pick a deployment for ``model`` without calling it (``pick``), list the deployments that + are healthy for the request (``healthy``), or pick one for a pass-through route + (``pass_through``). + """ + if kind == "pick": + return await self._core_async_get_available_deployment( # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType] # the selection cores return untyped deployment dicts + model=model, + request_kwargs=request_kwargs if request_kwargs is not None else {}, + messages=messages, + input=input, + specific_deployment=specific_deployment, + ) + if kind == "healthy": + return await self._core_async_get_healthy_deployments( # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType] # the selection cores return untyped deployment dicts + model=model, + request_kwargs=request_kwargs if request_kwargs is not None else {}, + messages=messages, + input=input, + specific_deployment=specific_deployment, + parent_otel_span=parent_otel_span, + health_check_probe=health_check_probe, + ) + if kind == "pass_through": + return self._core_get_available_deployment_for_pass_through( # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType] # the selection cores return untyped deployment dicts + model=model, + messages=messages, + input=input, + specific_deployment=specific_deployment, + request_kwargs=request_kwargs, + ) + raise ValueError(f"unknown select kind {kind!r}") + + def apply(self, changes: ChangeSet) -> ApplyResult: + """Apply runtime changes: settings, deployments, the routing state's Redis cache.""" + return apply_changes(self, changes) + + async def aapply(self, changes: ChangeSet) -> ApplyResult: + """``apply``, plus the changes that need I/O (``refresh_model_info``).""" + return await aapply_changes(self, changes) + + def observe(self, logging_obj: "LiteLLMLogging | None" = None, *, request_scoped: bool = False) -> RoutingObservers: + """ + The router's routing observers. ``request_scoped=True`` stops registering them globally, + so each request binds them instead; passing a request's ``logging_obj`` binds them to it. + """ + if request_scoped: + self._core_serve_through_request_logging() + if logging_obj is not None: + self._core_bind_to_request_logging(logging_obj) + return RoutingObservers(slack_alerting_logger=getattr(self, "_core_slack_alerting_logger", None)) + + def register_plugin(self, plugin: CustomRoutingStrategyBase) -> None: + """Route with a custom strategy instead of the configured one.""" + self._core_set_custom_routing_strategy(plugin) + + def retire(self) -> None: + """Release the router's global callback registrations and routing state.""" + self._core_discard() + default_cache_time_seconds: int = 1 * 60 * 60 # 1 hour tenacity = None leastbusy_logger: LeastBusyLoggingHandler | None = None @@ -1428,13 +1592,13 @@ class Router: self.set_verbose = set_verbose self.ignore_invalid_deployments = ignore_invalid_deployments self.auto_router_capability_limit = auto_router_capability_limit - self.fallback_access_check: Final = fallback_access_check - self.fallback_budget_check: Final = fallback_budget_check + self._core_fallback_access_check: Final = fallback_access_check + self._core_fallback_budget_check: Final = fallback_budget_check self.debug_level = debug_level - self.enable_pre_call_checks = enable_pre_call_checks - self.enable_tag_filtering = enable_tag_filtering - self.tag_filtering_match_any = tag_filtering_match_any - self.tag_routing_prefix = tag_routing_prefix + self._core_enable_pre_call_checks = enable_pre_call_checks + self._core_enable_tag_filtering = enable_tag_filtering + self._core_tag_filtering_match_any = tag_filtering_match_any + self._core_tag_routing_prefix = tag_routing_prefix from litellm._service_logger import ServiceLogging self.service_logger_obj: ServiceLogging = ServiceLogging() @@ -1444,20 +1608,20 @@ class Router: verbose_router_logger.setLevel(logging.INFO) elif debug_level == "DEBUG": verbose_router_logger.setLevel(logging.DEBUG) - self.router_general_settings: RouterGeneralSettings = router_general_settings or RouterGeneralSettings() + self._core_router_general_settings: RouterGeneralSettings = router_general_settings or RouterGeneralSettings() self.assistants_config = assistants_config - self.search_tools = search_tools or [] - self.guardrail_list = guardrail_list or [] - self.deployment_names: list = [] # names of models under litellm_params. ex. azure/chatgpt-v-2 - self.deployment_latency_map = {} + self._core_search_tools = search_tools or [] + self._core_guardrail_list = guardrail_list or [] + self._core_deployment_names: list[str] = [] # names of models under litellm_params. ex. azure/chatgpt-v-2 + self._core_deployment_latency_map = {} ### CACHING ### cache_type: Literal["local", "redis", "redis-semantic", "s3", "disk"] = "local" # default to an in-memory cache redis_cache = None cache_config: Final[dict[str, Any]] = {} self.client_ttl = client_ttl - self.kubernetes_pod_discovery: Final = KubernetesPodDiscovery() + self._core_kubernetes_pod_discovery: Final = KubernetesPodDiscovery() if redis_url is not None or (redis_host is not None and redis_port is not None): cache_type = "redis" @@ -1487,15 +1651,15 @@ class Router: if litellm.cache is None: # the cache can be initialized on the proxy server. We should not overwrite it litellm.cache = litellm.Cache(type=cache_type, **cache_config) - self.cache_responses = cache_responses - self.cache = DualCache( + self._core_cache_responses = cache_responses + self._core_cache = DualCache( redis_cache=redis_cache, in_memory_cache=InMemoryCache() ) # use a dual cache (Redis+In-Memory) for tracking cooldowns, usage, etc. self._claude_code_session_router_cache: DualCache = DualCache( redis_cache=redis_cache, in_memory_cache=InMemoryCache(), ) - self.prompt_caching_pin_cache: DualCache = DualCache( + self._core_prompt_caching_pin_cache: DualCache = DualCache( redis_cache=redis_cache, in_memory_cache=InMemoryCache(max_size_in_memory=PROMPT_CACHE_PIN_CACHE_MAX_SIZE), ) @@ -1511,21 +1675,21 @@ class Router: self.default_priority = ( None if isinstance(parsed_default_priority, InvalidPriority) else parsed_default_priority ) - self.default_deployment = ( + self._core_default_deployment = ( None # use this to track the users default deployment, when they want to use model = * ) - self.default_max_parallel_requests = default_max_parallel_requests - self.provider_default_deployment_ids: list[str] = [] - self.pattern_router = PatternMatchRouter() - self.team_pattern_routers: dict[str, PatternMatchRouter] = {} # {"TEAM_ID": PatternMatchRouter} - self.auto_routers: dict[str, list[TaggedPreRoutingStrategy[AutoRouter]]] = {} - self.complexity_routers: dict[str, list[TaggedPreRoutingStrategy[ComplexityRouter]]] = {} - self.adaptive_routers: dict[str, list[TaggedPreRoutingStrategy[AdaptiveRouter]]] = {} - self.quality_routers: dict[str, list[TaggedPreRoutingStrategy[QualityRouter]]] = {} - self.routing_plugins: list[RoutingPlugin] = list(plugins) if plugins else [] + self._core_default_max_parallel_requests = default_max_parallel_requests + self._core_provider_default_deployment_ids: list[str] = [] + self._core_pattern_router = PatternMatchRouter() + self._core_team_pattern_routers: dict[str, PatternMatchRouter] = {} # {"TEAM_ID": PatternMatchRouter} + self._core_auto_routers: dict[str, list[TaggedPreRoutingStrategy[AutoRouter]]] = {} + self._core_complexity_routers: dict[str, list[TaggedPreRoutingStrategy[ComplexityRouter]]] = {} + self._core_adaptive_routers: dict[str, list[TaggedPreRoutingStrategy[AdaptiveRouter]]] = {} + self._core_quality_routers: dict[str, list[TaggedPreRoutingStrategy[QualityRouter]]] = {} + self._core_routing_plugins: list[RoutingPlugin] = list(plugins) if plugins else [] # Initialize model_group_alias early since it's used in set_model_list - self.model_group_alias: dict[str, str | RouterModelGroupAliasItem] = ( + self._core_model_group_alias: dict[str, str | RouterModelGroupAliasItem] = ( model_group_alias or {} ) # dict to store aliases for router, ex. {"gpt-4": "gpt-3.5-turbo"}, all requests with gpt-4 -> get routed to gpt-3.5-turbo group @@ -1533,10 +1697,10 @@ class Router: self.model_id_to_deployment_index_map: dict[str, int] = {} # Initialize model name to deployment indices mapping for O(1) lookups # Maps model_name -> list of indices in model_list - self.model_name_to_deployment_indices: dict[str, list[int]] = {} + self._core_model_name_to_deployment_indices: dict[str, list[int]] = {} # Maps (team_id, team_public_model_name) -> list of indices in model_list - self.team_model_to_deployment_indices: dict[tuple[str, str], list[int]] = {} - self.team_public_model_names: frozenset[str] = frozenset() + self._core_team_model_to_deployment_indices: dict[tuple[str, str], list[int]] = {} + self._core_team_public_model_names: frozenset[str] = frozenset() # Guards model_list and the three index maps above: writers change them, and readers turn an # index into a deployment, under it, so a lookup never lands between a removal and its re-index self._deployment_index_lock = threading.RLock() @@ -1553,8 +1717,8 @@ class Router: # ``id()``-reuse risk after GC). See # ``litellm.proxy.auth.auth_checks._is_model_cost_zero``. self._zero_cost_cache: dict[str, bool] = {} - self.cached_deployment_model_info = lru_cache(maxsize=DEFAULT_MAX_LRU_CACHE_SIZE)( - self.get_deployment_model_info + self._core_cached_deployment_model_info = lru_cache(maxsize=DEFAULT_MAX_LRU_CACHE_SIZE)( + self._core_get_deployment_model_info ) self._ptu_share_teams = lru_cache(maxsize=1)(self._collect_ptu_share_teams) self._cached_team_ptu_ceiling = lru_cache(maxsize=DEFAULT_MAX_LRU_CACHE_SIZE)(self._resolve_team_ptu_ceiling) @@ -1578,31 +1742,33 @@ class Router: if model_list is not None: # set_model_list will build indices automatically self.set_model_list(model_list) - self.healthy_deployments: list = self.model_list + self.healthy_deployments: list = self._core_model_list for m in model_list: if "model" in m["litellm_params"]: - self.deployment_latency_map[m["litellm_params"]["model"]] = 0 + self._core_deployment_latency_map[m["litellm_params"]["model"]] = 0 else: - self.model_list: list = [] # initialize an empty list - to allow _add_deployment and delete_deployment to work + self._core_model_list: list = [] # initialize an empty list - to allow _add_deployment and delete_deployment to work # Track this router so a price data reload can rebuild its deployments' # cost-map entries from the list it is serving at that moment, whether # those deployments came from model_list or were added later from the DB. _live_routers.add(self) if allowed_fails is not None: - self.allowed_fails = allowed_fails + self._core_allowed_fails = allowed_fails else: - self.allowed_fails = litellm.allowed_fails + self._core_allowed_fails = litellm.allowed_fails self.explicit_allowed_fails = allowed_fails - self.cooldown_time = cooldown_time or DEFAULT_COOLDOWN_TIME_SECONDS - self.cooldown_cache = CooldownCache(cache=self.cache, default_cooldown_time=self.cooldown_time) - self.disable_cooldowns = disable_cooldowns + self._core_cooldown_time = cooldown_time or DEFAULT_COOLDOWN_TIME_SECONDS + self._core_cooldown_cache = CooldownCache( + cache=self._core_cache, default_cooldown_time=self._core_cooldown_time + ) + self._core_disable_cooldowns = disable_cooldowns self.fallback_on_unsupported_params = fallback_on_unsupported_params - self.enable_health_check_routing = enable_health_check_routing + self._core_enable_health_check_routing = enable_health_check_routing self.enable_weighted_failover = enable_weighted_failover self.enable_mid_stream_fallback_continuation = self._is_flag_enabled(enable_mid_stream_fallback_continuation) - self.health_check_ignore_transient_errors = health_check_ignore_transient_errors - self.background_health_check_model_groups: frozenset[str] | None = ( + self._core_health_check_ignore_transient_errors = health_check_ignore_transient_errors + self._core_background_health_check_model_groups: frozenset[str] | None = ( frozenset(background_health_check_model_groups) if background_health_check_model_groups is not None else None @@ -1610,7 +1776,9 @@ class Router: _staleness: Final = health_check_staleness_threshold or ( DEFAULT_HEALTH_CHECK_INTERVAL * DEFAULT_HEALTH_CHECK_STALENESS_MULTIPLIER ) - self.health_state_cache = DeploymentHealthCache(cache=self.cache, staleness_threshold=float(_staleness)) + self._core_health_state_cache = DeploymentHealthCache( + cache=self._core_cache, staleness_threshold=float(_staleness) + ) if num_retries is not None: self.num_retries = num_retries @@ -1635,7 +1803,7 @@ class Router: self.stream_timeout = stream_timeout self.retry_after = retry_after - self.routing_strategy = self._normalize_strategy(routing_strategy) + self._core_routing_strategy = self._normalize_strategy(routing_strategy) self._routing_groups_input: list[RoutingGroup | dict] | None = routing_groups ## SETTING FALLBACKS ## @@ -1644,7 +1812,7 @@ class Router: self.validate_fallbacks(fallback_param=_fallbacks) ### set fallbacks - self.fallbacks = _fallbacks + self._core_fallbacks = _fallbacks if default_fallbacks is not None or litellm.default_fallbacks is not None: _fallbacks = default_fallbacks or litellm.default_fallbacks @@ -1652,16 +1820,16 @@ class Router: raise TypeError( f"default_fallbacks must be a list of model names, got '{_fallbacks}'. Use ['{_fallbacks}']" ) - if self.fallbacks is not None: - self.fallbacks.append({"*": _fallbacks}) + if self._core_fallbacks is not None: + self._core_fallbacks.append({"*": _fallbacks}) else: - self.fallbacks = [{"*": _fallbacks}] + self._core_fallbacks = [{"*": _fallbacks}] - self.context_window_fallbacks = context_window_fallbacks or litellm.context_window_fallbacks + self._core_context_window_fallbacks = context_window_fallbacks or litellm.context_window_fallbacks _content_policy_fallbacks: Final = content_policy_fallbacks or litellm.content_policy_fallbacks self.validate_fallbacks(fallback_param=_content_policy_fallbacks) - self.content_policy_fallbacks = _content_policy_fallbacks + self._core_content_policy_fallbacks = _content_policy_fallbacks self.total_calls: defaultdict = defaultdict(int) # dict to store total calls made to each model self.fail_calls: defaultdict = defaultdict(int) # dict to store fail_calls made to each model self.success_calls: defaultdict = defaultdict(int) # dict to store success_calls made to each model @@ -1671,10 +1839,10 @@ class Router: self.chat = litellm.Chat(params=default_litellm_params, router_obj=self) # default litellm args - self.default_litellm_params = default_litellm_params - self.default_litellm_params.setdefault("timeout", timeout) - self.default_litellm_params.setdefault("max_retries", 0) - self.default_litellm_params.setdefault("metadata", {}).update({"caching_groups": caching_groups}) + self._core_default_litellm_params = default_litellm_params + self._core_default_litellm_params.setdefault("timeout", timeout) + self._core_default_litellm_params.setdefault("max_retries", 0) + self._core_default_litellm_params.setdefault("metadata", {}).update({"caching_groups": caching_groups}) self.deployment_stats: dict = {} # used for debugging load balancing """ @@ -1722,10 +1890,10 @@ class Router: else: litellm.failure_callback = [self.deployment_callback_on_failure] self.routing_strategy_args = routing_strategy_args - self.provider_budget_config = provider_budget_config + self._core_provider_budget_config = provider_budget_config self.router_budget_logger: RouterBudgetLimiting | None = None if RouterBudgetLimiting.should_init_router_budget_limiter( - model_list=model_list, provider_budget_config=self.provider_budget_config + model_list=model_list, provider_budget_config=self._core_provider_budget_config ): if optional_pre_call_checks is not None: optional_pre_call_checks.append("router_budget_limiting") @@ -1745,17 +1913,17 @@ class Router: self.model_group_retry_policy: dict[str, RetryPolicy] | None = model_group_retry_policy - self.allowed_fails_policy: AllowedFailsPolicy | None = None + self._core_allowed_fails_policy: AllowedFailsPolicy | None = None if allowed_fails_policy is not None: if isinstance(allowed_fails_policy, dict): - self.allowed_fails_policy = AllowedFailsPolicy(**allowed_fails_policy) + self._core_allowed_fails_policy = AllowedFailsPolicy(**allowed_fails_policy) elif isinstance(allowed_fails_policy, AllowedFailsPolicy): - self.allowed_fails_policy = allowed_fails_policy + self._core_allowed_fails_policy = allowed_fails_policy - if self.allowed_fails_policy is not None: + if self._core_allowed_fails_policy is not None: verbose_router_logger.info( "\x1b[32mRouter Custom Allowed Fails Policy Set:\n%s\x1b[0m", - self.allowed_fails_policy.model_dump(exclude_none=True), + self._core_allowed_fails_policy.model_dump(exclude_none=True), ) self.alerting_config: AlertingConfig | None = alerting_config @@ -1800,10 +1968,10 @@ class Router: @staticmethod def _register_continuation_prefill_check() -> None: - # Process-global on purpose: Router.discard() must not drop a filter another router still needs. + # Process-global on purpose: Router._core_discard() must not drop a filter another router still needs. litellm.logging_callback_manager.add_litellm_callback(ContinuationPrefillDeploymentCheck()) - def discard(self): + def _core_discard(self) -> None: """ Pseudo-destructor to be invoked to clean up global data structures when router is no longer used. For now, unhook router's callbacks from all lists @@ -1837,16 +2005,16 @@ class Router: adaptive_keys: Final = frozenset( map(litellm.logging_callback_manager.get_custom_logger_key, adaptive_router_hooks) ) - adaptive_survivor: Final = next((router for router in survivors if router.adaptive_routers), None) + adaptive_survivor: Final = next((router for router in survivors if router._core_adaptive_routers), None) if adaptive_survivor is not None and vacated_keys & adaptive_keys: adaptive_survivor._sync_adaptive_router_hooks() - def serve_through_request_logging(self) -> None: + def _core_serve_through_request_logging(self) -> None: self._request_scoped_selectors = (*self._callback_selectors(), *self._request_adaptive_router_hooks()) self._request_scoped_policies = (*self._optional_loggers(), *self._slack_alerting_loggers()) self._release_global_callbacks() - def bind_to_request_logging(self, logging_obj: LiteLLMLogging) -> None: + def _core_bind_to_request_logging(self, logging_obj: LiteLLMLogging) -> None: logging_obj.add_dynamic_function_callbacks( async_success=self.deployment_callback_on_success, failure=self.deployment_callback_on_failure, @@ -1877,7 +2045,7 @@ class Router: return tuple( AdaptiveRouterPostCallHook(adaptive_router=tagged.strategy) - for tagged in chain.from_iterable(self.adaptive_routers.values()) + for tagged in chain.from_iterable(self._core_adaptive_routers.values()) ) def _routing_callbacks(self) -> Iterable[object]: @@ -1898,7 +2066,7 @@ class Router: strategy.retire() def _slack_alerting_loggers(self) -> tuple[CustomLogger, ...]: - slack_alerting_logger: Final = getattr(self, "slack_alerting_logger", None) + slack_alerting_logger: Final = getattr(self, "_core_slack_alerting_logger", None) return (slack_alerting_logger,) if isinstance(slack_alerting_logger, CustomLogger) else () def _adaptive_router_hooks(self) -> tuple[CustomLogger, ...]: @@ -1907,7 +2075,7 @@ class Router: ) own_strategy_ids: Final = frozenset( - id(tagged.strategy) for tagged in chain.from_iterable(self.adaptive_routers.values()) + id(tagged.strategy) for tagged in chain.from_iterable(self._core_adaptive_routers.values()) ) hooks: Final = litellm.logging_callback_manager.get_custom_loggers_for_type(AdaptiveRouterPostCallHook) return tuple( @@ -1940,7 +2108,7 @@ class Router: else: return RedisCache(**cache_config) - def update_redis_cache(self, cache: RedisCache) -> None: + def _core_update_redis_cache(self, cache: RedisCache) -> None: """ Update the redis cache for the router, if none set. @@ -1951,11 +2119,9 @@ class Router: ``` and caching to just work. """ - self.cache.attach_redis_cache(cache) + self._core_cache.attach_redis_cache(cache) self._claude_code_session_router_cache.attach_redis_cache(cache) - self.prompt_caching_pin_cache.attach_redis_cache(cache) - - _update_redis_cache = update_redis_cache + self._core_prompt_caching_pin_cache.attach_redis_cache(cache) # Maps a routing strategy string to the attribute on `self` that holds # the default group's strategy selector for that strategy. (The selectors @@ -1996,25 +2162,25 @@ class Router: selector: RouterStrategySelector | None = None match self._normalize_strategy(strategy): case RoutingStrategy.LEAST_BUSY.value: - selector = LeastBusyLoggingHandler(router_cache=self.cache) + selector = LeastBusyLoggingHandler(router_cache=self._core_cache) case RoutingStrategy.USAGE_BASED_ROUTING.value: selector = LowestTPMLoggingHandler( - router_cache=self.cache, + router_cache=self._core_cache, routing_args=routing_strategy_args, ) case RoutingStrategy.USAGE_BASED_ROUTING_V2.value: selector = LowestTPMLoggingHandler_v2( - router_cache=self.cache, + router_cache=self._core_cache, routing_args=routing_strategy_args, ) case RoutingStrategy.LATENCY_BASED.value: selector = LowestLatencyLoggingHandler( - router_cache=self.cache, + router_cache=self._core_cache, routing_args=routing_strategy_args, ) case RoutingStrategy.COST_BASED.value: selector = LowestCostLoggingHandler( - router_cache=self.cache, + router_cache=self._core_cache, routing_args={}, ) case _: @@ -2067,7 +2233,7 @@ class Router: Latency/usage state survives the rebuild: it lives in the shared router cache, not on the selector. """ - strategy: Final = self._normalize_strategy(self.routing_strategy) + strategy: Final = self._normalize_strategy(self._core_routing_strategy) if strategy == "lar1": from litellm.router_strategy.lar1_routing import apply_lar1_routing_strategy @@ -2141,10 +2307,12 @@ class Router: self._replace_routing_groups(()) return - known_model_names: Final = frozenset(m["model_name"] for m in (self.model_list or ()) if m.get("model_name")) + known_model_names: Final = frozenset( + m["model_name"] for m in (self._core_model_list or ()) if m.get("model_name") + ) groups: Final = parse_routing_groups(groups_input, known_model_names=known_model_names) - alias_names: Final = frozenset(self.model_group_alias or ()) + alias_names: Final = frozenset(self._core_model_group_alias or ()) for group in groups: if group.group_name in known_model_names or group.group_name in alias_names: if group.routing_strategy == "priority": @@ -2182,7 +2350,7 @@ class Router: if selector is not None: self._register_router_selector(selector) - self.routing_groups: dict[str, RoutingGroup] = {group.group_name: group for group, _ in built} + self._core_routing_groups: dict[str, RoutingGroup] = {group.group_name: group for group, _ in built} self._model_to_group: dict[str, str] = { model_name: group.group_name for group, _ in built @@ -2206,13 +2374,13 @@ class Router: models win over indirection); config-time collisions are rejected by `_init_routing_groups`. """ - if not self.routing_groups: + if not self._core_routing_groups: return None - group: Final = self.routing_groups.get(model_name) + group: Final = self._core_routing_groups.get(model_name) if ( group is None - or model_name in self.model_name_to_deployment_indices - or model_name in (self.model_group_alias or {}) + or model_name in self._core_model_name_to_deployment_indices + or model_name in (self._core_model_group_alias or {}) ): return None return group @@ -2231,7 +2399,7 @@ class Router: specific deployment > model id > model_group_alias > routing group > model_name > team/pattern/default fallbacks. """ - if not self.routing_groups: + if not self._core_routing_groups: return None routing_group: Final = self.get_routing_group(model) if routing_group is None: @@ -2239,15 +2407,15 @@ class Router: return [ apply_routing_group_priority(routing_group, member, deployment) for member in routing_group.models - for deployment in self.get_all_deployments(model_name=member, team_id=team_id) + for deployment in self._core_get_all_deployments(model_name=member, team_id=team_id) ] def _is_priority_routing_group(self, model: str) -> bool: - resolved: Final = self.get_model_from_alias(model=model) or model + resolved: Final = self._core_get_model_from_alias(model=model) or model group: Final = self.get_routing_group(resolved) return group is not None and group.routing_strategy == "priority" - def is_recognized_model(self, model: str) -> bool: + def _core_is_recognized_model(self, model: str) -> bool: """ Whether `model` names something this router serves directly: a deployment model_name, a deployment id, a `model_group_alias`, or a @@ -2256,13 +2424,13 @@ class Router: default-deployment, and deployment-name fallbacks stay caller policy. """ return ( - model in self.model_names - or self.has_model_id(model) - or (self.model_group_alias is not None and model in self.model_group_alias) + model in self._core_model_names + or self._core_has_model_id(model) + or (self._core_model_group_alias is not None and model in self._core_model_group_alias) or self.get_routing_group(model) is not None ) - def routing_group_has_alternatives(self, model_group: str | None) -> bool: + def _core_routing_group_has_alternatives(self, model_group: str | None) -> bool: """ True when `model_group` names a callable routing group whose member union spans more than one deployment. Cooldown handling passes the @@ -2273,14 +2441,14 @@ class Router: """ if model_group is None: return False - resolved: Final = self.get_model_from_alias(model=model_group) or model_group + resolved: Final = self._core_get_model_from_alias(model=model_group) or model_group group: Final = self.get_routing_group(resolved) if group is None: return False - return sum(len(self.model_name_to_deployment_indices.get(member) or ()) for member in group.models) > 1 + return sum(len(self._core_model_name_to_deployment_indices.get(member) or ()) for member in group.models) > 1 - def team_model_has_alternatives(self, deployment_id: str) -> bool: - deployment: Final = self.get_deployment(model_id=deployment_id) + def _core_team_model_has_alternatives(self, deployment_id: str) -> bool: + deployment: Final = self._core_get_deployment(model_id=deployment_id) if deployment is None: return False team_id: Final = deployment.model_info.team_id @@ -2328,7 +2496,7 @@ class Router: caches it for the router's lifetime so its usage/latency state persists across requests. """ - if strategy == self._normalize_strategy(self.routing_strategy): + if strategy == self._normalize_strategy(self._core_routing_strategy): attr: Final = self._DEFAULT_SELECTOR_ATTR_BY_STRATEGY.get(strategy) return getattr(self, attr, None) if attr is not None else None with self._override_selectors_lock: @@ -2374,14 +2542,14 @@ class Router: def _globally_registered_strategies(self) -> frozenset[str]: configured: Final = ( - self.routing_strategy, - *(group.routing_strategy for group in self.routing_groups.values()), + self._core_routing_strategy, + *(group.routing_strategy for group in self._core_routing_groups.values()), ) return frozenset( normalized for normalized in map(self._normalize_strategy, configured) if normalized is not None ) - def arm_routing_read_prefetch(self, model: str, request_kwargs: dict[str, object] | None = None) -> None: + def _core_arm_routing_read_prefetch(self, model: str, request_kwargs: dict[str, object] | None = None) -> None: """Declare the cooldown read (and, for usage-based routing, the usage read) that `async_get_available_deployment` will make for `model` on the request's Redis batch, so admission's flush carries it. A miss (alias, no batch) costs nothing: routing then reads as it always has.""" @@ -2392,7 +2560,7 @@ class Router: if strategy == "usage-based-routing-v2" and isinstance(selector, LowestTPMLoggingHandler_v2) else None ) - deployments: Final = self.get_model_list(model_name=model) + deployments: Final = self._core_get_model_list(model_name=model) if deployments: RoutingPrefetch.arm(self, usage_selector, deployments) except Exception as e: # noqa: BLE001 # a prefetch is an optimisation, never a reason to fail the request @@ -2417,7 +2585,7 @@ class Router: driven by the router's top-level `routing_strategy` / `routing_strategy_args`. - `self.routing_strategy` may be either a string or a `RoutingStrategy` + `self._core_routing_strategy` may be either a string or a `RoutingStrategy` enum member (the constructor accepts both), so it is normalized to a string here. Downstream call sites and `_select_deployment_*` arms compare against string literals. @@ -2429,20 +2597,20 @@ class Router: self._bind_override_selector_to_request(override, override_selector, request_kwargs) return override, override_selector - resolved_model: Final = self.get_model_from_alias(model=model) or model + resolved_model: Final = self._core_get_model_from_alias(model=model) or model group_name: Final = ( resolved_model if self.get_routing_group(resolved_model) is not None else self._model_to_group.get(resolved_model) ) if group_name is None: - strategy = self._normalize_strategy(self.routing_strategy) + strategy = self._normalize_strategy(self._core_routing_strategy) attr: Final = self._DEFAULT_SELECTOR_ATTR_BY_STRATEGY.get(strategy or "") selector = getattr(self, attr, None) if attr is not None else None verbose_router_logger.debug("routing_group=default model=%s strategy=%s", model, strategy) return strategy, selector - group: Final = self.routing_groups[group_name] + group: Final = self._core_routing_groups[group_name] if group.routing_strategy == "priority": return "simple-shuffle", None strategy = self._normalize_strategy(group.routing_strategy) @@ -2973,7 +3141,7 @@ class Router: if self.optional_callbacks is None: self.optional_callbacks = [] affinity_callback: Final = DeploymentAffinityCheck( - cache=self.cache, + cache=self._core_cache, ttl_seconds=self.deployment_affinity_ttl_seconds, enable_user_key_affinity=False, enable_responses_api_affinity=False, @@ -3020,7 +3188,7 @@ class Router: existing_affinity_callback.model_group_affinity_config = self.model_group_affinity_config else: affinity_callback: Final = DeploymentAffinityCheck( - cache=self.cache, + cache=self._core_cache, ttl_seconds=self.deployment_affinity_ttl_seconds, enable_user_key_affinity=enable_user_key_affinity, enable_responses_api_affinity=enable_responses_api_affinity, @@ -3053,19 +3221,19 @@ class Router: continue if pre_call_check == "prompt_caching": _callback = PromptCachingDeploymentCheck( - cache=self.prompt_caching_pin_cache, is_priority_group=self._is_priority_routing_group + cache=self._core_prompt_caching_pin_cache, is_priority_group=self._is_priority_routing_group ) elif pre_call_check == "router_budget_limiting": - if self.get_router_deployment_budget_limiter() is not None: + if self._core_get_router_deployment_budget_limiter() is not None: continue _callback = RouterBudgetLimiting( - dual_cache=self.cache, - provider_budget_config=self.provider_budget_config, - model_list=self.model_list, + dual_cache=self._core_cache, + provider_budget_config=self._core_provider_budget_config, + model_list=self._core_model_list, ) self.router_budget_logger = _callback elif pre_call_check == "enforce_model_rate_limits": - _callback = ModelRateLimitingCheck(dual_cache=self.cache) + _callback = ModelRateLimitingCheck(dual_cache=self._core_cache) if _callback is None: continue @@ -3273,14 +3441,14 @@ class Router: ### DEPLOYMENT-SPECIFIC PRE-CALL CHECKS ### (e.g. update rpm pre-call. Raise error, if deployment over limit) ## only run if model group given, not model id - if model in self.model_names or not self.has_model_id(model): + if model in self._core_model_names or not self._core_has_model_id(model): self.routing_strategy_pre_call_checks(deployment=deployment) input_kwargs: Final = without_router_only_kwargs( { **litellm_params, "messages": messages, - "caching": self.cache_responses, + "caching": self._core_cache_responses, "client": model_client, **kwargs, } @@ -3372,16 +3540,17 @@ class Router: remaining_kwargs: Final = MappingProxyType( {key: value for key, value in silent_kwargs.items() if key != "stream"} ) - response: Final = await self.acompletion( - model=silent_model, - messages=cast(list[AllMessageValues], messages), - stream=bool(silent_kwargs.get("stream", False)), - **remaining_kwargs, - ) - if not isinstance(response, CustomStreamWrapper): - return - async for _ in response: - pass + with mirrored_call(): + response: Final = await self.acompletion( + model=silent_model, + messages=cast(list[AllMessageValues], messages), + stream=bool(silent_kwargs.get("stream", False)), + **remaining_kwargs, + ) + if not isinstance(response, CustomStreamWrapper): + return + async for _ in response: + pass def _silent_experiment_completion(self, silent_model: str, messages: Sequence[Mapping[str, str]], **kwargs): """ @@ -3466,7 +3635,7 @@ class Router: if not is_prompt_management_model: controls: Final = per_request_fallback_controls(kwargs) kwargs[MID_STREAM_FALLBACK_CONTROLS_KEY] = controls # rebind-ok: forwarded to every hop - request_priority, request_kwargs = self.split_request_priority(model=model, kwargs=kwargs) + request_priority, request_kwargs = self._core_split_request_priority(model=model, kwargs=kwargs) original_function: Final[Callable[..., Awaitable[object]]] = ( self._prompt_management_acompletion(model=model, messages=messages) if is_prompt_management_model @@ -3699,12 +3868,12 @@ class Router: try: # Use the router's fallback system model_group: Final = cast(str, initial_kwargs.get("model")) - fallbacks: Final[list | None] = initial_kwargs.get("fallbacks", self.fallbacks) + fallbacks: Final[list | None] = initial_kwargs.get("fallbacks", self._core_fallbacks) context_window_fallbacks: Final[list | None] = initial_kwargs.get( - "context_window_fallbacks", self.context_window_fallbacks + "context_window_fallbacks", self._core_context_window_fallbacks ) content_policy_fallbacks: Final[list | None] = initial_kwargs.get( - "content_policy_fallbacks", self.content_policy_fallbacks + "content_policy_fallbacks", self._core_content_policy_fallbacks ) initial_kwargs["original_function"] = self._acompletion continuation_messages: Final = ( @@ -3952,7 +4121,7 @@ class Router: ) -> Mapping[str, object]: """The request as the deployment received it, merged in the order _update_kwargs_with_deployment applies.""" deployment_params: Final = deployment.litellm_params.model_dump(exclude_none=True) if deployment else {} - default_params: Final = _REQUEST_KWARGS_ADAPTER.validate_python(self.default_litellm_params) + default_params: Final = _REQUEST_KWARGS_ADAPTER.validate_python(self._core_default_litellm_params) defaults: Final = {key: value for key, value in default_params.items() if value is not None} deployment_tool_choice: Final = ( {"tool_choice": deployment_params["tool_choice"]} @@ -3963,9 +4132,9 @@ class Router: def _names_deployment_that_cannot_continue(self, target: object) -> bool: name: Final = fallback_target_name(target) - if name is None or name in self.model_names or not self.has_model_id(name): + if name is None or name in self._core_model_names or not self._core_has_model_id(name): return False - deployment: Final = self.get_deployment(model_id=name) + deployment: Final = self._core_get_deployment(model_id=name) return deployment is not None and not deployment_can_continue(deployment.model_dump(exclude_none=True)) def _mid_stream_continuation_eligible( @@ -3982,7 +4151,7 @@ class Router: if isinstance(e.original_exception, litellm.ContentPolicyViolationError): return False cut_deployment: Final = ( - self.get_deployment(model_id=cut_deployment_id) if isinstance(cut_deployment_id, str) else None + self._core_get_deployment(model_id=cut_deployment_id) if isinstance(cut_deployment_id, str) else None ) if isinstance(cut_deployment_id, str) and cut_deployment is None: return False @@ -3992,7 +4161,9 @@ class Router: lookup_groups: Final = fallback_lookup_groups( request_kwargs, requested_group if isinstance(requested_group, str) else None ) - targets: Final = continuation_fallback_targets(request_kwargs.get("fallbacks", self.fallbacks), lookup_groups) + targets: Final = continuation_fallback_targets( + request_kwargs.get("fallbacks", self._core_fallbacks), lookup_groups + ) if any(fallback_target_overrides_request_params(target) for target in targets): return False if any(self._names_deployment_that_cannot_continue(target) for target in targets): @@ -4291,12 +4462,14 @@ class Router: _request_fallback_list( initial_kwargs, "content_policy_fallbacks", - cast("list[object] | None", self.content_policy_fallbacks), # cast-ok: untyped router attribute + cast( # cast-ok: untyped router attribute + "list[object] | None", self._core_content_policy_fallbacks + ), ), _request_fallback_list( initial_kwargs, "fallbacks", - cast("list[object] | None", self.fallbacks), # cast-ok: untyped router attribute + cast("list[object] | None", self._core_fallbacks), # cast-ok: untyped router attribute ), isinstance(hop_depth, int) and hop_depth > 0, ) @@ -4317,7 +4490,7 @@ class Router: Router._raise_original_mid_stream_error(e) cut_deployment_id: Final = (get_hidden_params(source_iterator) or {}).get("model_id") cut_deployment: Final = ( - self.get_deployment(model_id=cut_deployment_id) if isinstance(cut_deployment_id, str) else None + self._core_get_deployment(model_id=cut_deployment_id) if isinstance(cut_deployment_id, str) else None ) if constrains_responses_continuation(self._params_sent_to_deployment(request_kwargs, cut_deployment)): Router._raise_original_mid_stream_error(e) @@ -4367,13 +4540,13 @@ class Router: try: model_group: Final = cast(str, initial_kwargs.get("model")) # cast-ok: model group fallbacks: Final[list | None] = initial_kwargs.get( # mutable-ok: matches the common_utils list|None param - "fallbacks", self.fallbacks + "fallbacks", self._core_fallbacks ) context_window_fallbacks: Final[list | None] = initial_kwargs.get( # mutable-ok: matches the param below - "context_window_fallbacks", self.context_window_fallbacks + "context_window_fallbacks", self._core_context_window_fallbacks ) content_policy_fallbacks: Final[list | None] = initial_kwargs.get( # mutable-ok: matches the param below - "content_policy_fallbacks", self.content_policy_fallbacks + "content_policy_fallbacks", self._core_content_policy_fallbacks ) initial_kwargs["original_function"] = ( # rebind-ok: the fallback chain re-enters on the same kwargs self._ageneric_api_call_with_fallbacks_responses_attempt @@ -4492,14 +4665,14 @@ class Router: try: model_group: Final = cast(str, initial_kwargs.get("model")) - fallbacks: Final[list | None] = initial_kwargs.get("fallbacks", router_self.fallbacks) + fallbacks: Final[list | None] = initial_kwargs.get("fallbacks", router_self._core_fallbacks) context_window_fallbacks: Final[list | None] = initial_kwargs.get( "context_window_fallbacks", - router_self.context_window_fallbacks, + router_self._core_context_window_fallbacks, ) content_policy_fallbacks: Final[list | None] = initial_kwargs.get( "content_policy_fallbacks", - router_self.content_policy_fallbacks, + router_self._core_content_policy_fallbacks, ) initial_kwargs["original_function"] = router_self._completion initial_kwargs["messages"] = messages @@ -4613,7 +4786,7 @@ class Router: parent_otel_span: Final = get_parent_otel_span_from_kwargs(kwargs) start_time: Final = time.time() - deployment = await self.async_get_available_deployment( + deployment = await self._core_async_get_available_deployment( model=model, messages=messages, specific_deployment=kwargs.pop("specific_deployment", None), @@ -4677,7 +4850,7 @@ class Router: { **litellm_params, "messages": messages, - "caching": self.cache_responses, + "caching": self._core_cache_responses, "client": model_client, **kwargs, } @@ -4785,7 +4958,7 @@ class Router: """ kwargs.setdefault("litellm_trace_id", str(uuid.uuid4())) model_group_alias: str | None = None - if self.get_model_from_alias(model=model): + if self._core_get_model_from_alias(model=model): model_group_alias = model kwargs.setdefault(metadata_variable_name, {}).update( {"model_group": model, "model_group_alias": model_group_alias} @@ -4861,7 +5034,7 @@ class Router: Handles inserting this as either "metadata" or "litellm_metadata" depending on the metadata_variable_name """ # 1) copy your defaults and pull out metadata - defaults: Final = dict(without_retrieve_stream_defaults(self.default_litellm_params, kwargs)) + defaults: Final = dict(without_retrieve_stream_defaults(self._core_default_litellm_params, kwargs)) metadata_defaults: Final = defaults.pop("metadata", {}) or {} # 2) add any non-metadata defaults that aren't already in kwargs @@ -4882,7 +5055,7 @@ class Router: This deployment is deliberately never registered with the router (no upsert_deployment/add_deployment call): doing so used to add it to - self.model_list under the shared model_name, which made a request-scoped, + self._core_model_list under the shared model_name, which made a request-scoped, caller-supplied provider credential a permanent, load-balanced deployment that every other caller of that model group could be routed onto. Its pricing is still registered directly, so a custom price configured on the @@ -4896,7 +5069,7 @@ class Router: function_name=function_name, ) model_group: Final = kwargs.get(metadata_variable_name, {}).get("model_group") - _model_id: Final = self.generate_model_id(model_group=model_group, litellm_params=dynamic_litellm_params) + _model_id: Final = self._core_generate_model_id(model_group=model_group, litellm_params=dynamic_litellm_params) original_model_id: Final = model_info.get("id") model_info["id"] = _model_id model_info["original_model_id"] = original_model_id @@ -4951,7 +5124,7 @@ class Router: ): kwargs.pop(key, None) kwargs.pop("daily_reset_time", None) # pyright: ignore[reportUnknownMemberType] # kwargs is an untyped dict - defaults: Final = _LITELLM_PARAMS_ADAPTER.validate_python(self.default_litellm_params) + defaults: Final = _LITELLM_PARAMS_ADAPTER.validate_python(self._core_default_litellm_params) self._merge_tools_from_deployment(deployment=deployment, kwargs=kwargs, defaults=defaults) model_info = deployment.get("model_info", {}).copy() @@ -5025,7 +5198,7 @@ class Router: # Refund and clear any reservation the previous deployment attempt left # here before it's wiped below, instead of relying on that attempt's # (possibly still-pending) failure event to do it. - refund_stale_reservation_before_retry(self.cache, kwargs) + refund_stale_reservation_before_retry(self._core_cache, kwargs) set_io_token_rate_limit_request_kwargs(kwargs, store_in_context=deployment_has_io_token_limits(deployment)) kwargs[metadata_variable_name].setdefault( @@ -5094,7 +5267,7 @@ class Router: if self.stream_timeout is not None else self.request_timeout if self.request_timeout is not None - else self.default_litellm_params.get("stream_timeout") + else self._core_default_litellm_params.get("stream_timeout") ) passthrough_timeout: Final = partial( resolve_llm_passthrough_timeout, @@ -5190,7 +5363,7 @@ class Router: or data.get("stream_timeout", None) # timeout set on litellm_params for this deployment or self.stream_timeout # timeout set on router or self.request_timeout # litellm_settings.request_timeout (per-attempt) - or self.default_litellm_params.get("stream_timeout", None) + or self._core_default_litellm_params.get("stream_timeout", None) ) def _get_non_stream_timeout( @@ -5205,7 +5378,7 @@ class Router: or data.get("request_timeout", None) # timeout set on litellm_params for this deployment or self.request_timeout # litellm_settings.request_timeout (per-attempt) or self.timeout # timeout set on router (router_settings.timeout) - or self.default_litellm_params.get("timeout", None) + or self._core_default_litellm_params.get("timeout", None) ) def _get_timeout(self, kwargs: Mapping[str, object], data: Mapping[str, object] | GenericLiteLLMParams) -> object: @@ -5466,12 +5639,12 @@ class Router: stream=False, **kwargs, ): - queued_priority, _ = self.split_request_priority(model=model, kwargs={**kwargs, "priority": priority}) + queued_priority, _ = self._core_split_request_priority(model=model, kwargs={**kwargs, "priority": priority}) if queued_priority is None: raise priority_error(model=model, message=unqueueable_priority_message(priority)) return await self.acompletion(model=model, messages=messages, stream=stream, priority=queued_priority, **kwargs) - def split_request_priority( + def _core_split_request_priority( self, model: str, kwargs: Mapping[str, object] ) -> tuple[int | None, Mapping[str, object]]: request_priority: Final = resolve_request_priority( @@ -5490,7 +5663,7 @@ class Router: def _drops_params(self, model: str, kwargs: Mapping[str, object]) -> bool: return request_drops_params( - kwargs, self.default_litellm_params, self._request_deployment_params(model=model, kwargs=kwargs) + kwargs, self._core_default_litellm_params, self._request_deployment_params(model=model, kwargs=kwargs) ) async def _schedule_factory( @@ -5542,7 +5715,7 @@ class Router: ) def _is_prompt_management_model(self, model: str) -> bool: - model_list: Final = self.get_model_list(model_name=model) + model_list: Final = self._core_get_model_list(model_name=model) if model_list is None or len(model_list) != 1: return False @@ -5554,7 +5727,7 @@ class Router: return split_litellm_model in litellm._known_custom_logger_compatible_callbacks def _pinned_deployment(self, model: str) -> Deployment | None: - return self.get_deployment(model_id=model) if model not in self.model_names else None + return self._core_get_deployment(model_id=model) if model not in self._core_model_names else None def _request_deployment_params(self, model: str, kwargs: Mapping[str, object]) -> tuple[Mapping[str, object], ...]: deployment: Final = self._pinned_deployment(model) @@ -5563,7 +5736,7 @@ class Router: candidates: Final = ( self._get_deployment_by_litellm_model(model=model) if kwargs.get("specific_deployment") is True - else self.get_model_list(model_name=model, team_id=get_request_team_id(kwargs)) or () + else self._core_get_model_list(model_name=model, team_id=get_request_team_id(kwargs)) or () ) return tuple(candidate["litellm_params"] for candidate in candidates) @@ -5597,7 +5770,7 @@ class Router: caller_timeout: Final = kwargs.get("timeout") or kwargs.get("request_timeout") caller_stream_timeout: Final = kwargs.get("stream_timeout") specific_deployment: Final = kwargs.pop("specific_deployment", None) - prompt_management_deployment: Final = await self.async_get_available_deployment( + prompt_management_deployment: Final = await self._core_async_get_available_deployment( model=model, messages=cast(list[dict[str, str]], messages), # cast-ok: selection reads messages structurally specific_deployment=specific_deployment, @@ -5663,7 +5836,7 @@ class Router: kwargs["prompt_variables"] = prompt_variables kwargs["prompt_label"] = prompt_label - _model_list: Final = self.get_model_list(model_name=model) + _model_list: Final = self._core_get_model_list(model_name=model) if _model_list is None or len(_model_list) == 0: # if direct call to model kwargs.pop("original_function") return await litellm.acompletion(**kwargs) @@ -5723,7 +5896,7 @@ class Router: **{ **data, "prompt": prompt, - "caching": self.cache_responses, + "caching": self._core_cache_responses, "client": model_client, **kwargs, } @@ -5763,7 +5936,7 @@ class Router: try: verbose_router_logger.debug("Inside _image_generation()- model: %s", model) parent_otel_span: Final = get_parent_otel_span_from_kwargs(kwargs) - deployment: Final = await self.async_get_available_deployment( + deployment: Final = await self._core_async_get_available_deployment( model=model, messages=[{"role": "user", "content": "prompt"}], specific_deployment=kwargs.pop("specific_deployment", None), @@ -5785,7 +5958,7 @@ class Router: **{ **data, "prompt": prompt, - "caching": self.cache_responses, + "caching": self._core_cache_responses, "client": model_client, **kwargs, } @@ -5848,7 +6021,7 @@ class Router: try: verbose_router_logger.debug("Inside _atranscription()- model: %s", model) parent_otel_span: Final = get_parent_otel_span_from_kwargs(kwargs) - deployment: Final = await self.async_get_available_deployment( + deployment: Final = await self._core_async_get_available_deployment( model=model, messages=[{"role": "user", "content": "prompt"}], specific_deployment=kwargs.pop("specific_deployment", None), @@ -5868,7 +6041,7 @@ class Router: **{ **data, "file": file, - "caching": self.cache_responses, + "caching": self._core_cache_responses, "client": model_client, **kwargs, } @@ -5941,7 +6114,7 @@ class Router: try: verbose_router_logger.debug("Inside _aspeech()- model: %s", model) parent_otel_span: Final = get_parent_otel_span_from_kwargs(kwargs) - deployment: Final = await self.async_get_available_deployment( + deployment: Final = await self._core_async_get_available_deployment( model=model, messages=[{"role": "user", "content": "prompt"}], specific_deployment=kwargs.pop("specific_deployment", None), @@ -6002,7 +6175,7 @@ class Router: model_name = None try: verbose_router_logger.debug("Inside _rerank()- model: %s", model) - deployment: Final = await self.async_get_available_deployment( + deployment: Final = await self._core_async_get_available_deployment( model=model, specific_deployment=kwargs.pop("specific_deployment", None), request_kwargs=kwargs, @@ -6020,7 +6193,7 @@ class Router: response: Final = await litellm.arerank( **{ **data, - "caching": self.cache_responses, + "caching": self._core_cache_responses, "client": model_client, **kwargs, } @@ -6053,22 +6226,25 @@ class Router: kwargs.setdefault("metadata", {}).update({"model_group": model}) # pick the one that is available (lowest TPM/RPM) - deployment: Final = self.get_available_deployment( - model=model, - messages=messages, - specific_deployment=kwargs.pop("specific_deployment", None), - request_kwargs=kwargs, - ) + with endpoint_selection("text_completion", model=model): + deployment: Final = self.get_available_deployment( + model=model, + messages=messages, + specific_deployment=kwargs.pop("specific_deployment", None), + request_kwargs=kwargs, + ) data: Final = deployment["litellm_params"].copy() - for k, v in self.default_litellm_params.items(): + for k, v in self._core_default_litellm_params.items(): if k not in kwargs: # prioritize model-specific params > default router params kwargs[k] = v elif k == "metadata": kwargs[k].update(v) # call via litellm.completion() - return litellm.text_completion(**{**data, "prompt": prompt, "caching": self.cache_responses, **kwargs}) + return litellm.text_completion( + **{**data, "prompt": prompt, "caching": self._core_cache_responses, **kwargs} + ) except Exception as e: raise e @@ -6087,7 +6263,7 @@ class Router: kwargs["original_function"] = self._atext_completion self._update_kwargs_before_fallbacks(model=model, kwargs=kwargs) - request_priority, request_kwargs = self.split_request_priority(model=model, kwargs=kwargs) + request_priority, request_kwargs = self._core_split_request_priority(model=model, kwargs=kwargs) if request_priority is None: return await self.async_function_with_fallbacks(**request_kwargs) return await self._schedule_factory( @@ -6112,7 +6288,7 @@ class Router: try: verbose_router_logger.debug("Inside _atext_completion()- model: %s", model) parent_otel_span: Final = get_parent_otel_span_from_kwargs(kwargs) - deployment: Final = await self.async_get_available_deployment( + deployment: Final = await self._core_async_get_available_deployment( model=model, messages=[{"role": "user", "content": prompt}], specific_deployment=kwargs.pop("specific_deployment", None), @@ -6134,7 +6310,7 @@ class Router: **{ **data, "prompt": prompt, - "caching": self.cache_responses, + "caching": self._core_cache_responses, "client": model_client, **kwargs, } @@ -6182,7 +6358,7 @@ class Router: try: verbose_router_logger.debug("Inside _aadapter_completion()- model: %s", model) parent_otel_span: Final = get_parent_otel_span_from_kwargs(kwargs) - deployment: Final = await self.async_get_available_deployment( + deployment: Final = await self._core_async_get_available_deployment( model=model, messages=[{"role": "user", "content": "default text"}], specific_deployment=kwargs.pop("specific_deployment", None), @@ -6204,7 +6380,7 @@ class Router: **{ **data, "adapter_id": adapter_id, - "caching": self.cache_responses, + "caching": self._core_cache_responses, "client": model_client, **kwargs, } @@ -6294,7 +6470,7 @@ class Router: **kwargs: Additional arguments """ guardrail_name: Final = model - selected_guardrail: Final = self.get_available_guardrail( + selected_guardrail: Final = self._core_get_available_guardrail( guardrail_name=guardrail_name, ) @@ -6307,7 +6483,7 @@ class Router: response: Final = await original_generic_function(**kwargs) return response - def get_available_guardrail( + def _core_get_available_guardrail( self, guardrail_name: str, ) -> "GuardrailTypedDict": @@ -6322,7 +6498,7 @@ class Router: """ from litellm.router_strategy.simple_shuffle import simple_shuffle - healthy_deployments: Final = [g for g in self.guardrail_list if g.get("guardrail_name") == guardrail_name] + healthy_deployments: Final = [g for g in self._core_guardrail_list if g.get("guardrail_name") == guardrail_name] if not healthy_deployments: raise ValueError(f"No guardrail found with name: {guardrail_name}") @@ -6332,7 +6508,7 @@ class Router: # Use simple_shuffle for weighted selection return simple_shuffle( - resolve_model_alias=self.get_model_from_alias, + resolve_model_alias=self._core_get_model_from_alias, healthy_deployments=healthy_deployments, model=guardrail_name, request_kwargs=None, @@ -6411,12 +6587,14 @@ class Router: parent_otel_span: Final = get_parent_otel_span_from_kwargs(kwargs) try: with self._video_call_pin(handler_name, kwargs), self._route_capability_pin(handler_name): - deployment = await self.async_get_available_deployment( # rebind-ok: set on success, see pre-init - model=model, - request_kwargs=kwargs, - messages=kwargs.get("messages", None), - input=kwargs.get("input", None), - specific_deployment=kwargs.pop("specific_deployment", None), + deployment = ( # rebind-ok: set on success, see pre-init + await self._core_async_get_available_deployment( + model=model, + request_kwargs=kwargs, + messages=kwargs.get("messages", None), + input=kwargs.get("input", None), + specific_deployment=kwargs.pop("specific_deployment", None), + ) ) except Exception as e: if passthrough_on_no_deployment: @@ -6443,7 +6621,7 @@ class Router: response_kwargs: Final = { **without_retrieve_stream_defaults(data, kwargs), - "caching": self.cache_responses, + "caching": self._core_cache_responses, **kwargs, "model": model_name, **_with_router_resolved_session_model(kwargs.get("session"), model_name), @@ -6798,7 +6976,7 @@ class Router: if isinstance(request_num_retries, int): return request_num_retries deployment_id: Final = routed_deployment_id(kwargs) - deployment: Final = self.get_deployment(deployment_id) if deployment_id is not None else None + deployment: Final = self._core_get_deployment(deployment_id) if deployment_id is not None else None deployment_num_retries: Final = _deployment_num_retries(deployment) if deployment_num_retries is not None: return deployment_num_retries @@ -6882,17 +7060,21 @@ class Router: context_window_fallbacks=_request_fallback_list( kwargs, "context_window_fallbacks", - cast("list[object] | None", self.context_window_fallbacks), # cast-ok: untyped router attribute + cast( # cast-ok: untyped router attribute + "list[object] | None", self._core_context_window_fallbacks + ), ), content_policy_fallbacks=_request_fallback_list( kwargs, "content_policy_fallbacks", - cast("list[object] | None", self.content_policy_fallbacks), # cast-ok: untyped router attribute + cast( # cast-ok: untyped router attribute + "list[object] | None", self._core_content_policy_fallbacks + ), ), regular_fallbacks=_request_fallback_list( kwargs, "fallbacks", - cast("list[object] | None", self.fallbacks), # cast-ok: untyped router attribute + cast("list[object] | None", self._core_fallbacks), # cast-ok: untyped router attribute ), ) except Exception: # noqa: BLE001 # should_retry_this_error declines by raising the error it was given @@ -6914,6 +7096,7 @@ class Router: """ model_group: Final = cast(str, initial_kwargs.get("model")) # cast-ok: kwargs always carries the model group retry_kwargs: Final = mid_stream_retry_kwargs(initial_kwargs) + handler: Final = retry_kwargs.get("original_generic_function") healthy_deployments, all_deployments = await self._async_get_healthy_deployments( model=model_group, parent_otel_span=get_parent_otel_span_from_kwargs(retry_kwargs), @@ -6928,7 +7111,7 @@ class Router: ): return _StreamRetriesExhausted(last_error) if get_metadata_variable_name_from_kwargs(retry_kwargs) in retry_kwargs: - self.log_retry(kwargs=retry_kwargs, e=trigger) + self._core_log_retry(kwargs=retry_kwargs, e=trigger) await asyncio.sleep( self._time_to_sleep_before_retry( e=trigger, @@ -6944,7 +7127,10 @@ class Router: ) carry_superseded_attempt_usage(retry_kwargs) try: - response = await attempt_function(**retry_kwargs) + with endpoint_selection( + getattr(handler, "__name__", None), getattr(handler, "__module__", None), model=model_group + ): + response = await attempt_function(**retry_kwargs) except Exception as retry_error: # noqa: BLE001 # the fallback chain judges the failed retry wrapped = _stream_fallback_error_for_raised(retry_error, model_group, False, e.llm_provider) if wrapped is None: @@ -7014,13 +7200,13 @@ class Router: try: model_group: Final = cast(str, initial_kwargs.get("model")) # cast-ok: model group fallbacks: Final[list | None] = initial_kwargs.get( # mutable-ok: matches the common_utils list|None param - "fallbacks", self.fallbacks + "fallbacks", self._core_fallbacks ) context_window_fallbacks: Final[list | None] = initial_kwargs.get( # mutable-ok: matches the param below - "context_window_fallbacks", self.context_window_fallbacks + "context_window_fallbacks", self._core_context_window_fallbacks ) content_policy_fallbacks: Final[list | None] = initial_kwargs.get( # mutable-ok: matches the param below - "content_policy_fallbacks", self.content_policy_fallbacks + "content_policy_fallbacks", self._core_content_policy_fallbacks ) initial_kwargs["original_function"] = self._ageneric_api_call_with_fallbacks_anthropic_messages_attempt self._update_kwargs_before_fallbacks( @@ -7216,7 +7402,7 @@ class Router: **{ **data, "custom_llm_provider": custom_llm_provider, - "caching": self.cache_responses, + "caching": self._core_cache_responses, **kwargs, } ) @@ -7296,7 +7482,7 @@ class Router: **{ **data, "input": input, - "caching": self.cache_responses, + "caching": self._core_cache_responses, "client": model_client, **kwargs, } @@ -7342,7 +7528,7 @@ class Router: verbose_router_logger.debug("Inside _aembedding()- model: %s", model) parent_otel_span: Final = get_parent_otel_span_from_kwargs(kwargs) with embedding_deployment_selection(): - deployment: Final = await self.async_get_available_deployment( + deployment: Final = await self._core_async_get_available_deployment( model=model, input=input, specific_deployment=kwargs.pop("specific_deployment", None), @@ -7375,7 +7561,7 @@ class Router: **{ **data, "input": input, - "caching": self.cache_responses, + "caching": self._core_cache_responses, "client": model_client, **kwargs, } @@ -7425,7 +7611,7 @@ class Router: verbose_router_logger.debug("Inside _atext_completion()- model: %s", model) parent_otel_span: Final = get_parent_otel_span_from_kwargs(kwargs) - healthy_deployments: Final = await self.async_get_healthy_deployments( + healthy_deployments: Final = await self._core_async_get_healthy_deployments( model=model, messages=[{"role": "user", "content": "files-api-fake-text"}], specific_deployment=kwargs.pop("specific_deployment", None), @@ -7497,7 +7683,7 @@ class Router: **{ **data, "custom_llm_provider": custom_llm_provider, - "caching": self.cache_responses, + "caching": self._core_cache_responses, "client": model_client, **kwargs_copy, } @@ -7565,7 +7751,7 @@ class Router: from litellm.vector_stores import acreate as avector_store_create_sdk parent_otel_span: Final = get_parent_otel_span_from_kwargs(kwargs) - deployment: Final = await self.async_get_available_deployment( + deployment: Final = await self._core_async_get_available_deployment( model=model, messages=[{"role": "user", "content": "vector-store-api-fake-text"}], specific_deployment=kwargs.pop("specific_deployment", None), @@ -7598,7 +7784,7 @@ class Router: **{ **data, "custom_llm_provider": custom_llm_provider, - "caching": self.cache_responses, + "caching": self._core_cache_responses, "client": model_client, **kwargs, } @@ -7664,7 +7850,7 @@ class Router: verbose_router_logger.debug("Inside _acreate_batch()- model: %s", model) parent_otel_span: Final = get_parent_otel_span_from_kwargs(kwargs) with batch_create_routing(): - deployment: Final = await self.async_get_available_deployment( + deployment: Final = await self._core_async_get_available_deployment( model=model, messages=[{"role": "user", "content": "files-api-fake-text"}], specific_deployment=kwargs.pop("specific_deployment", None), @@ -7694,7 +7880,7 @@ class Router: **{ **data, "custom_llm_provider": custom_llm_provider, - "caching": self.cache_responses, + "caching": self._core_cache_responses, "client": model_client, **kwargs, } @@ -7730,7 +7916,7 @@ class Router: if model is not None: filtered_model_list: ( list[DeploymentTypedDict] | list[dict] | dict | None - ) = await self.async_get_healthy_deployments( + ) = await self._core_async_get_healthy_deployments( model=model, messages=[{"role": "user", "content": "retrieve-api-fake-text"}], specific_deployment=kwargs.pop("specific_deployment", None), @@ -7738,7 +7924,7 @@ class Router: parent_otel_span=parent_otel_span, ) else: - filtered_model_list = self.get_model_list() + filtered_model_list = self._core_get_model_list() if filtered_model_list is None: raise Exception("Router not yet initialized.") @@ -7862,7 +8048,7 @@ class Router: try: verbose_router_logger.debug("Inside _acancel_batch()- model: %s", model) parent_otel_span: Final = get_parent_otel_span_from_kwargs(kwargs) - deployment: Final = await self.async_get_available_deployment( + deployment: Final = await self._core_async_get_available_deployment( model=model, messages=[{"role": "user", "content": "batch-api-fake-text"}], specific_deployment=kwargs.pop("specific_deployment", None), @@ -7871,7 +8057,7 @@ class Router: selected_deployment_id: Final = (deployment.get("model_info") or {}).get("id") data: Final = deployment["litellm_params"].copy() - resolved_credentials: Final = self.get_deployment_credentials_with_provider( + resolved_credentials: Final = self._core_get_deployment_credentials_with_provider( model_id=selected_deployment_id or model ) if resolved_credentials is not None: @@ -7899,7 +8085,7 @@ class Router: **{ **data, "custom_llm_provider": custom_llm_provider, - "caching": self.cache_responses, + "caching": self._core_cache_responses, "client": model_client, **kwargs, } @@ -7927,7 +8113,7 @@ class Router: Return all the batches across all deployments of a model group. """ - filtered_model_list: Final = self.get_model_list(model_name=model) + filtered_model_list: Final = self._core_get_model_list(model_name=model) if filtered_model_list is None: raise Exception("Router not yet initialized.") @@ -7976,8 +8162,8 @@ class Router: model=kwargs.get("model", ""), kwargs=kwargs, ) - if kwargs.get("model") and self.get_model_list(model_name=kwargs["model"]): - deployment: Final = await self.async_get_available_deployment( + if kwargs.get("model") and self._core_get_model_list(model_name=kwargs["model"]): + deployment: Final = await self._core_async_get_available_deployment( model=kwargs["model"], request_kwargs=kwargs, ) @@ -8739,7 +8925,7 @@ class Router: # Use wildcard-aware lookup so order-based fallback also works for model # groups resolved via pattern routing (e.g. `openai/*` -> `openai/gpt-4.1-mini`). order_model_group: Final = get_pre_routing_selection(kwargs) or original_model_group - all_deployments: Final = self.get_model_list(model_name=order_model_group, team_id=_request_team_id) or () + all_deployments: Final = self._core_get_model_list(model_name=order_model_group, team_id=_request_team_id) or () _order_set: Final[set] = { litellm.utils.get_deployment_order(d) for d in all_deployments @@ -8991,9 +9177,13 @@ class Router: include_fallback_errors: Final = kwargs.get("include_fallback_errors", False) is True disable_fallbacks: Final[bool | None] = kwargs.pop("disable_fallbacks", False) record_disable_fallbacks(kwargs, disable_fallbacks is True) - fallbacks: Final[list | None] = kwargs.get("fallbacks", self.fallbacks) - context_window_fallbacks: list | None = kwargs.get("context_window_fallbacks", self.context_window_fallbacks) - content_policy_fallbacks: list | None = kwargs.get("content_policy_fallbacks", self.content_policy_fallbacks) + fallbacks: Final[list | None] = kwargs.get("fallbacks", self._core_fallbacks) + context_window_fallbacks: list | None = kwargs.get( + "context_window_fallbacks", self._core_context_window_fallbacks + ) + content_policy_fallbacks: list | None = kwargs.get( + "content_policy_fallbacks", self._core_content_policy_fallbacks + ) mock_timeout: Final = kwargs.pop("mock_timeout", None) @@ -9098,10 +9288,10 @@ class Router: async def async_function_with_retries(self, *args, **kwargs): verbose_router_logger.debug("Inside async function with retries.") original_function: Final = kwargs.pop("original_function") - fallbacks: Final = kwargs.pop("fallbacks", self.fallbacks) + fallbacks: Final = kwargs.pop("fallbacks", self._core_fallbacks) parent_otel_span: Final = get_parent_otel_span_from_kwargs(kwargs) - context_window_fallbacks: Final = kwargs.pop("context_window_fallbacks", self.context_window_fallbacks) - content_policy_fallbacks: Final = kwargs.pop("content_policy_fallbacks", self.content_policy_fallbacks) + context_window_fallbacks: Final = kwargs.pop("context_window_fallbacks", self._core_context_window_fallbacks) + content_policy_fallbacks: Final = kwargs.pop("content_policy_fallbacks", self._core_content_policy_fallbacks) # Support per-request model_group_retry_policy override (from key/team settings) model_group_retry_policy: Final = kwargs.pop("model_group_retry_policy", self.model_group_retry_policy) model_group: Final[str | None] = kwargs.get("model") @@ -9115,7 +9305,7 @@ class Router: ## ADD MODEL GROUP SIZE TO METADATA - used for model_group_rate_limit_error tracking _metadata: Final[dict] = kwargs.get("litellm_metadata", kwargs.get("metadata")) or {} if "model_group" in _metadata and isinstance(_metadata["model_group"], str): - model_list: Final = self.get_model_list(model_name=_metadata["model_group"]) + model_list: Final = self._core_get_model_list(model_name=_metadata["model_group"]) if model_list is not None: _metadata.update({"model_group_size": len(model_list)}) @@ -9194,7 +9384,7 @@ class Router: ## LOGGING if num_retries > 0: - kwargs = self.log_retry(kwargs=kwargs, e=original_exception) + kwargs = self._core_log_retry(kwargs=kwargs, e=original_exception) first_skipped_ids: Final = self._deployment_ids_to_skip_on_retry( exception=original_exception, already_skipped=kwargs.get("_retry_skipped_deployment_ids"), @@ -9243,7 +9433,7 @@ class Router: original_exception = e ## LOGGING - kwargs = self.log_retry(kwargs=kwargs, e=e) + kwargs = self._core_log_retry(kwargs=kwargs, e=e) remaining_retries = num_retries - current_attempt - 1 _model: str | None = kwargs.get("model") if _model is not None: @@ -9314,9 +9504,18 @@ class Router: Handler for making a call to the .completion()/.embeddings()/etc. functions. """ model_group: Final = kwargs.get("model") - response = original_function(*args, **kwargs) - if coroutine_checker.is_async_callable(response) or inspect.isawaitable(response): - response = await response + generic_function: Final = _OPAQUE_VALUE_ADAPTER.validate_python(kwargs.get("original_generic_function")) + endpoint_function: Final = ( + generic_function if callable(generic_function) else _OPAQUE_VALUE_ADAPTER.validate_python(original_function) + ) + with endpoint_selection( + getattr(endpoint_function, "__name__", None), + getattr(endpoint_function, "__module__", None), + model=_OPAQUE_VALUE_ADAPTER.validate_python(model_group), + ): + response = original_function(*args, **kwargs) + if coroutine_checker.is_async_callable(response) or inspect.isawaitable(response): + response = await response await self.increment_deployment_usage_for_response(response=response, request_kwargs=kwargs) ## PROCESS RESPONSE HEADERS response = await self.set_response_headers(response=response, model_group=model_group, request_kwargs=kwargs) @@ -9332,7 +9531,7 @@ class Router: """ mock_testing_rate_limit_error: Final[bool | None] = kwargs.pop("mock_testing_rate_limit_error", None) - available_models: Final = self.get_model_list(model_name=model_group) + available_models: Final = self._core_get_model_list(model_name=model_group) num_retries: int | None = None if available_models is not None and len(available_models) == 1: @@ -9435,9 +9634,9 @@ class Router: """ Returns the first model from the default_fallbacks list, if it exists. """ - if self.fallbacks is None: + if self._core_fallbacks is None: return None - for fallback in self.fallbacks: + for fallback in self._core_fallbacks: if isinstance(fallback, dict) and "*" in fallback: default_list = fallback["*"] if isinstance(default_list, list) and len(default_list) > 0: @@ -9516,7 +9715,11 @@ class Router: model_group: Final = metadata.get("model_group", None) model_info: Final = litellm_params.get("model_info", {}) or {} deployment_id: Final = model_info.get("id", None) - if model_group is None or deployment_id is None or self.get_deployment(model_id=str(deployment_id)) is None: + if ( + model_group is None + or deployment_id is None + or self._core_get_deployment(model_id=str(deployment_id)) is None + ): return if self.session_affinity_repin_on_failover: @@ -9565,12 +9768,12 @@ class Router: model_group: Final = callback.get_repin_model_group(litellm_params) if model_group is None or self._is_priority_routing_group(model_group): return - registered_ids: Final = self.get_candidate_model_ids_for_route( + registered_ids: Final = self._core_get_candidate_model_ids_for_route( model=model_group, team_id=get_request_team_id(litellm_params) ) cooldown_ids: Final = frozenset( model_id - for model_id, _ in await self.cooldown_cache.async_get_active_cooldowns( + for model_id, _ in await self._core_cooldown_cache.async_get_active_cooldowns( model_ids=list(registered_ids), parent_otel_span=None ) ) @@ -9642,22 +9845,22 @@ class Router: RedisPipelineIncrementOperation(key=key, increment_value=increment_value, ttl=RoutingArgs.ttl.value) for key, increment_value in ((tpm_key, total_tokens), (rpm_key, rpm_increment)) ] - post_increment_values: Final = await self.cache.async_increment_cache_pipeline( + post_increment_values: Final = await self._core_cache.async_increment_cache_pipeline( increment_list=pipeline_operations, parent_otel_span=parent_otel_span, ) - if post_increment_values is not None and self.cache.redis_cache is not None: + if post_increment_values is not None and self._core_cache.redis_cache is not None: for operation, value in zip(pipeline_operations, post_increment_values): - await self.cache.async_set_cache( + await self._core_cache.async_set_cache( operation["key"], int(value), local_only=True, ttl=RoutingArgs.ttl.value ) return tpm_key def _deployment_tracks_usage(self, deployment_id: str, model_group: str) -> bool: - deployment_info: Final = self.get_deployment(model_id=deployment_id) + deployment_info: Final = self._core_get_deployment(model_id=deployment_id) if deployment_info is None: return False - deployment_model_info: Final = self.get_router_model_info( + deployment_model_info: Final = self._core_get_router_model_info( deployment=deployment_info, received_model_name=model_group, ) @@ -9775,7 +9978,7 @@ class Router: caller_failure_model_info.get("id") if caller_failure_model_info is not None else None ) caller_failure_deployment: Final = ( - self.get_deployment(model_id=caller_failure_model_id) + self._core_get_deployment(model_id=caller_failure_model_id) if isinstance(caller_failure_model_id, str) else None ) @@ -9811,7 +10014,7 @@ class Router: elif header_cooldown is not None and header_cooldown >= 0: _time_to_cooldown = header_cooldown else: - _time_to_cooldown = self.cooldown_time + _time_to_cooldown = self._core_cooldown_time if isinstance(_model_info, dict): deployment_id: Final[str | None] = _model_info.get("id") @@ -9872,7 +10075,7 @@ class Router: ## RPM rpm_key: Final = RouterCacheEnum.RPM.value.format(id=id, current_minute=current_minute, model=deployment_name) with service_target(ROUTER_USAGE_TARGET): - await self.cache.async_increment_cache( + await self._core_cache.async_increment_cache( key=rpm_key, value=1, parent_otel_span=parent_otel_span, @@ -9893,7 +10096,7 @@ class Router: """ return get_metadata_variable_name_from_kwargs(kwargs) - def log_retry(self, kwargs: dict, e: Exception) -> dict: + def _core_log_retry(self, kwargs: dict, e: Exception) -> dict: """ When a retry or fallback happens, record which model group, deployment and attempt just failed and why, and count it toward the request-wide num_retries_per_request cap @@ -9949,7 +10152,7 @@ class Router: def _deployment_params_base_model(self, deployment_id: str | None) -> str | None: """The ``base_model`` a deployment declares under ``litellm_params`` (the Azure named-deployment shape), the same key the success path's ``get_base_model_from_metadata`` reads.""" - deployment: Final = self.get_deployment(model_id=deployment_id) if deployment_id is not None else None + deployment: Final = self._core_get_deployment(model_id=deployment_id) if deployment_id is not None else None if deployment is None: return None extra_params: Final = cast( # cast-ok: pydantic extras are untyped, base_model is isinstance-checked below @@ -9967,20 +10170,20 @@ class Router: """ rpm_key: Final = deployment_id - request_count = self.cache.get_cache(key=rpm_key, parent_otel_span=parent_otel_span, local_only=True) + request_count = self._core_cache.get_cache(key=rpm_key, parent_otel_span=parent_otel_span, local_only=True) if request_count is None: request_count = 1 - self.cache.set_cache(key=rpm_key, value=request_count, local_only=True, ttl=60) # only store for 60s + self._core_cache.set_cache(key=rpm_key, value=request_count, local_only=True, ttl=60) # only store for 60s else: request_count += 1 - self.cache.set_cache(key=rpm_key, value=request_count, local_only=True) # don't change existing ttl + self._core_cache.set_cache(key=rpm_key, value=request_count, local_only=True) # don't change existing ttl return request_count def _has_default_fallbacks(self) -> bool: - if self.fallbacks is None: + if self._core_fallbacks is None: return False - for fallback in self.fallbacks: + for fallback in self._core_fallbacks: if isinstance(fallback, dict): if "*" in fallback: return True @@ -9993,7 +10196,7 @@ class Router: selected wins over the requested group. Raising without this returning True would turn a deliverable response into an error the fallback chain cannot recover from. """ - content_policy_fallbacks: Final = kwargs.get("content_policy_fallbacks", self.content_policy_fallbacks) + content_policy_fallbacks: Final = kwargs.get("content_policy_fallbacks", self._core_content_policy_fallbacks) if content_policy_fallbacks is not None: return has_unattempted_fallback_target( self._get_fallback_model_group_for_lookup_groups( @@ -10020,12 +10223,12 @@ class Router: """ if fallbacks_disabled_for_request(kwargs): return False - content_policy_fallbacks: Final = kwargs.get("content_policy_fallbacks", self.content_policy_fallbacks) + content_policy_fallbacks: Final = kwargs.get("content_policy_fallbacks", self._core_content_policy_fallbacks) if content_policy_fallbacks is not None: return self._has_content_policy_fallback(model_group, kwargs) if self._has_default_fallbacks(): return True - fallbacks: Final = kwargs.get("fallbacks", self.fallbacks) + fallbacks: Final = kwargs.get("fallbacks", self._core_fallbacks) if fallbacks is None: return False resolved, _ = get_fallback_model_group_for_lookup_groups( @@ -10041,7 +10244,7 @@ class Router: """ request_team_id: Final[str | None] = (kwargs.get("metadata", {}) or {}).get("user_api_key_team_id") order_model_group: Final = get_pre_routing_selection(kwargs) or model_group - all_deployments: Final = self.get_model_list(model_name=order_model_group, team_id=request_team_id) or () + all_deployments: Final = self._core_get_model_list(model_name=order_model_group, team_id=request_team_id) or () return tuple( sorted( { @@ -10070,10 +10273,10 @@ class Router: skip_up_to: Final = current_target if current_target is not None else order_levels[0] if any(o > skip_up_to for o in order_levels): return True - content_policy_fallbacks: Final = kwargs.get("content_policy_fallbacks", self.content_policy_fallbacks) + content_policy_fallbacks: Final = kwargs.get("content_policy_fallbacks", self._core_content_policy_fallbacks) if content_policy_fallbacks is not None and self._has_content_policy_fallback(model_group, kwargs): return True - fallbacks: Final = kwargs.get("fallbacks", self.fallbacks) + fallbacks: Final = kwargs.get("fallbacks", self._core_fallbacks) if not fallbacks: return False if check_non_standard_fallback_format(fallbacks=fallbacks): @@ -10220,7 +10423,7 @@ class Router: exception_status=e.status_code, original_exception=e, deployment=deployment["model_info"]["id"], - time_to_cooldown=self.cooldown_time, + time_to_cooldown=self._core_cooldown_time, ) raise e except Exception as e: @@ -10240,14 +10443,14 @@ class Router: model_id: Final = ( _MODEL_INFO_ADAPTER.validate_python(model_info).get("id") if isinstance(model_info, dict) else None ) - current: Final = self.get_deployment(model_id) if isinstance(model_id, str) else None + current: Final = self._core_get_deployment(model_id) if isinstance(model_id, str) else None return deployment if current is None else current.model_dump(exclude_none=True) async def async_admit_deployment_turn( self, model_group: str, opened_on: Mapping[str, object], request_kwargs: Mapping[str, object] ) -> None: deployment: Final = self._deployment_as_configured_now(opened_on) - budget_limiter: Final = self.get_router_deployment_budget_limiter() + budget_limiter: Final = self._core_get_router_deployment_budget_limiter() if budget_limiter is not None: try: await budget_limiter.async_filter_deployments( # pyright: ignore[reportUnknownMemberType] # legacy signature takes untyped deployment dicts @@ -10370,36 +10573,9 @@ class Router: return f"{type(value).__module__}.{type(value).__qualname__}" @staticmethod - def generate_model_id(model_group: str, litellm_params: dict) -> str: # mutable-ok: hashed read-only - """ - Helper function to consistently generate the same id for a deployment - - - create a string from all the litellm params - - hash - - use hash as id - """ - # Optimized: Use list and join instead of string concatenation in loop - # This avoids creating many temporary string objects (O(n) vs O(n²) complexity) - parts: Final = [model_group] - for k, v in litellm_params.items(): - if isinstance(k, str): - parts.append(k) - elif isinstance(k, dict): - parts.append(json.dumps(k, default=Router._json_default_stable_id)) - else: - parts.append(str(k)) - - if isinstance(v, str): - parts.append(v) - elif isinstance(v, dict): - parts.append(json.dumps(v, default=Router._json_default_stable_id)) - else: - parts.append(str(v)) - - concat_str: Final = "".join(parts) - hash_object: Final = hashlib.sha256(concat_str.encode()) - - return hash_object.hexdigest() + def _core_generate_model_id(model_group: str, litellm_params: dict) -> str: # mutable-ok: hashed read-only + """The stable id of a deployment without ``model_info.id``; see ``litellm.router_api.deployment_id``.""" + return _deployment_id(model_group, litellm_params) @staticmethod def _inherit_builtin_cache_pricing(model_info: dict, backend_model: str, custom_llm_provider: str | None) -> None: @@ -10637,6 +10813,7 @@ class Router: declared_params: Final = _LITELLM_PARAMS_ADAPTER.validate_python(_litellm_params) warn_on_provider_credential_mismatch(model_name=_model_name, litellm_params=declared_params) warn_on_ignored_model_capability_flags(model_name=_model_name, litellm_params=declared_params) + warn_on_unenforced_endpoint_flag(model_name=_model_name, model_info=_model_info) deployment = without_model_capability_flags( Deployment( **deployment_info, @@ -10749,7 +10926,7 @@ class Router: max_input_chars=(max_input_chars if max_input_chars is not None else DEFAULT_AUTO_ROUTER_MAX_INPUT_CHARS), ) self._register_pre_routing_strategy( - registry=self.auto_routers, + registry=self._core_auto_routers, deployment=deployment, strategy=autor_router, strategy_label="Auto-router", @@ -10763,9 +10940,9 @@ class Router: """ return classify_strategy_router_model(litellm_params.model) == "complexity" - def config_deployments(self) -> Iterator[Mapping[str, object]]: + def _core_config_deployments(self) -> Iterator[Mapping[str, object]]: """The model_list rows that came from config.yaml rather than the DB (``model_info.db_model`` unset).""" - for deployment in self.model_list: + for deployment in self._core_model_list: if not isinstance(deployment, Mapping): continue model_info = deployment.get("model_info") @@ -10783,7 +10960,7 @@ class Router: """ limit: Final = self.auto_router_capability_limit() if self.auto_router_capability_limit is not None else None others: Final = count_capability_routers( - (deployment for deployment in self.model_list if isinstance(deployment, Mapping)), + (deployment for deployment in self._core_model_list if isinstance(deployment, Mapping)), capability=capability, ) return capability_limit_violation(capability=capability, held=others + 1, limit=limit) @@ -10830,7 +11007,7 @@ class Router: complexity_router_config=complexity_router_config, ) self._register_pre_routing_strategy( - registry=self.complexity_routers, + registry=self._core_complexity_routers, deployment=deployment, strategy=complexity_router, strategy_label="Complexity-router", @@ -10907,7 +11084,7 @@ class Router: ) except (TypeError, ValueError): return None - return Router.generate_model_id(deployment.model_name, {"routing_definition": definition}) + return Router._core_generate_model_id(deployment.model_name, {"routing_definition": definition}) @staticmethod def _unregister_pre_routing_strategy( @@ -10949,9 +11126,9 @@ class Router: return model_name: Final = deployment.model_name tags: Final = self._deployment_tags(deployment) - for registry in (self.auto_routers, self.complexity_routers, self.quality_routers): + for registry in (self._core_auto_routers, self._core_complexity_routers, self._core_quality_routers): self._unregister_pre_routing_strategy(registry, model_name, tags) - if self._unregister_pre_routing_strategy(self.adaptive_routers, model_name, tags): + if self._unregister_pre_routing_strategy(self._core_adaptive_routers, model_name, tags): self._sync_adaptive_router_hooks() def _finalize_adaptive_router_if_configured(self) -> None: @@ -10959,7 +11136,7 @@ class Router: build an AdaptiveRouter for each. Safe no-op when none are configured. Idempotent: skips any deployment whose (model_name, tags) pair is already initialized, so hot-reloads don't rebuild routers that would lose state.""" - for entry in self.model_list or []: + for entry in self._core_model_list or []: lp = entry.get("litellm_params") if isinstance(entry, dict) else entry.litellm_params lp_model = (lp.get("model") if isinstance(lp, dict) else lp.model) if lp else None if not (lp_model and lp_model.startswith("auto_router/adaptive_router")): @@ -10972,21 +11149,23 @@ class Router: litellm_params=(lp if not isinstance(lp, dict) else LiteLLM_Params.model_validate(lp)), model_info=(entry.get("model_info") if isinstance(entry, dict) else entry.model_info), ) - if self._has_registered_strategy(self.adaptive_routers, model_name, self._deployment_tags(deployment)): + if self._has_registered_strategy( + self._core_adaptive_routers, model_name, self._deployment_tags(deployment) + ): continue self.init_adaptive_router_deployment(deployment=deployment) - for model_name, tagged_complexity_routers in self.complexity_routers.items(): + for model_name, tagged_complexity_routers in self._core_complexity_routers.items(): for tagged in tagged_complexity_routers: complexity_router = tagged.strategy if not complexity_router.config.adaptive: continue - if self._has_registered_strategy(self.adaptive_routers, model_name, tagged.tags): + if self._has_registered_strategy(self._core_adaptive_routers, model_name, tagged.tags): continue adaptive_router: AdaptiveRouter | None = complexity_router._ensure_adaptive_router() if adaptive_router is not None: - self.adaptive_routers[model_name] = [ - *self.adaptive_routers.get(model_name, []), + self._core_adaptive_routers[model_name] = [ + *self._core_adaptive_routers.get(model_name, []), TaggedPreRoutingStrategy( tags=tagged.tags, strategy=adaptive_router, @@ -11008,12 +11187,12 @@ class Router: for callback in litellm.logging_callback_manager.get_custom_loggers_for_type(AdaptiveRouterPostCallHook): litellm.logging_callback_manager.remove_callback_from_all_lists(callback) - for tagged_adaptive_routers in self.adaptive_routers.values(): + for tagged_adaptive_routers in self._core_adaptive_routers.values(): for tagged in tagged_adaptive_routers: litellm.logging_callback_manager.add_litellm_callback( AdaptiveRouterPostCallHook(adaptive_router=tagged.strategy) ) - if self.adaptive_routers: + if self._core_adaptive_routers: litellm.logging_callback_manager.add_litellm_callback(AdaptiveRouterResponseHeaderHook()) def init_adaptive_router_deployment(self, deployment: Deployment) -> None: @@ -11023,7 +11202,7 @@ class Router: keyed by `deployment.model_name`. `model_to_prefs` and `model_to_cost` are derived from the OTHER models - already registered in `self.model_list` whose `model_name` appears in + already registered in `self._core_model_list` whose `model_name` appears in `available_models`. Models not yet registered fall back to defaults. """ # Local import: AdaptiveRouter -> hooks -> classifier all import litellm @@ -11050,7 +11229,7 @@ class Router: # O(k) via the name→indices map: only touch deployments whose name # is listed in `available_models`, instead of scanning model_list. for name in config.available_models: - deployments = self.deployments_named(name) + deployments = self._core_deployments_named(name) if not deployments: continue d = deployments[0] @@ -11073,7 +11252,7 @@ class Router: model_to_cost=model_to_cost, ) self._register_pre_routing_strategy( - registry=self.adaptive_routers, + registry=self._core_adaptive_routers, deployment=deployment, strategy=adaptive_router, strategy_label="Adaptive-router", @@ -11101,7 +11280,7 @@ class Router: Resolves the default model from either `quality_router_default_model` or `quality_router_config["default_model"]`, then instantiates the - QualityRouter and stores it in `self.quality_routers`. + QualityRouter and stores it in `self._core_quality_routers`. """ # Import here to mirror the AutoRouter / ComplexityRouter init pattern # and avoid circular imports. @@ -11128,7 +11307,7 @@ class Router: quality_router_config=quality_router_config, ) self._register_pre_routing_strategy( - registry=self.quality_routers, + registry=self._core_quality_routers, deployment=deployment, strategy=quality_router, strategy_label="Quality-router", @@ -11153,17 +11332,17 @@ class Router: original_model_list: Final = copy.deepcopy(model_list) self._discovered_model_info_cache.flush_cache() with self._deployment_index_lock: - self.model_list = [] + self._core_model_list = [] self.model_id_to_deployment_index_map = {} # Reset the index - self.model_name_to_deployment_indices = {} # Reset the model_name index - self.team_model_to_deployment_indices = {} # Reset the team_model index - self.team_pattern_routers = {} - self.team_public_model_names = frozenset() + self._core_model_name_to_deployment_indices = {} # Reset the model_name index + self._core_team_model_to_deployment_indices = {} # Reset the team_model index + self._core_team_pattern_routers = {} + self._core_team_public_model_names = frozenset() # Reset per-strategy router registries so hot-reload doesn't leave # stale routers pointing at the old model_list. - self.quality_routers = {} - self.complexity_routers = {} - self.auto_routers = {} + self._core_quality_routers = {} + self._core_complexity_routers = {} + self._core_auto_routers = {} self._provider_unresolved_deployments = () self._invalidate_model_group_info_cache() self._invalidate_access_groups_cache() @@ -11206,7 +11385,7 @@ class Router: # check if model info has id if "id" not in _model_info: - _id = self.generate_model_id(_model_name, _litellm_params) + _id = self._core_generate_model_id(_model_name, _litellm_params) _model_info["id"] = _id if _litellm_params.get("organization", None) is not None and isinstance( @@ -11232,8 +11411,8 @@ class Router: duplicate_ids=duplicate_ids, ) - verbose_router_logger.debug("\nInitialized Model List %s", self.get_model_names()) - self.model_names = {m["model_name"] for m in model_list} + verbose_router_logger.debug("\nInitialized Model List %s", self._core_get_model_names()) + self._core_model_names = {m["model_name"] for m in model_list} # Note: model_name_to_deployment_indices is already built incrementally # by _create_deployment -> _add_model_to_list_and_index_map @@ -11295,7 +11474,7 @@ class Router: raise Exception(f"Unsupported provider - {custom_llm_provider}") #### DEPLOYMENT NAMES INIT ######## - self.deployment_names.append(deployment.litellm_params.model) + self._core_deployment_names.append(deployment.litellm_params.model) ############ Users can either pass tpm/rpm as a litellm_param or a router param ########### # for get_available_deployment, we use the litellm_param["rpm"] # in this snippet we also set rpm to be a litellm_param @@ -11310,24 +11489,24 @@ class Router: # if deployment.model_name == "*": # if deployment.litellm_params.model == "*": # # user wants to pass through all requests to litellm.acompletion for unknown deployments - # self.router_general_settings.pass_through_all_models = True + # self._core_router_general_settings.pass_through_all_models = True # else: - # self.default_deployment = deployment.to_json(exclude_none=True) + # self._core_default_deployment = deployment.to_json(exclude_none=True) # Check if user is using provider specific wildcard routing # example model_name = "databricks/*" or model_name = "anthropic/*" if "*" in deployment.model_name: # store this as a regex pattern - all deployments matching this pattern will be sent to this deployment # Store deployment.model_name as a regex pattern - self.pattern_router.add_pattern(deployment.model_name, deployment.to_json(exclude_none=True)) + self._core_pattern_router.add_pattern(deployment.model_name, deployment.to_json(exclude_none=True)) if deployment.model_info.id: - self.provider_default_deployment_ids.append(deployment.model_info.id) + self._core_provider_default_deployment_ids.append(deployment.model_info.id) _team_id: Final = deployment.model_info.get("team_id") _team_public_model_name: Final[str | None] = deployment.model_info.get("team_public_model_name") if _team_id is not None and _team_public_model_name is not None and "*" in _team_public_model_name: - if _team_id not in self.team_pattern_routers: - self.team_pattern_routers[_team_id] = PatternMatchRouter() - self.team_pattern_routers[_team_id].add_pattern( + if _team_id not in self._core_team_pattern_routers: + self._core_team_pattern_routers[_team_id] = PatternMatchRouter() + self._core_team_pattern_routers[_team_id].add_pattern( _team_public_model_name, deployment.to_json(exclude_none=True) ) @@ -11434,12 +11613,15 @@ class Router: # check if deployment already exists _deployment_model_id: Final = deployment.model_info.id - if _deployment_model_id and self.has_model_id(_deployment_model_id): + if _deployment_model_id and self._core_has_model_id(_deployment_model_id): return None declared_params: Final = deployment.litellm_params.model_dump(exclude_none=True) warn_on_provider_credential_mismatch(model_name=deployment.model_name, litellm_params=declared_params) warn_on_ignored_model_capability_flags(model_name=deployment.model_name, litellm_params=declared_params) + warn_on_unenforced_endpoint_flag( + model_name=deployment.model_name, model_info=deployment.model_info.model_dump() + ) served: Final = with_admin_api_key_secret_reference(without_model_capability_flags(deployment)) # add to model list @@ -11451,7 +11633,7 @@ class Router: # add to model names self._add_model_to_list_and_index_map(model=_deployment, model_id=served.model_info.id) - self.model_names.add(served.model_name) + self._core_model_names.add(served.model_name) self._sync_deployment_budget_config(deployment=served) return served @@ -11473,7 +11655,7 @@ class Router: del self.model_id_to_deployment_index_map[model_id] # Update model_name_to_deployment_indices - for model_name, indices in list(self.model_name_to_deployment_indices.items()): + for model_name, indices in list(self._core_model_name_to_deployment_indices.items()): # Build new list without mutating the original updated_indices = [] for idx in indices: @@ -11489,13 +11671,13 @@ class Router: # Update or remove the entry if len(updated_indices) > 0: - self.model_name_to_deployment_indices[model_name] = updated_indices + self._core_model_name_to_deployment_indices[model_name] = updated_indices else: - del self.model_name_to_deployment_indices[model_name] - self.model_names.discard(model_name) + del self._core_model_name_to_deployment_indices[model_name] + self._core_model_names.discard(model_name) # Update team_model_to_deployment_indices - for key, indices in list(self.team_model_to_deployment_indices.items()): + for key, indices in list(self._core_team_model_to_deployment_indices.items()): # Build new list without mutating the original updated_indices = [] for idx in indices: @@ -11511,19 +11693,19 @@ class Router: # Update or remove the entry if len(updated_indices) > 0: - self.team_model_to_deployment_indices[key] = updated_indices + self._core_team_model_to_deployment_indices[key] = updated_indices else: - del self.team_model_to_deployment_indices[key] - self.team_public_model_names = frozenset( - public_model_name for _, public_model_name in self.team_model_to_deployment_indices + del self._core_team_model_to_deployment_indices[key] + self._core_team_public_model_names = frozenset( + public_model_name for _, public_model_name in self._core_team_model_to_deployment_indices ) - self.pattern_router.remove_deployment(model_id) - for team_id in list(self.team_pattern_routers.keys()): - team_pattern_router = self.team_pattern_routers[team_id] + self._core_pattern_router.remove_deployment(model_id) + for team_id in list(self._core_team_pattern_routers.keys()): + team_pattern_router = self._core_team_pattern_routers[team_id] team_pattern_router.remove_deployment(model_id) if not team_pattern_router.patterns: - del self.team_pattern_routers[team_id] + del self._core_team_pattern_routers[team_id] def _update_team_model_index(self, model: dict, idx: int) -> None: """ @@ -11537,11 +11719,13 @@ class Router: team_public_model_name: Final = (model.get("model_info") or {}).get("team_public_model_name") if team_id and team_public_model_name: key: Final = (team_id, team_public_model_name) - self.team_public_model_names = self.team_public_model_names | frozenset({team_public_model_name}) - if key not in self.team_model_to_deployment_indices: - self.team_model_to_deployment_indices[key] = [] - if idx not in self.team_model_to_deployment_indices[key]: - self.team_model_to_deployment_indices[key].append(idx) + self._core_team_public_model_names = self._core_team_public_model_names | frozenset( + {team_public_model_name} + ) + if key not in self._core_team_model_to_deployment_indices: + self._core_team_model_to_deployment_indices[key] = [] + if idx not in self._core_team_model_to_deployment_indices[key]: + self._core_team_model_to_deployment_indices[key].append(idx) def _add_model_to_list_and_index_map(self, model: dict, model_id: str | None = None) -> None: """ @@ -11552,8 +11736,8 @@ class Router: - model_id: Optional[str] - the model ID to use for indexing. If None, will try to get from model["model_info"]["id"] """ with self._deployment_index_lock: - idx: Final = len(self.model_list) - self.model_list.append(model) + idx: Final = len(self._core_model_list) + self._core_model_list.append(model) self._invalidate_model_group_info_cache() self._invalidate_access_groups_cache() @@ -11566,14 +11750,14 @@ class Router: # Update model_name index for O(1) lookup model_name: Final = model.get("model_name") if model_name: - if model_name not in self.model_name_to_deployment_indices: - self.model_name_to_deployment_indices[model_name] = [] - self.model_name_to_deployment_indices[model_name].append(idx) + if model_name not in self._core_model_name_to_deployment_indices: + self._core_model_name_to_deployment_indices[model_name] = [] + self._core_model_name_to_deployment_indices[model_name].append(idx) # Update team_model index for O(1) team-scoped lookup self._update_team_model_index(model, idx) - def upsert_deployment(self, deployment: Deployment) -> Deployment | None: + def _core_upsert_deployment(self, deployment: Deployment) -> Deployment | None: """ Add or update deployment Parameters: @@ -11583,7 +11767,7 @@ class Router: - The added/updated deployment """ _deployment_model_id: Final = deployment.model_info.id or "" - _deployment_on_router: Final[Deployment | None] = self.get_deployment(model_id=_deployment_model_id) + _deployment_on_router: Final[Deployment | None] = self._core_get_deployment(model_id=_deployment_model_id) served: Final = without_model_capability_flags(deployment) try: if _deployment_on_router is not None: @@ -11601,7 +11785,7 @@ class Router: with self._deployment_index_lock: removal_idx: Final = self.model_id_to_deployment_index_map.get(_deployment_model_id) if removal_idx is not None: - self.model_list.pop(removal_idx) + self._core_model_list.pop(removal_idx) self._invalidate_model_group_info_cache() self._invalidate_access_groups_cache() self._update_deployment_indices_after_removal( @@ -11648,7 +11832,7 @@ class Router: capability ceiling a newcomer gets: with the ceiling tightened since the deployment first registered, judging the rollback would drop a serving router over an unrelated failed edit. """ - if previous_deployment is None or self.has_model_id(model_id): + if previous_deployment is None or self._core_has_model_id(model_id): return limit_resolver: Final = self.auto_router_capability_limit self.auto_router_capability_limit = None @@ -11741,7 +11925,7 @@ class Router: """Register a deployment's custom/inherited pricing in ``litellm.model_cost``. Takes only a ``Deployment``, so it registers pricing for a deployment that - is never added to ``self.model_list`` (a per-request client-side-credential + is never added to ``self._core_model_list`` (a per-request client-side-credential deployment) just as readily as one that is. """ Router._register_deployment_in_model_cost( @@ -11812,7 +11996,7 @@ class Router: self._provider_unresolved_deployments = () for create_deployment in provider_unresolved: create_deployment() - for entry in tuple(self.model_list): + for entry in tuple(self._core_model_list): try: deployment = entry if isinstance(entry, Deployment) else Deployment(**entry) except Exception: # noqa: BLE001 # a malformed entry must not abort the rest of the rebuild @@ -11831,7 +12015,7 @@ class Router: _replay_model_cost_registrations = replay_model_cost_registrations - def delete_deployment(self, id: str) -> Deployment | None: + def _core_delete_deployment(self, id: str) -> Deployment | None: """ Parameters: - id: str - the id of the deployment to be deleted @@ -11846,11 +12030,11 @@ class Router: if deployment_idx is None: return None # Pop the item from the list first - item: Final = self.model_list.pop(deployment_idx) + item: Final = self._core_model_list.pop(deployment_idx) self._invalidate_model_group_info_cache() self._invalidate_access_groups_cache() self._update_deployment_indices_after_removal(model_id=id, removal_idx=deployment_idx) - _budget_limiter: Final = self.get_router_deployment_budget_limiter() + _budget_limiter: Final = self._core_get_router_deployment_budget_limiter() if _budget_limiter is not None: _budget_limiter.unregister_deployment_budget(model_id=id) try: @@ -11867,7 +12051,7 @@ class Router: return None return item - def get_router_deployment_budget_limiter( + def _core_get_router_deployment_budget_limiter( self, ) -> RouterBudgetLimiting | None: """ @@ -11886,8 +12070,6 @@ class Router: return _cb return None - _get_router_deployment_budget_limiter = get_router_deployment_budget_limiter - def _deployment_has_budget_limits(self, deployment: Deployment) -> bool: return ( deployment.litellm_params.get("max_budget") is not None @@ -11900,7 +12082,7 @@ class Router: if model_id is None: return - _budget_limiter = self.get_router_deployment_budget_limiter() + _budget_limiter = self._core_get_router_deployment_budget_limiter() if not self._deployment_has_budget_limits(deployment=deployment): if _budget_limiter is not None: @@ -11909,32 +12091,32 @@ class Router: if _budget_limiter is None: self.add_optional_pre_call_checks(optional_pre_call_checks=["router_budget_limiting"]) - _budget_limiter = self.get_router_deployment_budget_limiter() + _budget_limiter = self._core_get_router_deployment_budget_limiter() if _budget_limiter is not None: _budget_limiter.register_deployment_budget(deployment=deployment.to_json(exclude_none=True)) - def deployments_named(self, model_name: str) -> tuple[DeploymentTypedDict, ...] | None: + def _core_deployments_named(self, model_name: str) -> tuple[DeploymentTypedDict, ...] | None: """The deployments indexed under `model_name`, read under the index lock, or None when the name is not indexed.""" with self._deployment_index_lock: - indices: Final = self.model_name_to_deployment_indices.get(model_name) - return None if indices is None else tuple(self.model_list[idx] for idx in indices) + indices: Final = self._core_model_name_to_deployment_indices.get(model_name) + return None if indices is None else tuple(self._core_model_list[idx] for idx in indices) def _served_openai_models(self, model_name: str) -> tuple[str | None, ...]: - return served_openai_models(self.deployments_named(model_name) or ()) + return served_openai_models(self._core_deployments_named(model_name) or ()) def team_public_model_names_of(self, team_id: str) -> frozenset[str]: """The public names `team_id` serves its own deployments under.""" with self._deployment_index_lock: - return frozenset(name for owner, name in self.team_model_to_deployment_indices if owner == team_id) + return frozenset(name for owner, name in self._core_team_model_to_deployment_indices if owner == team_id) def _team_deployments_named(self, team_id: str, public_model_name: str) -> tuple[DeploymentTypedDict, ...] | None: """The deployments `team_id` serves under `public_model_name`, or None when that pair is not indexed.""" with self._deployment_index_lock: - indices: Final = self.team_model_to_deployment_indices.get((team_id, public_model_name)) - return None if indices is None else tuple(self.model_list[idx] for idx in indices) + indices: Final = self._core_team_model_to_deployment_indices.get((team_id, public_model_name)) + return None if indices is None else tuple(self._core_model_list[idx] for idx in indices) - def get_deployment(self, model_id: str) -> Deployment | None: + def _core_get_deployment(self, model_id: str) -> Deployment | None: """ Returns -> Deployment or None @@ -11943,7 +12125,7 @@ class Router: # Use O(1) lookup via model_id_to_deployment_index_map only with self._deployment_index_lock: idx: Final = self.model_id_to_deployment_index_map.get(model_id) - model: Final = None if idx is None else self.model_list[idx] + model: Final = None if idx is None else self._core_model_list[idx] if model is not None: if isinstance(model, dict): return Deployment(**model) @@ -11954,7 +12136,7 @@ class Router: return None - def get_deployment_credentials(self, model_id: str) -> dict | None: + def _core_get_deployment_credentials(self, model_id: str) -> dict | None: """ Returns -> dict of credentials for a given model id. @@ -11962,14 +12144,14 @@ class Router: so file/batch/passthrough callers that resolve credentials directly cannot keep using a paused deployment. """ - deployment: Final = self.get_deployment(model_id=model_id) - if deployment is None or self.is_deployment_blocked(deployment): + deployment: Final = self._core_get_deployment(model_id=model_id) + if deployment is None or self._core_is_deployment_blocked(deployment): return None return CredentialLiteLLMParams.model_validate( deployment.litellm_params.model_dump(exclude_none=True) ).model_dump(exclude_none=True) - def get_deployment_by_model_group_name(self, model_group_name: str) -> Deployment | None: + def _core_get_deployment_by_model_group_name(self, model_group_name: str) -> Deployment | None: """ Returns -> Deployment or None @@ -11979,9 +12161,9 @@ class Router: """ # O(1) lookup in model_name index with self._deployment_index_lock: - indices: Final = self.model_name_to_deployment_indices.get(model_group_name) + indices: Final = self._core_model_name_to_deployment_indices.get(model_group_name) # Return first deployment for this model_name - model: Final = self.model_list[indices[0]] if indices else None + model: Final = self._core_model_list[indices[0]] if indices else None if model is not None: if isinstance(model, dict): return Deployment(**model) @@ -11992,7 +12174,7 @@ class Router: return None @staticmethod - def deployment_usable_by_team(model: Mapping[str, object] | Deployment, team_id: str | None) -> bool: + def _core_deployment_usable_by_team(model: Mapping[str, object] | Deployment, team_id: str | None) -> bool: """ A team-scoped deployment (``model_info.team_id`` set) is only usable by callers from that same team; deployments without a team owner are shared. @@ -12001,8 +12183,6 @@ class Router: owner_team_id: Final = cast(Mapping[str, object], model_info).get("team_id") if model_info is not None else None return team_may_use_deployment(owner_team_id, team_id) - _deployment_usable_by_team = deployment_usable_by_team - def _get_model_group_deployment_usable_by_team( self, model_group_name: str, team_id: str | None ) -> Deployment | None: @@ -12012,18 +12192,20 @@ class Router: credentials. """ with self._deployment_index_lock: - indices: Final = self.model_name_to_deployment_indices.get(model_group_name) or () + indices: Final = self._core_model_name_to_deployment_indices.get(model_group_name) or () usable: Final = ( - self.model_list[idx] for idx in indices if self.deployment_usable_by_team(self.model_list[idx], team_id) + self._core_model_list[idx] + for idx in indices + if self._core_deployment_usable_by_team(self._core_model_list[idx], team_id) ) first_usable: Final = next(usable, None) if first_usable is None: return None return Deployment(**first_usable) if isinstance(first_usable, dict) else first_usable - async def arefresh_model_info(self, *, client: AsyncHTTPHandler | None = None) -> None: + async def _core_arefresh_model_info(self, *, client: AsyncHTTPHandler | None = None) -> None: """Refresh token limits advertised by configured OpenAI-compatible deployments.""" - deployments: Final = iter(tuple(self.model_list)) + deployments: Final = iter(tuple(self._core_model_list)) async def refresh_worker() -> None: for raw_deployment in deployments: @@ -12044,7 +12226,7 @@ class Router: { **deployment.litellm_params.model_dump(exclude_none=True), **( - self.get_deployment_credentials_with_provider(deployment.model_info.id or "") + self._core_get_deployment_credentials_with_provider(deployment.model_info.id or "") or MappingProxyType({}) ), } @@ -12055,7 +12237,7 @@ class Router: return if api_base is None or "*" in model or params.get("use_clientside_credentials"): return - api_key: Final = resolve_api_key_secret_reference( + api_key: Final = await aresolve_api_key_secret_reference( params.api_key or dynamic_api_key, model=model, llm_provider=provider, @@ -12077,30 +12259,30 @@ class Router: } ), client=client or get_async_httpx_client(llm_provider=LlmProviders.OPENAI), - cache=self.cache.in_memory_cache, + cache=self._core_cache.in_memory_cache, ) model_id: Final = deployment.model_info.id - if not limits or model_id is None or self.get_model_info(model_id) is not raw_deployment: + if not limits or model_id is None or self._core_get_model_info(model_id) is not raw_deployment: return - self._discovered_model_info_cache.max_size_in_memory = max(len(self.model_list), 1) + self._discovered_model_info_cache.max_size_in_memory = max(len(self._core_model_list), 1) self._discovered_model_info_cache.delete_cache(model_id) self._discovered_model_info_cache.set_cache( model_id, DiscoveredDeploymentModelInfo(deployment=raw_deployment, limits=limits) ) self._invalidate_model_group_info_cache() - def get_discovered_model_info(self, model_id: str | None) -> Mapping[str, int]: + def _core_get_discovered_model_info(self, model_id: str | None) -> Mapping[str, int]: cached: Final[object] = self._discovered_model_info_cache.get_cache(model_id) if ( model_id is not None and isinstance(cached, DiscoveredDeploymentModelInfo) - and cached.deployment is self.get_model_info(model_id) + and cached.deployment is self._core_get_model_info(model_id) ): configured: Final = TypeAdapter(Mapping[str, object]).validate_python(cached.deployment["model_info"]) return MappingProxyType({key: value for key, value in cached.limits.items() if configured.get(key) is None}) return MappingProxyType({}) - def get_model_listing_info(self, model_name: str) -> DeploymentModelListingInfo | None: + def _core_get_model_listing_info(self, model_name: str) -> DeploymentModelListingInfo | None: """ Return what the concrete deployments behind model_name contribute to its /v1/models entry: the cost-map keys for their underlying models, plus the widest @@ -12119,14 +12301,16 @@ class Router: this never triggers pattern matching or deep copies, so it is safe to call per listed model on the /v1/models hot path. """ - deployments: Final = self.deployments_named(model_name) + deployments: Final = self._core_deployments_named(model_name) if not deployments: return None model_infos: Final = tuple( MappingProxyType( { - **self.get_discovered_model_info((deployment.get("model_info") or MappingProxyType({})).get("id")), + **self._core_get_discovered_model_info( + (deployment.get("model_info") or MappingProxyType({})).get("id") + ), **MappingProxyType( { k: v @@ -12157,7 +12341,7 @@ class Router: ) return max(limits) if limits else None - def get_configured_token_limits(self, model_name: str) -> "tuple[int | None, int | None]": + def _core_get_configured_token_limits(self, model_name: str) -> "tuple[int | None, int | None]": """ Return (max_input_tokens, max_output_tokens) configured or discovered for a concrete deployment of model_name, via O(1) index lookup. @@ -12169,13 +12353,13 @@ class Router: get_model_listing_info does: its caller truncates an embedding input to this value, so the widest window in a mixed group would be the wrong answer there. """ - deployment: Final = self.get_deployment_by_model_group_name(model_group_name=model_name) + deployment: Final = self._core_get_deployment_by_model_group_name(model_group_name=model_name) if deployment is None: return (None, None) model_info: Final = MappingProxyType( { - **self.get_discovered_model_info(deployment.model_info.id), + **self._core_get_discovered_model_info(deployment.model_info.id), **deployment.model_info.model_dump(exclude_none=True), } ) @@ -12184,9 +12368,9 @@ class Router: coerce_token_limit(model_info.get("max_output_tokens")), ) - def get_configured_mode(self, model_name: str) -> "str | None": + def _core_get_configured_mode(self, model_name: str) -> "str | None": """Return the mode explicitly configured for a concrete deployment.""" - deployment: Final = self.get_deployment_by_model_group_name(model_group_name=model_name) + deployment: Final = self._core_get_deployment_by_model_group_name(model_group_name=model_name) if deployment is None: return None @@ -12195,7 +12379,7 @@ class Router: return mode return None - def get_configured_display_name(self, model_name: str) -> "str | None": + def _core_get_configured_display_name(self, model_name: str) -> "str | None": """ Return the display_name explicitly configured in a concrete deployment's model_info for model_name, via O(1) index lookup. @@ -12206,7 +12390,7 @@ class Router: matching or deep copies, so it is safe to call per listed model on the /v1/models hot path. """ - deployment: Final = self.get_deployment_by_model_group_name(model_group_name=model_name) + deployment: Final = self._core_get_deployment_by_model_group_name(model_group_name=model_name) if deployment is None: return None @@ -12215,12 +12399,12 @@ class Router: return display_name return None - def routable_model_group(self, model_name: str) -> str: + def _core_routable_model_group(self, model_name: str) -> str: """ The model group a request to model_name routes to: its target when model_name is a `model_group_alias`, else model_name itself. """ - target: Final = self.get_model_from_alias(model_name) + target: Final = self._core_get_model_from_alias(model_name) return target if target is not None else model_name def _routable_deployments(self, model_name: str, team_id: str | None) -> tuple[DeploymentTypedDict, ...]: @@ -12236,15 +12420,19 @@ class Router: Returns an empty tuple for wildcard-expanded or unknown names. """ - named: Final = self.get_all_deployments(model_name=self.routable_model_group(model_name), team_id=team_id) - usable: Final = tuple(deployment for deployment in named if self.deployment_usable_by_team(deployment, team_id)) + named: Final = self._core_get_all_deployments( + model_name=self._core_routable_model_group(model_name), team_id=team_id + ) + usable: Final = tuple( + deployment for deployment in named if self._core_deployment_usable_by_team(deployment, team_id) + ) return tuple( deployment for deployment in (usable or named) if _configured_model_info(deployment).get("blocked") is not True ) - def get_configured_service_tiers(self, model_name: str, team_id: str | None = None) -> tuple[object, ...]: + def _core_get_configured_service_tiers(self, model_name: str, team_id: str | None = None) -> tuple[object, ...]: """ Return the service_tiers value each deployment a request from team_id to model_name can route to (see _routable_deployments) configures in @@ -12259,7 +12447,7 @@ class Router: for deployment in self._routable_deployments(model_name, team_id) ) - def get_search_tool_support(self, model_name: str, team_id: str | None = None) -> tuple[bool | None, ...]: + def _core_get_search_tool_support(self, model_name: str, team_id: str | None = None) -> tuple[bool | None, ...]: """ Whether each deployment a request from team_id to model_name can route to (see _routable_deployments) answers Codex's client `tool_search`: @@ -12268,7 +12456,7 @@ class Router: """ return tuple(_search_tool_support(deployment) for deployment in self._routable_deployments(model_name, team_id)) - def get_routable_upstream_model(self, model_name: str, team_id: str | None = None) -> str | None: + def _core_get_routable_upstream_model(self, model_name: str, team_id: str | None = None) -> str | None: """ Return the `litellm_params.model` of the first deployment a request from team_id to model_name can route to (see _routable_deployments). @@ -12279,7 +12467,7 @@ class Router: deployment: Final = next(iter(self._routable_deployments(model_name, team_id)), None) return deployment["litellm_params"].get("model") if deployment is not None else None - def get_credential_deployment(self, model_id: str, team_id: str | None = None) -> Deployment | None: + def _core_get_credential_deployment(self, model_id: str, team_id: str | None = None) -> Deployment | None: """ The deployment a passthrough endpoint (files, batches, etc.) resolves for a model id or model name: by deployment id first, then by model_name, then by @@ -12292,12 +12480,12 @@ class Router: by resolving the deployment directly. """ deployment: Final = ( - self.get_deployment(model_id=model_id) + self._core_get_deployment(model_id=model_id) or self._get_model_group_deployment_usable_by_team(model_group_name=model_id, team_id=team_id) or self._get_team_public_name_deployment(model_id=model_id, team_id=team_id) or self._get_wildcard_deployment_usable_by_team(model_id=model_id, team_id=team_id) ) - if deployment is None or self.is_deployment_blocked(deployment): + if deployment is None or self._core_is_deployment_blocked(deployment): return None return deployment @@ -12305,19 +12493,19 @@ class Router: if team_id is None: return None with self._deployment_index_lock: - team_indices: Final = self.team_model_to_deployment_indices.get((team_id, model_id)) - team_model: Final = self.model_list[team_indices[0]] if team_indices else None + team_indices: Final = self._core_team_model_to_deployment_indices.get((team_id, model_id)) + team_model: Final = self._core_model_list[team_indices[0]] if team_indices else None if team_model is None: return None return Deployment(**team_model) if isinstance(team_model, dict) else team_model def _get_wildcard_deployment_usable_by_team(self, model_id: str, team_id: str | None) -> Deployment | None: - team_pattern_router: Final = self.team_pattern_routers.get(team_id) if team_id is not None else None + team_pattern_router: Final = self._core_team_pattern_routers.get(team_id) if team_id is not None else None team_wildcard_models: Final = team_pattern_router.route(model_id) if team_pattern_router else None global_wildcard_models: Final = tuple( wildcard_model - for wildcard_model in (self.pattern_router.route(model_id) or ()) - if self.deployment_usable_by_team(wildcard_model, team_id) + for wildcard_model in (self._core_pattern_router.route(model_id) or ()) + if self._core_deployment_usable_by_team(wildcard_model, team_id) ) potential_wildcard_models: Final = team_wildcard_models or global_wildcard_models if not potential_wildcard_models: @@ -12329,7 +12517,7 @@ class Router: return wildcard_deployment return None - def get_deployment_credentials_with_provider( + def _core_get_deployment_credentials_with_provider( self, model_id: str, team_id: str | None = None ) -> dict[str, Any] | None: """ @@ -12358,7 +12546,7 @@ class Router: credentials = router.get_deployment_credentials_with_provider("gpt-4o-litellm") # Returns: {"api_key": "sk-...", "custom_llm_provider": "openai", "model": "gpt-4o", ...} """ - deployment: Final = self.get_credential_deployment(model_id=model_id, team_id=team_id) + deployment: Final = self._core_get_credential_deployment(model_id=model_id, team_id=team_id) if deployment is None: return None @@ -12394,7 +12582,7 @@ class Router: return credentials @overload - def get_router_model_info( + def _core_get_router_model_info( self, deployment: Union[dict, "Deployment"], received_model_name: str, @@ -12403,10 +12591,10 @@ class Router: pass @overload - def get_router_model_info(self, deployment: None, received_model_name: str, id: str) -> ModelMapInfo: + def _core_get_router_model_info(self, deployment: None, received_model_name: str, id: str) -> ModelMapInfo: pass - def get_router_model_info( + def _core_get_router_model_info( self, deployment: Union[dict, "Deployment"] | None, received_model_name: str, @@ -12427,7 +12615,7 @@ class Router: - ValueError -> If model is not mapped yet """ if id is not None: - _deployment: Final = self.get_deployment(model_id=id) + _deployment: Final = self._core_get_deployment(model_id=id) if _deployment is not None: deployment = _deployment @@ -12485,7 +12673,7 @@ class Router: model = _model if "*" in model: # only call pattern_router for wildcard models - potential_models: Final = self.pattern_router.route(received_model_name) + potential_models: Final = self._core_pattern_router.route(received_model_name) if potential_models is not None: for potential_model in potential_models: try: @@ -12529,13 +12717,13 @@ class Router: _declared_base_model(user_model_info) or _declared_base_model(litellm_params.model_extra or {}), custom_llm_provider, ), - **self.get_discovered_model_info((deployment.get("model_info") or {}).get("id")), + **self._core_get_discovered_model_info((deployment.get("model_info") or {}).get("id")), **MappingProxyType({key: value for key, value in user_model_info.items() if value is not None}), } return merged_model_info - def get_model_info(self, id: str) -> dict | None: + def _core_get_model_info(self, id: str) -> dict | None: """ For a given model id, return the model info @@ -12548,21 +12736,21 @@ class Router: # O(1) lookup via model_id_to_deployment_index_map with self._deployment_index_lock: idx: Final = self.model_id_to_deployment_index_map.get(id) - return None if idx is None else self.model_list[idx] + return None if idx is None else self._core_model_list[idx] - def get_model_group(self, id: str) -> list | None: + def _core_get_model_group(self, id: str) -> list | None: """ Return list of all models in the same model group as that model id """ - model_info: Final = self.get_model_info(id=id) + model_info: Final = self._core_get_model_info(id=id) if model_info is None: return None model_name: Final = model_info["model_name"] - return self.get_model_list(model_name=model_name) + return self._core_get_model_list(model_name=model_name) - def get_deployment_model_info(self, model_id: str, model_name: str) -> ModelInfo | None: + def _core_get_deployment_model_info(self, model_id: str, model_name: str) -> ModelInfo | None: """ For a given model id, return the model info @@ -12581,7 +12769,7 @@ class Router: custom_model_info = ( { **copy.deepcopy(litellm.model_cost.get(model_id) or MappingProxyType({})), - **self.get_discovered_model_info(model_id), + **self._core_get_discovered_model_info(model_id), } if model_id in litellm.model_cost else None @@ -12674,7 +12862,7 @@ class Router: serves_team_deployments: Final = team_id is not None and bool( self._team_deployments_named(team_id, model_group) ) - model_list: Final = self.get_model_list_of_routed_group(model_group, team_id) + model_list: Final = self._core_get_model_list_of_routed_group(model_group, team_id) model_group_description: Final[str | None] = next( (d for d in (_configured_model_info(m).get("description") for m in model_list) if isinstance(d, str)), None, @@ -12686,7 +12874,7 @@ class Router: or "model_name" in model and model["model_name"] == model_group or "model_name" in model - and self.pattern_router.route(model_group) is not None + and self._core_pattern_router.route(model_group) is not None ): # exact match is_match = True @@ -12735,7 +12923,9 @@ class Router: try: model_id = model_info_dict.get("id", None) if model_id is not None: - model_info = self.get_deployment_model_info(model_id=model_id, model_name=litellm_params.model) + model_info = self._core_get_deployment_model_info( + model_id=model_id, model_name=litellm_params.model + ) else: model_info = None except Exception: @@ -12934,7 +13124,7 @@ class Router: return model_group_info - def get_model_group_info( + def _core_get_model_group_info( self, model_group: str, *, include_hidden: bool = False, team_id: str | None = None ) -> ModelGroupInfo | None: """ @@ -12945,8 +13135,8 @@ class Router: - None if error constructing model group info or hidden model group (unless include_hidden) """ ## Check if model group alias - if model_group in self.model_group_alias: - item: Final = self.model_group_alias[model_group] + if model_group in self._core_model_group_alias: + item: Final = self._core_model_group_alias[model_group] if isinstance(item, str): _router_model_group = item elif isinstance(item, dict): @@ -12968,7 +13158,7 @@ class Router: model_group=model_group, user_facing_model_group_name=model_group, team_id=team_id ) - async def get_model_group_usage(self, model_group: str) -> tuple[int | None, int | None]: + async def _core_get_model_group_usage(self, model_group: str) -> tuple[int | None, int | None]: """ Returns current tpm/rpm usage for model group @@ -12983,7 +13173,7 @@ class Router: tpm_keys: Final[list[str]] = [] rpm_keys: Final[list[str]] = [] - model_list: Final = self.get_model_list(model_name=model_group) + model_list: Final = self._core_get_model_list(model_name=model_group) if model_list is None: # no matching deployments return None, None @@ -13010,7 +13200,7 @@ class Router: ) combined_tpm_rpm_keys: Final = tpm_keys + rpm_keys - combined_tpm_rpm_values: Final = await self.cache.async_batch_get_cache(keys=combined_tpm_rpm_keys) + combined_tpm_rpm_values: Final = await self._core_cache.async_batch_get_cache(keys=combined_tpm_rpm_keys) if combined_tpm_rpm_values is None: return None, None @@ -13044,7 +13234,7 @@ class Router: itpm_keys: Final[list[str]] = [] otpm_keys: Final[list[str]] = [] - model_list: Final = self.get_model_list(model_name=model_group) + model_list: Final = self._core_get_model_list(model_name=model_group) if model_list is None: return None, None @@ -13068,7 +13258,7 @@ class Router: ) ) - combined_values: Final = await self.cache.async_batch_get_cache(keys=itpm_keys + otpm_keys) + combined_values: Final = await self._core_cache.async_batch_get_cache(keys=itpm_keys + otpm_keys) if combined_values is None: return None, None @@ -13094,12 +13284,12 @@ class Router: This is a speed optimization, since set_response_headers makes a call to get_model_group_info on every request """ - return self.get_model_group_info(model_group) + return self._core_get_model_group_info(model_group) - def cached_model_group_info(self, model_group: str) -> ModelGroupInfo | None: + def _core_cached_model_group_info(self, model_group: str) -> ModelGroupInfo | None: return self._cached_get_model_group_info(model_group) - async def get_remaining_model_group_usage(self, model_group: str) -> dict[str, int]: + async def _core_get_remaining_model_group_usage(self, model_group: str) -> dict[str, int]: model_group_info: Final = self._cached_get_model_group_info(model_group) returned_dict: Final[dict[str, int]] = {} @@ -13124,7 +13314,7 @@ class Router: if tpm_limit is None and rpm_limit is None: return returned_dict - current_tpm, current_rpm = await self.get_model_group_usage(model_group) + current_tpm, current_rpm = await self._core_get_model_group_usage(model_group) if tpm_limit is not None: returned_dict["x-ratelimit-remaining-tokens"] = tpm_limit - (current_tpm or 0) @@ -13161,7 +13351,7 @@ class Router: additional_headers.update(complexity_router_decision_headers(request_kwargs)) if model_group is not None: - remaining_usage: Final = await self.get_remaining_model_group_usage(model_group) + remaining_usage: Final = await self._core_get_remaining_model_group_usage(model_group) apply_remaining_usage_headers(additional_headers, remaining_usage) return response @@ -13172,16 +13362,16 @@ class Router: This index allows us to find all deployments for a given model_name in O(1) time instead of O(n) linear scan through the entire model_list. """ - self.model_name_to_deployment_indices.clear() - self.team_model_to_deployment_indices.clear() - self.team_public_model_names = frozenset() + self._core_model_name_to_deployment_indices.clear() + self._core_team_model_to_deployment_indices.clear() + self._core_team_public_model_names = frozenset() for idx, model in enumerate(model_list): model_name = model.get("model_name") if model_name: - if model_name not in self.model_name_to_deployment_indices: - self.model_name_to_deployment_indices[model_name] = [] - self.model_name_to_deployment_indices[model_name].append(idx) + if model_name not in self._core_model_name_to_deployment_indices: + self._core_model_name_to_deployment_indices[model_name] = [] + self._core_model_name_to_deployment_indices[model_name].append(idx) self._update_team_model_index(model, idx) @@ -13192,7 +13382,7 @@ class Router: requests arrive before model_id_to_deployment_index_map is populated. """ # First populate the model_list - self.model_list = [] + self._core_model_list = [] self._invalidate_model_group_info_cache() self._invalidate_access_groups_cache() for _, model in enumerate(model_list): @@ -13204,7 +13394,7 @@ class Router: if model_id is None: model_name = model.get("model_name", "") litellm_params = model.get("litellm_params", {}) - model_id = self.generate_model_id(model_name, litellm_params) + model_id = self._core_generate_model_id(model_name, litellm_params) # Update the model_info in the original list if "model_info" not in model: model["model_info"] = {} @@ -13212,7 +13402,7 @@ class Router: self._add_model_to_list_and_index_map(model=model, model_id=model_id) - def get_model_ids(self, model_name: str | None = None, exclude_team_models: bool = False) -> list[str]: + def _core_get_model_ids(self, model_name: str | None = None, exclude_team_models: bool = False) -> list[str]: """ if 'model_name' is none, returns all. @@ -13225,7 +13415,7 @@ class Router: if model_name is not None: # O(1) lookup in model_name index, then O(k) iteration where k = deployments for this model_name - for model in self.deployments_named(model_name) or (): + for model in self._core_deployments_named(model_name) or (): if "model_info" in model and "id" in model["model_info"]: if exclude_team_models and model["model_info"].get("team_id"): continue @@ -13235,7 +13425,7 @@ class Router: # Use the index map keys for O(n) where n = total deployments with self._deployment_index_lock: id_indices: Final = tuple(self.model_id_to_deployment_index_map.items()) - models: Final = tuple(self.model_list) + models: Final = tuple(self._core_model_list) for model_id, idx in id_indices: model = models[idx] if "model_info" in model and "id" in model["model_info"]: @@ -13245,7 +13435,7 @@ class Router: return ids - def get_candidate_model_ids_for_route(self, model: str, team_id: str | None = None) -> frozenset[str]: + def _core_get_candidate_model_ids_for_route(self, model: str, team_id: str | None = None) -> frozenset[str]: """ Deployment ids that could serve ``model`` for ``team_id``, following the same precedence ``_common_checks_available_deployment`` uses to build a candidate pool: @@ -13259,7 +13449,7 @@ class Router: check tell a genuine cross-group route from same-group unavailability without leaking deployment ids into request kwargs bound for the provider. """ - resolved: Final = self.get_model_from_alias(model=model) or model + resolved: Final = self._core_get_model_from_alias(model=model) or model routing_group_members: Final = self._get_routing_group_deployments(model=resolved, team_id=team_id) if routing_group_members is not None: return self._deployment_ids(routing_group_members) @@ -13271,7 +13461,7 @@ class Router: return self._deployment_ids( (early_deployments,) if isinstance(early_deployments, Mapping) else early_deployments ) - return self._deployment_ids(self.get_all_deployments(model_name=resolved, team_id=team_id)) + return self._deployment_ids(self._core_get_all_deployments(model_name=resolved, team_id=team_id)) @staticmethod def _deployment_ids(deployments: Sequence[Mapping[str, object]]) -> frozenset[str]: @@ -13293,7 +13483,7 @@ class Router: region, typed for the tag routing filter """ allowed_model_region: Final = None if request_kwargs is None else request_kwargs.get("allowed_model_region") - if allowed_model_region is None or not self.enable_pre_call_checks or not has_request_input: + if allowed_model_region is None or not self._core_enable_pre_call_checks or not has_request_input: return tuple(deployments) if not isinstance(allowed_model_region, str): return () @@ -13357,7 +13547,7 @@ class Router: remaining = rest return routable_ids - def has_model_id(self, candidate_id: str) -> bool: + def _core_has_model_id(self, candidate_id: str) -> bool: """ O(1) membership check for a deployment ID without allocating large lists. @@ -13370,7 +13560,7 @@ class Router: """ return candidate_id in self.model_id_to_deployment_index_map - def resolve_model_name_from_model_id( + def _core_resolve_model_name_from_model_id( self, model_id: str | None, custom_llm_provider: str | None = None ) -> str | None: """ @@ -13400,16 +13590,16 @@ class Router: return None # Strategy 1: Check if model_id directly matches a model_name or deployment ID - if model_id in self.model_names: + if model_id in self._core_model_names: return model_id - if self.has_model_id(model_id): - deployment = self.get_deployment(model_id=model_id) + if self._core_has_model_id(model_id): + deployment = self._core_get_deployment(model_id=model_id) if deployment is not None and deployment.model_name: return deployment.model_name return model_id # Strategy 2: Search through router's model_list to find by litellm_params.model - all_models: Final = self.get_model_list(model_name=None) + all_models: Final = self._core_get_model_list(model_name=None) if not all_models: return None @@ -13430,30 +13620,30 @@ class Router: if model_name: return model_name - if self.is_recognized_model(model_id): + if self._core_is_recognized_model(model_id): return model_id if custom_llm_provider is None: return None - if self.pattern_router.routes_to_provider(model_id, custom_llm_provider): + if self._core_pattern_router.routes_to_provider(model_id, custom_llm_provider): return model_id provider_prefixed: Final = f"{custom_llm_provider}/{model_id}" - if not model_id.startswith(f"{custom_llm_provider}/") and self.pattern_router.routes_to_provider( + if not model_id.startswith(f"{custom_llm_provider}/") and self._core_pattern_router.routes_to_provider( provider_prefixed, custom_llm_provider ): return provider_prefixed return None - def resolve_served_model_name(self, model_name: str, served_model: str) -> str: + def _core_resolve_served_model_name(self, model_name: str, served_model: str) -> str: """ `served_model` when `model_name` routes to it (the alias target, the routing group member, or the name itself), so a request can return to the group that served an earlier one, else `model_name`. """ - target: Final = self.get_model_from_alias(model_name) or model_name + target: Final = self._core_get_model_from_alias(model_name) or model_name routing_group: Final = self.get_routing_group(target) members: Final = routing_group.models if routing_group is not None else () return served_model if served_model == target or served_model in members else model_name - def map_team_model( + def _core_map_team_model( self, team_model_name: str | None, team_id: str, custom_llm_provider: str | None = None ) -> str | None: """ @@ -13463,7 +13653,7 @@ class Router: use the first team deployment of that provider, pinned by ID when its public name also covers another provider. """ - models: Final = self.get_model_list(model_name=team_model_name, team_id=team_id) + models: Final = self._core_get_model_list(model_name=team_model_name, team_id=team_id) if not models: return None if team_model_name is None and custom_llm_provider is not None: @@ -13516,7 +13706,7 @@ class Router: # handled downstream by the pattern_router in _common_checks_available_deployment. return None - def should_include_deployment( + def _core_should_include_deployment( self, model_name: str, model: DeploymentTypedDict | dict, team_id: str | None = None ) -> bool: """ @@ -13541,7 +13731,7 @@ class Router: # No match: deployment is for a different team or doesn't match the requested model return False - def get_all_deployments( + def _core_get_all_deployments( self, model_name: str, model_alias: str | None = None, @@ -13570,7 +13760,7 @@ class Router: if team_models is not None: # O(k) where k = team deployments for this model_name (typically 1-10) for model in team_models: - if not self.should_include_deployment(model_name=model_name, model=model, team_id=team_id): + if not self._core_should_include_deployment(model_name=model_name, model=model, team_id=team_id): continue if model_alias is not None: alias_model = model.copy() @@ -13582,11 +13772,11 @@ class Router: return returned_models # O(1) lookup in model_name index - named_models: Final = self.deployments_named(model_name) + named_models: Final = self._core_deployments_named(model_name) if named_models is not None: # O(k) where k = deployments for this model_name (typically 1-10) for model in named_models: - if self.should_include_deployment(model_name=model_name, model=model, team_id=team_id): + if self._core_should_include_deployment(model_name=model_name, model=model, team_id=team_id): if model_alias is not None: # Optimized: Use shallow copy since we only modify top-level model_name # This is much faster than deepcopy for nested dict structures @@ -13599,8 +13789,8 @@ class Router: # Fallback: if team_id is provided and model_name not in index, # check if model_name matches any team_public_model_name # O(n) scan but only when team_id lookup fails - for _idx, model in enumerate(self.model_list): - if self.should_include_deployment(model_name=model_name, model=model, team_id=team_id): + for _idx, model in enumerate(self._core_model_list): + if self._core_should_include_deployment(model_name=model_name, model=model, team_id=team_id): if model_alias is not None: # Optimized: Use shallow copy since we only modify top-level model_name alias_model = model.copy() @@ -13611,16 +13801,14 @@ class Router: return returned_models - _get_all_deployments = get_all_deployments - - def get_model_names(self, team_id: str | None = None) -> list[str]: + def _core_get_model_names(self, team_id: str | None = None) -> list[str]: """ Returns all possible model names for the router, including models defined via model_group_alias. If a team_id is provided, only deployments configured with that team_id (i.e. team‐specific models) will yield their team public name. """ - deployments: Final = self.get_model_list() or [] + deployments: Final = self._core_get_model_list() or [] model_names: Final = [] for deployment in deployments: @@ -13634,7 +13822,7 @@ class Router: return model_names - def get_fully_blocked_model_names(self) -> set[str]: + def _core_get_fully_blocked_model_names(self) -> set[str]: """ Returns the set of model_names where every backing deployment has `blocked=True`. @@ -13642,7 +13830,7 @@ class Router: surfacing them on admin endpoints (e.g. `/model/info`). A model with at least one non-blocked deployment is still serviceable and remains visible. """ - deployments: Final = self.get_model_list() or [] + deployments: Final = self._core_get_model_list() or [] blocked_by_name: Final[dict[str, bool]] = {} for deployment in deployments: name = deployment.get("model_name") or "" @@ -13656,20 +13844,18 @@ class Router: return {name for name, fully_blocked in blocked_by_name.items() if fully_blocked} @staticmethod - def are_all_deployments_blocked( - deployments: list[DeploymentTypedDict], + def _core_are_all_deployments_blocked( + deployments: Sequence[DeploymentTypedDict], ) -> bool: return len(deployments) > 0 and all( (deployment.get("model_info") or {}).get("blocked") is True for deployment in deployments ) - _are_all_deployments_blocked = are_all_deployments_blocked - def _is_model_fully_blocked(self, model: str) -> bool: - deployments: Final = self.get_model_list(model_name=model) or [] - return self.are_all_deployments_blocked(deployments=deployments) + deployments: Final = self._core_get_model_list(model_name=model) or [] + return self._core_are_all_deployments_blocked(deployments=deployments) - async def async_get_fully_unhealthy_model_names(self) -> set[str]: + async def _core_async_get_fully_unhealthy_model_names(self) -> set[str]: """ Returns the set of model names where every backing deployment is currently marked unhealthy by background health checks (and the health state is not stale). @@ -13697,12 +13883,12 @@ class Router: request for it still be attempted?". A hidden model can still be called directly. """ - if self.allowed_fails_policy is not None: + if self._core_allowed_fails_policy is not None: return set() - unhealthy_ids: Final = await self.health_state_cache.async_get_unhealthy_deployment_ids() + unhealthy_ids: Final = await self._core_health_state_cache.async_get_unhealthy_deployment_ids() if not unhealthy_ids: return set() - deployments: Final = self.get_model_list() or [] + deployments: Final = self._core_get_model_list() or [] unhealthy_by_name: Final[dict[str, bool]] = {} for deployment in deployments: model_info = deployment.get("model_info") or {} @@ -13751,7 +13937,7 @@ class Router: """ return bool(model_info and model_info.get("team_id")) - def get_model_list_from_model_alias(self, model_name: str | None = None) -> list[DeploymentTypedDict]: + def _core_get_model_list_from_model_alias(self, model_name: str | None = None) -> list[DeploymentTypedDict]: """ Helper function to get model list from model alias. @@ -13761,11 +13947,11 @@ class Router: if model_name is not None: # Fast path: direct dict lookup avoids scanning all aliases for non-alias model names. - if model_name not in self.model_group_alias: + if model_name not in self._core_model_group_alias: return returned_models - alias_items = [(model_name, self.model_group_alias[model_name])] + alias_items = [(model_name, self._core_model_group_alias[model_name])] else: - alias_items = list(self.model_group_alias.items()) + alias_items = list(self._core_model_group_alias.items()) for model_alias, model_value in alias_items: if isinstance(model_value, str): @@ -13783,11 +13969,13 @@ class Router: {**row, "model_name": model_alias} for row in self._materialize_routing_group_rows((alias_group,)) ) else: - returned_models.extend(self.get_all_deployments(model_name=_router_model_name, model_alias=model_alias)) + returned_models.extend( + self._core_get_all_deployments(model_name=_router_model_name, model_alias=model_alias) + ) return returned_models - def get_model_list_from_routing_groups(self, model_name: str | None = None) -> Sequence[DeploymentTypedDict]: + def _core_get_model_list_from_routing_groups(self, model_name: str | None = None) -> Sequence[DeploymentTypedDict]: """ Callable routing groups materialized as model-list rows, mirroring `get_model_list_from_model_alias`: each member deployment is emitted @@ -13805,7 +13993,7 @@ class Router: rows: Final = self._materialize_routing_group_rows( tuple( callable_group - for name in self.routing_groups + for name in self._core_routing_groups if (callable_group := self.get_routing_group(name)) is not None ) ) @@ -13817,7 +14005,7 @@ class Router: self._as_routing_group_row(apply_routing_group_priority(group, member, deployment)) for group in groups for member in group.models - for deployment in self.get_all_deployments(model_name=member, model_alias=group.group_name) + for deployment in self._core_get_all_deployments(model_name=member, model_alias=group.group_name) ) @staticmethod @@ -13937,7 +14125,7 @@ class Router: comparing. The filter asks the same question, so a param named by the allowlist on the tier overlay, the request, or a deployment's own litellm_params is never a drop candidate. """ - deployments: Final = self.get_model_list(model_name=model) or () + deployments: Final = self._core_get_model_list(model_name=model) or () if not deployments: return tier_params allowlisted: Final = self._declared_param_allowlist(tier_params) | self._declared_param_allowlist( @@ -13958,7 +14146,7 @@ class Router: ) return MappingProxyType({key: value for key, value in tier_params.items() if key not in unsupported}) - def get_model_list( + def _core_get_model_list( self, model_name: str | None = None, team_id: str | None = None ) -> list[DeploymentTypedDict] | None: """ @@ -13971,16 +14159,16 @@ class Router: returned_models: list[DeploymentTypedDict] = [] if model_name is not None: - returned_models.extend(self.get_all_deployments(model_name=model_name, team_id=team_id)) + returned_models.extend(self._core_get_all_deployments(model_name=model_name, team_id=team_id)) - returned_models.extend(self.get_model_list_from_model_alias(model_name=model_name)) - returned_models.extend(self.get_model_list_from_routing_groups(model_name=model_name)) + returned_models.extend(self._core_get_model_list_from_model_alias(model_name=model_name)) + returned_models.extend(self._core_get_model_list_from_routing_groups(model_name=model_name)) if len(returned_models) == 0 and model_name is not None: returned_models.extend(self._get_wildcard_deployments(model_name=model_name, team_id=team_id)) if model_name is None: - returned_models += self.model_list + returned_models += self._core_model_list return returned_models @@ -13989,31 +14177,31 @@ class Router: The deployments of the wildcard routes matching model_name (the proxy-wide ones, plus team_id's own when given), each emitted under model_name. """ - team_router: Final = self.team_pattern_routers.get(team_id) if team_id is not None else None + team_router: Final = self._core_team_pattern_routers.get(team_id) if team_id is not None else None matches: Final = [ - *self.pattern_router.get_deployments_by_pattern(model=model_name), + *self._core_pattern_router.get_deployments_by_pattern(model=model_name), *(team_router.get_deployments_by_pattern(model=model_name) if team_router is not None else ()), ] return [{**DeploymentTypedDict(**m), "model_name": model_name} for m in matches] - def wildcard_route_deployments(self, model: str, team_id: str | None) -> Sequence[Mapping[str, object]]: + def _core_wildcard_route_deployments(self, model: str, team_id: str | None) -> Sequence[Mapping[str, object]]: """The deployments routing serves ``model`` from through a wildcard: ``team_id``'s own pattern matching it, else the most specific proxy-wide pattern, never both.""" - team_router: Final = self.team_pattern_routers.get(team_id) if team_id is not None else None + team_router: Final = self._core_team_pattern_routers.get(team_id) if team_id is not None else None team_owned: Final[Sequence[Mapping[str, object]]] = ( team_router.get_deployments_by_pattern(model=model) if team_router is not None else () ) - return team_owned or self.pattern_router.get_deployments_by_pattern(model=model) + return team_owned or self._core_pattern_router.get_deployments_by_pattern(model=model) - def wildcard_routed_models(self, model: str, team_id: str | None) -> frozenset[str]: + def _core_wildcard_routed_models(self, model: str, team_id: str | None) -> frozenset[str]: """The models wildcard_route_deployments would route ``model`` to, without copying the deployments.""" - team_router: Final = self.team_pattern_routers.get(team_id) if team_id is not None else None + team_router: Final = self._core_team_pattern_routers.get(team_id) if team_id is not None else None team_owned: Final = ( team_router.get_models_by_pattern(model=model) if team_router is not None else frozenset[str]() ) - return team_owned or self.pattern_router.get_models_by_pattern(model=model) + return team_owned or self._core_pattern_router.get_models_by_pattern(model=model) - def team_ptu_ceiling(self, team_id: str, model_group: str) -> PTUTeamCeiling | None: + def _core_team_ptu_ceiling(self, team_id: str, model_group: str) -> PTUTeamCeiling | None: """The per-minute ceiling ``team_id``'s PTU shares set on the group serving ``model_group``, else None, kept until the model list next changes.""" if team_id not in self._ptu_share_teams(): @@ -14021,18 +14209,20 @@ class Router: return self._cached_team_ptu_ceiling(team_id, model_group) def _collect_ptu_share_teams(self) -> frozenset[str]: - return ptu_share_teams(self.get_model_list() or ()) + return ptu_share_teams(self._core_get_model_list() or ()) def _resolve_team_ptu_ceiling(self, team_id: str, model_group: str) -> PTUTeamCeiling | None: return team_ptu_ceiling( - self.get_model_list() or (), - self.model_list, + self._core_get_model_list() or (), + self._core_model_list, team_id, model_group, - lambda name: self.wildcard_route_deployments(name, team_id), + lambda name: self._core_wildcard_route_deployments(name, team_id), ) - def get_model_list_of_routed_group(self, model_group: str, team_id: str | None = None) -> list[DeploymentTypedDict]: + def _core_get_model_list_of_routed_group( + self, model_group: str, team_id: str | None = None + ) -> list[DeploymentTypedDict]: """ The deployments a request the router has already resolved to model_group is served from: team_id's own deployments under that public name when the team has @@ -14045,14 +14235,14 @@ class Router: in the chain X -> T -> U a request to X is served from T's deployments. """ if team_id is not None and self._team_deployments_named(team_id, model_group): - return self.get_all_deployments(model_name=model_group, team_id=team_id) - named: Final = [ - *self._get_all_deployments(model_name=model_group), - *self.get_model_list_from_routing_groups(model_name=model_group), - ] + return self._core_get_all_deployments(model_name=model_group, team_id=team_id) + own: Final = cast( # cast-ok: the private alias returns what get_all_deployments returns + list[DeploymentTypedDict], self._get_all_deployments(model_name=model_group) + ) + named: Final = [*own, *self._core_get_model_list_from_routing_groups(model_name=model_group)] return named or self._get_wildcard_deployments(model_name=model_group) - def resolved_litellm_models( + def _core_resolved_litellm_models( self, model_name: str, team_id: str | None = None, *, use_base_model: bool = False ) -> tuple[str, ...]: """The provider model strings `model_name` can actually be served by on this proxy. @@ -14070,26 +14260,26 @@ class Router: if use_base_model: return tuple( backend_model - for deployment in self.get_model_list(model_name=model_name, team_id=team_id) or () + for deployment in self._core_get_model_list(model_name=model_name, team_id=team_id) or () if (backend_model := self._cost_map_backend_model(deployment)) ) return tuple( litellm_model - for deployment in self.get_model_list(model_name=model_name, team_id=team_id) or () + for deployment in self._core_get_model_list(model_name=model_name, team_id=team_id) or () if isinstance(litellm_model := deployment.get("litellm_params", {}).get("model"), str) and litellm_model ) def _invalidate_model_group_info_cache(self) -> None: """Invalidate the cached model group info. - Call this whenever self.model_list is modified to ensure the cache is rebuilt. + Call this whenever self._core_model_list is modified to ensure the cache is rebuilt. Also clears the auth-layer zero-cost cache, which depends on the same ``ModelGroupInfo`` data — without this, an in-place pricing update on an existing deployment (same model count) would keep a stale ``True`` result and bypass budget enforcement. """ self._cached_get_model_group_info.cache_clear() - self.cached_deployment_model_info.cache_clear() + self._core_cached_deployment_model_info.cache_clear() self._ptu_share_teams.cache_clear() self._cached_team_ptu_ceiling.cache_clear() self._cached_deployment_tracks_usage.cache_clear() @@ -14100,11 +14290,14 @@ class Router: def _invalidate_access_groups_cache(self) -> None: """Invalidate the cached access groups. - Call this whenever self.model_list is modified to ensure the cache is rebuilt. + Call this whenever self._core_model_list is modified to ensure the cache is rebuilt. It also + advances ``registry().version``, so every write path (``apply``, the old ``set_model_list`` + and ``add_deployment``) moves it. """ self._access_groups_cache = None + self._registry_version += 1 - def get_model_access_groups( + def _core_get_model_access_groups( self, model_name: str | None = None, model_access_group: str | None = None, @@ -14129,7 +14322,7 @@ class Router: access_groups: Final = defaultdict(list) - model_list: Final = self.get_model_list(model_name=model_name, team_id=team_id) + model_list: Final = self._core_get_model_list(model_name=model_name, team_id=team_id) if model_list: for m in model_list: _model_info = m.get("model_info") @@ -14150,12 +14343,12 @@ class Router: return access_groups - def is_model_access_group_for_wildcard_route(self, model_access_group: str) -> bool: + def _core_is_model_access_group_for_wildcard_route(self, model_access_group: str) -> bool: """ Return True if model access group is a wildcard route """ # GET ACCESS GROUPS - access_groups: Final = self.get_model_access_groups(model_access_group=model_access_group) + access_groups: Final = self._core_get_model_access_groups(model_access_group=model_access_group) if len(access_groups) == 0: return False @@ -14164,14 +14357,12 @@ class Router: for model in models: # CHECK IF MODEL ACCESS GROUP IS A WILDCARD ROUTE - if self.pattern_router.route(request=model) is not None: + if self._core_pattern_router.route(request=model) is not None: return True return False - _is_model_access_group_for_wildcard_route = is_model_access_group_for_wildcard_route - - def get_settings(self): + def _core_get_settings(self) -> dict[str, object]: # mutable-ok: the settings snapshot is built fresh per call """ Get router settings method, returns a dictionary of the settings and their values. For example get the set values for routing_strategy_args, routing_strategy, allowed_fails, cooldown_time, num_retries, timeout, max_retries, retry_after @@ -14200,18 +14391,19 @@ class Router: ] for var in vars_to_include: - if var in _all_vars: - _settings_to_return[var] = _all_vars[var] + stored = var if var in _all_vars else "_core_" + var + if stored in _all_vars: + _settings_to_return[var] = _all_vars[stored] if ( var == "routing_strategy_args" - and self.routing_strategy == "latency-based-routing" + and self._core_routing_strategy == "latency-based-routing" and self.lowestlatency_logger is not None ): _settings_to_return[var] = self.lowestlatency_logger.routing_args.json() _settings_to_return["routing_groups"] = [ group.model_dump(exclude=frozenset(("model_priorities",)) if group.model_priorities is None else None) - for group in self.routing_groups.values() + for group in self._core_routing_groups.values() ] return _settings_to_return @@ -14225,7 +14417,7 @@ class Router: routing_strategy=routing_strategy, routing_strategy_args=kwargs.get("routing_strategy_args", {}) ) - def update_settings(self, **kwargs): + def _core_update_settings(self, **kwargs) -> None: """ Update the router settings. """ @@ -14237,8 +14429,8 @@ class Router: "cooldown_time", ] - _existing_router_settings: Final = self.get_settings() - model_group_alias_before: Final = self.model_group_alias + _existing_router_settings: Final = self._core_get_settings() + model_group_alias_before: Final = self._core_model_group_alias rebuild_routing_groups = False routing_args_updated = False for var in kwargs: @@ -14271,14 +14463,14 @@ class Router: if routing_args_updated: self._apply_updated_routing_strategy_args() - if self.model_group_alias != model_group_alias_before: + if self._core_model_group_alias != model_group_alias_before: self._invalidate_model_group_info_cache() if rebuild_routing_groups: routing_groups_input: Final = kwargs.get("routing_groups", self._routing_groups_input) self._init_routing_groups(routing_groups_input) self._routing_groups_input = routing_groups_input - verbose_router_logger.debug("Updated Router settings: %s", self.get_settings()) + verbose_router_logger.debug("Updated Router settings: %s", self._core_get_settings()) @staticmethod def _is_flag_enabled(value: object) -> bool: @@ -14310,10 +14502,10 @@ class Router: parent_otel_span: Final[Span | None] = get_parent_otel_span_from_kwargs(kwargs) if client_type == "max_parallel_requests": cache_key = f"{model_id}_max_parallel_requests_client" - client = self.cache.get_cache(key=cache_key, local_only=True, parent_otel_span=parent_otel_span) + client = self._core_cache.get_cache(key=cache_key, local_only=True, parent_otel_span=parent_otel_span) if client is None: InitalizeCachedClient.set_max_parallel_requests_client(litellm_router_instance=self, model=deployment) - client = self.cache.get_cache(key=cache_key, local_only=True, parent_otel_span=parent_otel_span) + client = self._core_cache.get_cache(key=cache_key, local_only=True, parent_otel_span=parent_otel_span) return client elif client_type == "async": if ( @@ -14325,11 +14517,11 @@ class Router: return None if kwargs.get("stream") is True: cache_key = f"{model_id}_stream_async_client" - client = self.cache.get_cache(key=cache_key, local_only=True, parent_otel_span=parent_otel_span) + client = self._core_cache.get_cache(key=cache_key, local_only=True, parent_otel_span=parent_otel_span) return client else: cache_key = f"{model_id}_async_client" - client = self.cache.get_cache(key=cache_key, local_only=True, parent_otel_span=parent_otel_span) + client = self._core_cache.get_cache(key=cache_key, local_only=True, parent_otel_span=parent_otel_span) return client else: if ( @@ -14341,11 +14533,11 @@ class Router: return None if kwargs.get("stream") is True: cache_key = f"{model_id}_stream_client" - client = self.cache.get_cache(key=cache_key, parent_otel_span=parent_otel_span) + client = self._core_cache.get_cache(key=cache_key, parent_otel_span=parent_otel_span) return client else: cache_key = f"{model_id}_client" - client = self.cache.get_cache(key=cache_key, parent_otel_span=parent_otel_span) + client = self._core_cache.get_cache(key=cache_key, parent_otel_span=parent_otel_span) return client def _count_pre_call_check_tokens( @@ -14408,7 +14600,7 @@ class Router: def _deployment_max_input_tokens(self, model: str, deployment: Mapping[str, object]) -> int | None: """The deployment's declared context window, or None when it declares none or cannot be resolved.""" try: - model_info: Final = self.get_router_model_info( + model_info: Final = self._core_get_router_model_info( deployment=cast(dict, deployment), # cast-ok: router deployments are plain dicts received_model_name=model, ) @@ -14755,7 +14947,7 @@ class Router: group_deployments: Final = ( tuple(request_deployments) if request_deployments is not None - else (tuple(self.get_all_deployments(model_name=model)) or typed_deployments) + else (tuple(self._core_get_all_deployments(model_name=model)) or typed_deployments) ) if ( not _context_window_error @@ -14785,7 +14977,7 @@ class Router: return _returned_deployments - def get_model_from_alias(self, model: str) -> str | None: + def _core_get_model_from_alias(self, model: str) -> str | None: """ Get the model from the alias. @@ -14793,19 +14985,23 @@ class Router: - str, the litellm model name - None, if model is not in model group alias """ - return resolve_model_group_alias(self.model_group_alias, model) - - _get_model_from_alias = get_model_from_alias + return resolve_model_group_alias(self._core_model_group_alias, model) def _get_deployment_by_litellm_model(self, model: str) -> list[DeploymentTypedDict]: """ Get the deployment by litellm model. """ - return [m for m in self.model_list if m["litellm_params"]["model"] == model] + return [m for m in self._core_model_list if m["litellm_params"]["model"] == model] - def get_routable_deployments(self, model: str) -> tuple[DeploymentTypedDict, ...]: + def _core_get_routable_deployments( + self, + model: str, + request_kwargs: Mapping[str, object] = MappingProxyType({}), + call_type: str | None = None, + ) -> tuple[DeploymentTypedDict, ...]: try: - _, candidates = self._common_checks_available_deployment(model=model, request_kwargs={}) # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType] # its signature leaves input and the result untyped + with endpoint_selection(call_type, model=model): + _, candidates = self._common_checks_available_deployment(model=model, request_kwargs=request_kwargs) # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType] # its signature leaves input and the result untyped except (litellm.BadRequestError, ValueError): return () if isinstance(candidates, dict): @@ -14819,16 +15015,16 @@ class Router: include_team_models: bool = False, ) -> tuple[str, list | dict] | None: """ - When ``model`` is not in ``self.model_names``, try team routes, pattern routes, + When ``model`` is not in ``self._core_model_names``, try team routes, pattern routes, team pattern routers, then default deployment. Returns None if none apply. """ - if model in self.model_names: + if model in self._core_model_names: return None # Check for team-specific deployments by team_public_model_name. # This intentionally takes priority over team pattern routers below, # so that named team deployments shadow wildcard/pattern routes. if request_team_id is not None: - team_deployments = self.get_all_deployments(model_name=model, team_id=request_team_id) + team_deployments = self._core_get_all_deployments(model_name=model, team_id=request_team_id) if team_deployments: return model, team_deployments elif include_team_models: @@ -14836,14 +15032,14 @@ class Router: if team_deployments: return model, team_deployments - wildcard_deployments: Final = self.wildcard_route_deployments(model, request_team_id) + wildcard_deployments: Final = self._core_wildcard_route_deployments(model, request_team_id) if wildcard_deployments: return model, list(wildcard_deployments) - if self.default_deployment is not None: + if self._core_default_deployment is not None: # Shallow copy with nested litellm_params copy (100x+ faster than deepcopy) - updated_deployment: Final = self.default_deployment.copy() - updated_deployment["litellm_params"] = self.default_deployment["litellm_params"].copy() + updated_deployment: Final = self._core_default_deployment.copy() + updated_deployment["litellm_params"] = self._core_default_deployment["litellm_params"].copy() updated_deployment["litellm_params"]["model"] = model return model, updated_deployment @@ -14853,8 +15049,8 @@ class Router: """Every team's deployments under public name `model`, for a proxy admin calling without a team.""" with self._deployment_index_lock: team_deployments: Final = [ - self.model_list[index] - for (_, public_model_name), indices in self.team_model_to_deployment_indices.items() + self._core_model_list[index] + for (_, public_model_name), indices in self._core_team_model_to_deployment_indices.items() if public_model_name == model for index in indices ] @@ -14875,16 +15071,16 @@ class Router: ) return team_deployments - def deployments_for_request( + def _core_deployments_for_request( self, model: str, request_kwargs: Mapping[str, object] ) -> Sequence[DeploymentTypedDict]: """The deployments `model` names for this caller, through the same alias, then team-first, then global, then admin-across-teams resolution `_common_checks_available_deployment` applies, so strategy selection and compression policy can never disagree with deployment selection about which marker a name means.""" - registered_name: Final = self.get_model_from_alias(model=model) or model + registered_name: Final = self._core_get_model_from_alias(model=model) or model team_id: Final = get_request_team_id(request_kwargs) - deployments: Final = self.get_all_deployments(model_name=registered_name, team_id=team_id) + deployments: Final = self._core_get_all_deployments(model_name=registered_name, team_id=team_id) if deployments or team_id is not None or not is_proxy_admin_request(request_kwargs): return deployments return self._team_deployments_across_teams(registered_name) @@ -14927,8 +15123,8 @@ class Router: request_team_id=request_team_id, ), ) - elif model not in self.model_names and self.has_model_id(model): - deployment: Final = self.get_deployment(model_id=model) + elif model not in self._core_model_names and self._core_has_model_id(model): + deployment: Final = self._core_get_deployment(model_id=model) if deployment is not None: deployment_model: Final = deployment.litellm_params.model return deployment_model, cast( # cast-ok: contract requires a plain dict for a single deployment @@ -14947,7 +15143,7 @@ class Router: f"LiteLLM Router: Trying to call specific deployment, but Model ID :{model} does not exist in Model ID map" ) - _model_from_alias: Final = self.get_model_from_alias(model=model) + _model_from_alias: Final = self._core_get_model_from_alias(model=model) if _model_from_alias is not None: model = _model_from_alias @@ -14986,7 +15182,7 @@ class Router: healthy_deployments = ( _routing_group_deployments if _routing_group_deployments is not None - else self.get_all_deployments(model_name=model, team_id=request_team_id) + else self._core_get_all_deployments(model_name=model, team_id=request_team_id) ) _pre_model_access_group_filter_len: Final = len(healthy_deployments) healthy_deployments = self._filter_reserved_deployments( @@ -15043,7 +15239,7 @@ class Router: ) # Re-assign model to the fallback and try to get deployments again model = fallback_model - healthy_deployments = self.get_all_deployments(model_name=model, team_id=request_team_id) + healthy_deployments = self._core_get_all_deployments(model_name=model, team_id=request_team_id) healthy_deployments = self._filter_reserved_deployments( model=model, healthy_deployments=self._filter_deployments_by_model_access_groups( @@ -15081,7 +15277,10 @@ class Router: model=model, llm_provider="", ) - return selectable + eligible: Final = eligible_deployments(model, selectable) + if isinstance(eligible, tuple): + return list(eligible) + raise eligible def _filter_reserved_deployments( self, @@ -15141,7 +15340,7 @@ class Router: if model in allowed_models or "*" in allowed_models or "all-proxy-models" in allowed_models: return healthy_deployments - access_groups_for_model: Final = self.get_model_access_groups(model_name=model, team_id=request_team_id) + access_groups_for_model: Final = self._core_get_model_access_groups(model_name=model, team_id=request_team_id) if len(access_groups_for_model) == 0: return healthy_deployments @@ -15160,7 +15359,7 @@ class Router: return filtered_deployments - async def async_get_healthy_deployments( + async def _core_async_get_healthy_deployments( self, model: str, request_kwargs: dict, @@ -15254,7 +15453,11 @@ class Router: # Safety net: only bypass cooldown filter when health-check routing is # driving cooldown (i.e. allowed_fails_policy is set). Without a policy, # cooldowns are from real request failures and must not be bypassed. - if not healthy_deployments and self.enable_health_check_routing and self.allowed_fails_policy is not None: + if ( + not healthy_deployments + and self._core_enable_health_check_routing + and self._core_allowed_fails_policy is not None + ): verbose_router_logger.warning( "All deployments in cooldown via health-check routing, bypassing cooldown filter" ) @@ -15270,7 +15473,7 @@ class Router: parent_otel_span=parent_otel_span, ) - if self.enable_pre_call_checks and (messages is not None or input is not None): + if self._core_enable_pre_call_checks and (messages is not None or input is not None): deployments_to_check: Final = healthy_deployments healthy_deployments = self._pre_call_checks( model=model, @@ -15298,7 +15501,7 @@ class Router: request_deployments=request_deployments, ) - # narrow to whatever `self.routing_plugins` left in candidate_models + # narrow to whatever `self._core_routing_plugins` left in candidate_models healthy_deployments = self._filter_by_routing_plugin_candidates( healthy_deployments=healthy_deployments, request_kwargs=request_kwargs, @@ -15518,11 +15721,11 @@ class Router: specific_deployment: bool | None, ): if ( - self.routing_strategy != "usage-based-routing-v2" - and self.routing_strategy != "simple-shuffle" - and self.routing_strategy != "cost-based-routing" - and self.routing_strategy != "latency-based-routing" - and self.routing_strategy != "least-busy" + self._core_routing_strategy != "usage-based-routing-v2" + and self._core_routing_strategy != "simple-shuffle" + and self._core_routing_strategy != "cost-based-routing" + and self._core_routing_strategy != "latency-based-routing" + and self._core_routing_strategy != "least-busy" ): # prevent regressions for other routing strategies, that don't have async get available deployments implemented. return self._get_available_deployment_unresolved( model=model, @@ -15539,7 +15742,7 @@ class Router: # this hook can modify the model, messages before the routing decision is made ######################################################### responses_call: Final = input is not None and messages is None - pre_routing_hook_response: Final = await self.async_pre_routing_hook( + pre_routing_hook_response: Final = await self._core_async_pre_routing_hook( model=model, request_kwargs=request_kwargs, messages=messages, @@ -15568,7 +15771,7 @@ class Router: routing_read_batch: Final = RoutingReadBatch.for_strategy(strategy, strategy_selector) with RoutingReadBatch.scoped(routing_read_batch): - healthy_deployments: Final = await self.async_get_healthy_deployments( + healthy_deployments: Final = await self._core_async_get_healthy_deployments( model=model, request_kwargs=request_kwargs, messages=messages, @@ -15594,7 +15797,7 @@ class Router: start_time: Final = time.time() if strategy == "simple-shuffle": shuffled: Final = simple_shuffle( - resolve_model_alias=self.get_model_from_alias, + resolve_model_alias=self._core_get_model_from_alias, healthy_deployments=healthy_deployments, model=model, request_kwargs=request_kwargs, @@ -15662,7 +15865,7 @@ class Router: ) raise e - async_get_available_deployment = ( # pyright: ignore[reportUnknownVariableType] # legacy selector return + _core_async_get_available_deployment = ( # pyright: ignore[reportUnknownVariableType] # legacy selector return async_resolve_pods_after( _async_get_available_deployment_unresolved # pyright: ignore[reportUnknownArgumentType] # legacy type ) @@ -15703,7 +15906,7 @@ class Router: # 1. Execute pre-routing hook responses_call: Final = input is not None and messages is None - pre_routing_hook_response: Final = await self.async_pre_routing_hook( + pre_routing_hook_response: Final = await self._core_async_pre_routing_hook( model=model, request_kwargs=request_kwargs, messages=messages, @@ -15722,7 +15925,7 @@ class Router: ) # 2. Get healthy deployments - healthy_deployments: Final = await self.async_get_healthy_deployments( + healthy_deployments: Final = await self._core_async_get_healthy_deployments( model=model, request_kwargs=request_kwargs, messages=messages, @@ -15768,7 +15971,7 @@ class Router: start_time: Final = time.perf_counter() if strategy == "simple-shuffle": return simple_shuffle( - resolve_model_alias=self.get_model_from_alias, + resolve_model_alias=self._core_get_model_from_alias, healthy_deployments=pass_through_deployments, model=model, request_kwargs=request_kwargs, @@ -15836,7 +16039,7 @@ class Router: messages: list[dict[str, object]] | None, ) -> RoutingContext: """ - Build a RoutingContext for `model`, run it through `self.routing_plugins` + Build a RoutingContext for `model`, run it through `self._core_routing_plugins` in order, then stash the narrowed candidate list and accumulated signals onto `request_kwargs["metadata"]` so `_filter_by_routing_plugin_candidates` (called later, during healthy-deployment filtering) can consume them. @@ -15845,7 +16048,7 @@ class Router: resolve_structured_messages, ) - candidate_models: Final = list(self.resolved_litellm_models(model)) + candidate_models: Final = list(self._core_resolved_litellm_models(model)) metadata_key: Final = self._get_metadata_variable_name_from_kwargs(request_kwargs) metadata: Final = request_kwargs.setdefault(metadata_key, {}) @@ -15857,7 +16060,7 @@ class Router: metadata=metadata, ) - for plugin in self.routing_plugins: + for plugin in self._core_routing_plugins: context = await plugin.run(context) metadata["routing_plugin_signals"] = context.signals @@ -15872,12 +16075,12 @@ class Router: request_kwargs: dict, ) -> list[dict] | dict: """ - Narrow `healthy_deployments` to whatever `self.routing_plugins` left in + Narrow `healthy_deployments` to whatever `self._core_routing_plugins` left in `context.candidate_models`. Raises rather than silently falling back to the unfiltered pool -- a plugin narrowing to nothing is a policy decision (e.g. no model this tenant's budget allows), not something to bypass. """ - if not self.routing_plugins or not isinstance(healthy_deployments, list): + if not self._core_routing_plugins or not isinstance(healthy_deployments, list): return healthy_deployments metadata_key: Final = self._get_metadata_variable_name_from_kwargs(request_kwargs) @@ -15921,10 +16124,15 @@ class Router: deployments: returning None hands the request to ordinary tag-aware deployment selection. """ - registries: Final = (self.auto_routers, self.complexity_routers, self.adaptive_routers, self.quality_routers) + registries: Final = ( + self._core_auto_routers, + self._core_complexity_routers, + self._core_adaptive_routers, + self._core_quality_routers, + ) if not any(registries): return None - deployments: Final = self.deployments_for_request(model, request_kwargs) + deployments: Final = self._core_deployments_for_request(model, request_kwargs) registered_names: Final = tuple(dict.fromkeys(str(d["model_name"]) for d in deployments)) or (model,) candidates: Final = tuple( tagged for registry in registries for name in registered_names for tagged in registry.get(name, []) @@ -15936,7 +16144,7 @@ class Router: if request_tags: for tagged in candidates: if tagged.tags and is_valid_deployment_tag( - list(tagged.tags), request_tags, self.tag_filtering_match_any + list(tagged.tags), request_tags, self._core_tag_filtering_match_any ): return tagged for tagged in candidates: @@ -15944,7 +16152,7 @@ class Router: return tagged request_scoped_filtering: Final = request_kwargs.get("enable_tag_filtering") is True if ( - (self.enable_tag_filtering or request_scoped_filtering) + (self._core_enable_tag_filtering or request_scoped_filtering) and all(tagged.tags for tagged in candidates) and any(not self._is_strategy_marker_deployment(d) for d in deployments) ): @@ -16016,7 +16224,14 @@ class Router: ) -> str: if is_native_compaction_call(): return registered_model_name - if not any((self.auto_routers, self.complexity_routers, self.adaptive_routers, self.quality_routers)): + if not any( + ( + self._core_auto_routers, + self._core_complexity_routers, + self._core_adaptive_routers, + self._core_quality_routers, + ) + ): return registered_model_name cache_key: Final = self._claude_code_session_router_cache_key(request_kwargs) if cache_key is None or not isinstance(request_kwargs, dict): @@ -16029,7 +16244,7 @@ class Router: bound_model: Final = await self._get_claude_code_session_router_binding(cache_key) if not isinstance(bound_model, str): return registered_model_name - bound_registered_model: Final = self.get_model_from_alias(model=bound_model) or bound_model + bound_registered_model: Final = self._core_get_model_from_alias(model=bound_model) or bound_model if self._select_pre_routing_strategy(bound_registered_model, request_kwargs) is None: await self._delete_claude_code_session_router_binding(cache_key) return registered_model_name @@ -16050,7 +16265,7 @@ class Router: ) return registered_model_name - async def async_pre_routing_hook( + async def _core_async_pre_routing_hook( self, model: str, request_kwargs: dict[str, object], @@ -16072,7 +16287,7 @@ class Router: """ self._restore_client_output_ceiling(request_kwargs) self._record_routing_decision(request_kwargs=request_kwargs, routing_decision=None) - requested_registered_model_name: Final = self.get_model_from_alias(model=model) or model + requested_registered_model_name: Final = self._core_get_model_from_alias(model=model) or model registered_model_name: Final = await self._resolve_claude_code_session_router( model=model, registered_model_name=requested_registered_model_name, @@ -16085,7 +16300,7 @@ class Router: # `_filter_by_routing_plugin_candidates`) and may attach signals for # downstream strategies (auto-router, complexity-router, ...) to read. ######################################################### - if self.routing_plugins: + if self._core_routing_plugins: await self._run_routing_plugins( model=registered_model_name, request_kwargs=request_kwargs, messages=messages ) @@ -16271,7 +16486,7 @@ class Router: ) -> DeploymentTypedDict | None: markers: Final = tuple( deployment - for deployment in self.deployments_for_request(model, request_kwargs) + for deployment in self._core_deployments_for_request(model, request_kwargs) if "model" in deployment["litellm_params"] and str(deployment["litellm_params"]["model"]).startswith(AUTO_ROUTER_MODEL_PREFIX) ) @@ -16338,7 +16553,7 @@ class Router: """ if pre_routing_hook_response is None or not selected_strategy.tags or not request_tags: return None - if not is_valid_deployment_tag(selected_strategy.tags, request_tags, self.tag_filtering_match_any): + if not is_valid_deployment_tag(selected_strategy.tags, request_tags, self._core_tag_filtering_match_any): return None return ConsumedRequestTagsStamp(model_group=pre_routing_hook_response.model, tags=selected_strategy.tags) @@ -16453,7 +16668,7 @@ class Router: """ Returns the deployment based on routing strategy """ - if self.routing_plugins: + if self._core_routing_plugins: raise ValueError( "Router(plugins=[...]) is configured, but this call resolved to the synchronous " "deployment-selection path, which never runs the routing-plugin pipeline. This " @@ -16515,7 +16730,11 @@ class Router: healthy_deployments=healthy_deployments, cooldown_deployments=cooldown_deployments, ) - if not healthy_deployments and self.enable_health_check_routing and self.allowed_fails_policy is not None: + if ( + not healthy_deployments + and self._core_enable_health_check_routing + and self._core_allowed_fails_policy is not None + ): verbose_router_logger.warning( "All deployments in cooldown via health-check routing, bypassing cooldown filter" ) @@ -16526,7 +16745,7 @@ class Router: ) # filter pre-call checks - if self.enable_pre_call_checks and (messages is not None or input is not None): + if self._core_enable_pre_call_checks and (messages is not None or input is not None): healthy_deployments = self._pre_call_checks( model=model, healthy_deployments=healthy_deployments, @@ -16600,15 +16819,15 @@ class Router: ) if len(healthy_deployments) == 0: - model_ids = self.get_model_ids(model_name=model) - _cooldown_time = self.cooldown_cache.get_min_cooldown( + model_ids = self._core_get_model_ids(model_name=model) + _cooldown_time = self._core_cooldown_cache.get_min_cooldown( model_ids=model_ids, parent_otel_span=parent_otel_span ) _cooldown_list = get_cooldown_deployments(litellm_router_instance=self, parent_otel_span=parent_otel_span) raise RouterRateLimitError( model=model, cooldown_time=_cooldown_time, - enable_pre_call_checks=self.enable_pre_call_checks, + enable_pre_call_checks=self._core_enable_pre_call_checks, cooldown_list=_cooldown_list, model_ids=model_ids, cooldown_deployments=get_cooldown_deployment_info( @@ -16628,7 +16847,7 @@ class Router: # if users pass rpm or tpm, we do a random weighted pick - based on rpm/tpm ############## Check 'weight' param set for weighted pick ################# shuffled: Final = simple_shuffle( - resolve_model_alias=self.get_model_from_alias, + resolve_model_alias=self._core_get_model_from_alias, healthy_deployments=healthy_deployments, model=model, request_kwargs=request_kwargs, @@ -16647,15 +16866,15 @@ class Router: if deployment is None: verbose_router_logger.info("get_available_deployment for model: %s, No deployment available", model) - model_ids = self.get_model_ids(model_name=model) - _cooldown_time = self.cooldown_cache.get_min_cooldown( + model_ids = self._core_get_model_ids(model_name=model) + _cooldown_time = self._core_cooldown_cache.get_min_cooldown( model_ids=model_ids, parent_otel_span=parent_otel_span ) _cooldown_list = get_cooldown_deployments(litellm_router_instance=self, parent_otel_span=parent_otel_span) raise RouterRateLimitError( model=model, cooldown_time=_cooldown_time, - enable_pre_call_checks=self.enable_pre_call_checks, + enable_pre_call_checks=self._core_enable_pre_call_checks, cooldown_list=_cooldown_list, model_ids=model_ids, ) @@ -16675,7 +16894,7 @@ class Router: ) ) - def get_available_deployment_for_pass_through( + def _core_get_available_deployment_for_pass_through( # noqa: ANN202 # unannotated before the rename too; its inferred return mixes untyped deployment dicts self, model: str, messages: list[dict[str, str]] | None = None, @@ -16768,7 +16987,7 @@ class Router: pass_through_deployments = self._filter_blocked_deployments(pass_through_deployments) # 5. Apply pre-call checks (if enabled) - if self.enable_pre_call_checks and (messages is not None or input is not None): + if self._core_enable_pre_call_checks and (messages is not None or input is not None): pass_through_deployments = self._pre_call_checks( model=model, healthy_deployments=pass_through_deployments, @@ -16785,15 +17004,15 @@ class Router: ) if len(pass_through_deployments) == 0: - model_ids = self.get_model_ids(model_name=model) - _cooldown_time = self.cooldown_cache.get_min_cooldown( + model_ids = self._core_get_model_ids(model_name=model) + _cooldown_time = self._core_cooldown_cache.get_min_cooldown( model_ids=model_ids, parent_otel_span=parent_otel_span ) _cooldown_list = get_cooldown_deployments(litellm_router_instance=self, parent_otel_span=parent_otel_span) raise RouterRateLimitError( model=model, cooldown_time=_cooldown_time, - enable_pre_call_checks=self.enable_pre_call_checks, + enable_pre_call_checks=self._core_enable_pre_call_checks, cooldown_list=_cooldown_list, model_ids=pass_through_model_ids, cooldown_deployments=get_cooldown_deployment_info( @@ -16812,7 +17031,7 @@ class Router: # 6. Apply load balancing strategy if strategy == "simple-shuffle": return simple_shuffle( - resolve_model_alias=self.get_model_from_alias, + resolve_model_alias=self._core_get_model_from_alias, healthy_deployments=pass_through_deployments, model=model, request_kwargs=request_kwargs, @@ -16831,15 +17050,15 @@ class Router: verbose_router_logger.info( "get_available_deployment_for_pass_through model: %s, no available deployments", model ) - model_ids = self.get_model_ids(model_name=model) - _cooldown_time = self.cooldown_cache.get_min_cooldown( + model_ids = self._core_get_model_ids(model_name=model) + _cooldown_time = self._core_cooldown_cache.get_min_cooldown( model_ids=model_ids, parent_otel_span=parent_otel_span ) _cooldown_list = get_cooldown_deployments(litellm_router_instance=self, parent_otel_span=parent_otel_span) raise RouterRateLimitError( model=model, cooldown_time=_cooldown_time, - enable_pre_call_checks=self.enable_pre_call_checks, + enable_pre_call_checks=self._core_enable_pre_call_checks, cooldown_list=_cooldown_list, model_ids=model_ids, ) @@ -16888,7 +17107,7 @@ class Router: ] @staticmethod - def is_deployment_blocked(deployment: "Deployment") -> bool: + def _core_is_deployment_blocked(deployment: "Deployment") -> bool: """ Returns True when a `Deployment` Pydantic instance carries the admin-paused flag. Used by credential-lookup helpers so passthrough file / batch endpoints @@ -16899,8 +17118,6 @@ class Router: return False return getattr(model_info, "blocked", None) is True - _is_deployment_blocked = is_deployment_blocked - async def _async_filter_health_check_unhealthy_deployments( self, healthy_deployments: list[dict], @@ -16917,18 +17134,18 @@ class Router: Returns all deployments if health state is unavailable, stale, or would exclude every candidate (safety net). """ - if not self.enable_health_check_routing: + if not self._core_enable_health_check_routing: return healthy_deployments # When allowed_fails_policy is set, cooldown is the sole routing exclusion # mechanism -- skip the binary health check filter so the policy threshold # is respected before any deployment is excluded. With a model-group # allowlist the filter is already scoped, so listed groups keep it. - scoped_groups: Final = self.background_health_check_model_groups - if self.allowed_fails_policy is not None and scoped_groups is None: + scoped_groups: Final = self._core_background_health_check_model_groups + if self._core_allowed_fails_policy is not None and scoped_groups is None: return healthy_deployments - unhealthy_ids: Final = await self.health_state_cache.async_get_unhealthy_deployment_ids( + unhealthy_ids: Final = await self._core_health_state_cache.async_get_unhealthy_deployment_ids( parent_otel_span=parent_otel_span ) if not unhealthy_ids: @@ -16952,14 +17169,16 @@ class Router: parent_otel_span: Span | None = None, ) -> list[dict]: """Sync version of _async_filter_health_check_unhealthy_deployments.""" - if not self.enable_health_check_routing: + if not self._core_enable_health_check_routing: return healthy_deployments - scoped_groups: Final = self.background_health_check_model_groups - if self.allowed_fails_policy is not None and scoped_groups is None: + scoped_groups: Final = self._core_background_health_check_model_groups + if self._core_allowed_fails_policy is not None and scoped_groups is None: return healthy_deployments - unhealthy_ids: Final = self.health_state_cache.get_unhealthy_deployment_ids(parent_otel_span=parent_otel_span) + unhealthy_ids: Final = self._core_health_state_cache.get_unhealthy_deployment_ids( + parent_otel_span=parent_otel_span + ) if not unhealthy_ids: return healthy_deployments @@ -17021,7 +17240,7 @@ class Router: retry_policy=self.retry_policy, ) - def get_allowed_fails_from_policy(self, exception: Exception): + def _core_get_allowed_fails_from_policy(self, exception: Exception) -> int | None: """ BadRequestErrorRetries: Optional[int] = None AuthenticationErrorRetries: Optional[int] = None @@ -17030,7 +17249,7 @@ class Router: ContentPolicyViolationErrorRetries: Optional[int] = None """ # if we can find the exception then in the retry policy -> return the number of retries - allowed_fails_policy: Final[AllowedFailsPolicy | None] = self.allowed_fails_policy + allowed_fails_policy: Final[AllowedFailsPolicy | None] = self._core_allowed_fails_policy if allowed_fails_policy is None: return None @@ -17089,7 +17308,7 @@ class Router: default_webhook_url=router_alerting_config.webhook_url, ) - self.slack_alerting_logger = _slack_alerting_logger + self._core_slack_alerting_logger = _slack_alerting_logger litellm.logging_callback_manager.add_litellm_callback(_slack_alerting_logger) litellm.logging_callback_manager.add_litellm_success_callback( @@ -17097,7 +17316,7 @@ class Router: ) verbose_router_logger.info("\033[94m\nInitialized Alerting for litellm.Router\033[0m\n") - def set_custom_routing_strategy(self, CustomRoutingStrategy: CustomRoutingStrategyBase): + def _core_set_custom_routing_strategy(self, CustomRoutingStrategy: CustomRoutingStrategyBase) -> None: """ Sets get_available_deployment and async_get_available_deployment on an instanced of litellm.Router @@ -17113,30 +17332,30 @@ class Router: "get_available_deployment", resolve_pods_after_bound( strategy.get_available_deployment, # pyright: ignore[reportUnknownArgumentType] # legacy stub - self.kubernetes_pod_discovery, + self._core_kubernetes_pod_discovery, ), ) setattr( self, - "async_get_available_deployment", + "_core_async_get_available_deployment", async_resolve_pods_after_bound( strategy.async_get_available_deployment, # pyright: ignore[reportUnknownArgumentType] # legacy stub - self.kubernetes_pod_discovery, + self._core_kubernetes_pod_discovery, ), ) def _reset_custom_routing_strategy(self) -> None: - for attr in ("get_available_deployment", "async_get_available_deployment"): + for attr in ("get_available_deployment", "_core_async_get_available_deployment"): if attr in self.__dict__: delattr(self, attr) def flush_cache(self): litellm.cache = None - self.cache.flush_cache() + self._core_cache.flush_cache() session_in_memory_cache: Final = self._claude_code_session_router_cache.in_memory_cache if session_in_memory_cache is not None: session_in_memory_cache.flush_cache() - self.prompt_caching_pin_cache.in_memory_cache.flush_cache() + self._core_prompt_caching_pin_cache.in_memory_cache.flush_cache() def reset(self): ## clean up on close @@ -17146,3 +17365,16 @@ class Router: litellm._async_failure_callback = [] self.retry_policy = None self.flush_cache() + + +def _name_core_members_after_old_names() -> None: + # An old member that moved to ``_core_`` can still be reached as the function itself + # (``cached_deployment_model_info`` is an lru_cache over one), so errors that name the method + # keep the name it had before. + for name, member in vars(Router).items(): + old_name = name.removeprefix("_core_") + if old_name != name and inspect.isfunction(member) and hasattr(DeprecatedRouterMembers, old_name): + member.__qualname__ = f"Router.{old_name}" + + +_name_core_members_after_old_names() diff --git a/litellm/router_api.py b/litellm/router_api.py new file mode 100644 index 00000000000..22059ad0dcf --- /dev/null +++ b/litellm/router_api.py @@ -0,0 +1,1296 @@ +""" +The Router's entry points and the typed views they return. + +``litellm.Router`` exposes its endpoint methods (``acompletion``, ``aembedding``, ...) plus eight +entry points, and every other read or write that code outside the router needs goes through one +of them: + +- ``registry()`` -> ``RouterRegistry``: the configured deployments, indexed by id, model group, + alias, wildcard pattern and team, and the plugins they configure. +- ``settings()`` -> ``RouterSettings``: one frozen snapshot of the configuration a caller may read. +- ``health()`` -> ``RouterHealth``: cooldowns, blocked deployments, health checks and usage. +- ``select(model, ...)``: pick a deployment without calling it. +- ``apply(ChangeSet)`` / ``aapply(ChangeSet)``: runtime changes (deployments, settings, plugins), + returning an ``ApplyResult`` with the new registry version. +- ``observe(...)``: bind the router's routing observers to request logging. +- ``register_plugin(plugin)``: a custom routing strategy. +- ``retire()``: release the router's global registrations. + +The views read the router's state when they are called; they hold no copy of it. A Rust core +implements the same views (see litellm-rust/crates/router-core). Each query names the old Router +member it replaces in its docstring; ``DeprecatedRouterMembers`` keeps the old names working, typed +as they were. +""" + +# pyright: reportPrivateUsage=false, reportUnknownMemberType=false, reportUnknownVariableType=false +# pyright: reportMissingTypeArgument=false, reportUnknownParameterType=false +# The views are part of the router: they read and write the Router's ``_core_*`` members by design +# (see the router-core boundary in litellm/router.py), the same way Router's own methods do. Each +# selector query (``deployments(source=...)``, ``names(kind=...)``, ...) is overloaded per selector +# value and returns exactly the type of the ``_core_*`` member that value reads, which is the type +# the old public member had, so moving a caller onto the entry point keeps its types. Some of those +# members are still untyped (bare ``dict`` / ``list``) or mutable (the live objects); the overloads +# repeat them unchanged, and typing the members themselves is the core's job. + +import hashlib +import json +from collections.abc import Iterator, Mapping, Sequence +from collections.abc import Set as AbstractSet +from dataclasses import dataclass, field +from types import MappingProxyType +from typing import TYPE_CHECKING, Final, Literal, TypeAlias, overload + +if TYPE_CHECKING: + from typing import Any # noqa: TID251 # only to spell the cores' untyped (Unknown) containers and one Any type + + from litellm.caching.caching import DualCache, RedisCache + from litellm.integrations.SlackAlerting.slack_alerting import SlackAlerting + from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler + from litellm.router import Router + from litellm.router_strategy.adaptive_router.adaptive_router import AdaptiveRouter + from litellm.router_strategy.auto_router.auto_router import AutoRouter + from litellm.router_strategy.budget_limiter import RouterBudgetLimiting + from litellm.router_strategy.complexity_router.complexity_router import ComplexityRouter + from litellm.router_strategy.quality_router.quality_router import QualityRouter + from litellm.router_utils.cooldown_cache import CooldownCache + from litellm.router_utils.health_state_cache import DeploymentHealthCache + from litellm.router_utils.kubernetes_pod_discovery import KubernetesPodDiscovery + from litellm.router_utils.pattern_match_deployments import PatternMatchRouter + from litellm.router_utils.ptu_shares import PTUTeamCeiling + from litellm.types.router import ( + AllowedFailsPolicy, + Deployment, + DeploymentModelListingInfo, + DeploymentTypedDict, + FallbackAccessCheck, + FallbackBudgetCheck, + GuardrailTypedDict, + ModelGroupInfo, + PreRoutingHookResponse, + RouterGeneralSettings, + RouterModelGroupAliasItem, + RoutingGroup, + RoutingPlugin, + SearchToolTypedDict, + TaggedPreRoutingStrategy, + ) + from litellm.types.utils import BudgetConfig + from litellm.types.utils import ModelInfo as ModelMapInfo + + # The cores' untyped containers (bare ``list`` / ``dict``, element types Unknown), spelled with Any: + # an overload set with one partially-unknown overload makes every call through it partially unknown. + UntypedList: TypeAlias = list[Any] # pyright: ignore[reportExplicitAny] # see above + UntypedDict: TypeAlias = dict[Any, Any] # pyright: ignore[reportExplicitAny] # see above + + # What registry().plugins(kind) returns, one member per kind. + RegistryPlugins: TypeAlias = ( + dict[str, list[TaggedPreRoutingStrategy[AutoRouter]]] + | dict[str, list[TaggedPreRoutingStrategy[ComplexityRouter]]] + | dict[str, list[TaggedPreRoutingStrategy[QualityRouter]]] + | dict[str, list[TaggedPreRoutingStrategy[AdaptiveRouter]]] + | list[RoutingPlugin] + | RouterBudgetLimiting + | DualCache + | None + ) + + +DeploymentSource: TypeAlias = Literal[ + "group", + "all", + "config", + "named", + "alias", + "routing_groups", + "routed_group", + "wildcard", + "routable", + "first_in_group", + "for_request", +] +NameKind: TypeAlias = Literal[ + "ids", + "groups", + "group_set", + "deployment_names", + "team_public", + "litellm_models", + "litellm_base_models", + "wildcard", + "route_candidates", +] +ResolveKind: TypeAlias = Literal[ + "group", + "alias", + "served", + "from_id", + "team_model", + "recognized", + "upstream", + "group_of_id", + "wildcard_access_group", + "team_model_alternatives", + "routing_group_alternatives", + "search_tool_support", +] +GroupField: TypeAlias = Literal[ + "info", + "cached_info", + "display_name", + "mode", + "service_tiers", + "token_limits", + "listing_info", +] +CredentialForm: TypeAlias = Literal["params", "with_provider", "deployment"] +IndexName: TypeAlias = Literal["model_name", "team_model"] +PluginKind: TypeAlias = Literal[ + "auto", + "complexity", + "quality", + "adaptive", + "routing", + "budget_limiter", + "prompt_caching_pins", +] +SelectKind: TypeAlias = Literal["pick", "healthy", "pass_through"] +UsageKind: TypeAlias = Literal["current", "remaining"] + + +def deployment_id(model_group: str, litellm_params: Mapping[str, object]) -> str: + """ + The stable id of a deployment that has no ``model_info.id``: a sha256 over the model group and + every litellm_params key and value, in order. Replaces ``Router.generate_model_id``. + """ + parts: Final = [model_group] + for k, v in litellm_params.items(): + for item in (k, v): + if isinstance(item, str): + parts.append(item) + elif isinstance(item, dict): + parts.append(json.dumps(item, default=_json_default_stable_id)) + else: + parts.append(str(item)) + return hashlib.sha256("".join(parts).encode()).hexdigest() + + +def _json_default_stable_id(obj: object) -> str: + from litellm.router import Router + + return Router._json_default_stable_id(obj) + + +class RouterRegistry: + """ + Read-only queries over the configured deployments (``registry()``). + + ``version`` changes whenever the deployments change, so a caller can tell whether + what it read earlier is still current. + """ + + __slots__ = ("_router",) + + def __init__(self, router: "Router") -> None: + self._router = router + + @property + def version(self) -> int: + return self._router._registry_version + + # -- one deployment ------------------------------------------------------------------------- + + def deployment(self, id: str) -> "Deployment | None": + """The deployment with this ``model_info.id`` (was ``get_deployment``).""" + return self._router._core_get_deployment(model_id=id) + + def has_deployment(self, id: str) -> bool: + """Whether a deployment has this id (was ``has_model_id``).""" + return self._router._core_has_model_id(id) + + def deployment_dict( + self, id: str + ) -> "UntypedDict | None": # a mutable list/dict, as before: Router._core_get_model_info's type, the live dict + """The deployment as its model_list dict (was ``get_model_info``).""" + return self._router._core_get_model_info(id=id) + + def deployment_model_info(self, id: str, model_name: str, *, cached: bool = False) -> "ModelMapInfo | None": + """ + Model info for a deployment, merged with the pricing map (was + ``get_deployment_model_info``; ``cached=True`` was ``cached_deployment_model_info``). + """ + if cached: + return self._router._core_cached_deployment_model_info(id, model_name) + return self._router._core_get_deployment_model_info(model_id=id, model_name=model_name) + + def router_model_info( + self, + deployment: "dict[str, object] | Deployment | None", # mutable-ok: the core takes a dict + received_model_name: str, + id: str | None = None, + ) -> "ModelMapInfo": + """Model info used for routing decisions (was ``get_router_model_info``).""" + r: Final = self._router + # The core's overloads split (deployment, id) by which one is None; this forwards either form unchanged. + return r._core_get_router_model_info( # pyright: ignore[reportCallIssue] # see above + deployment=deployment, # pyright: ignore[reportArgumentType] # see above + received_model_name=received_model_name, + id=id, # pyright: ignore[reportArgumentType] # see above + ) + + def discovered_model_info(self, id: str | None) -> Mapping[str, int]: + """Limits discovered from the provider for a deployment (was ``get_discovered_model_info``).""" + return self._router._core_get_discovered_model_info(id) + + def zero_cost(self) -> Mapping[str, bool] | None: + """The per-deployment zero-cost lookup the auth checks read (was ``_zero_cost_cache``).""" + return getattr(self._router, "_zero_cost_cache", None) + + # -- many deployments ----------------------------------------------------------------------- + + @overload + def deployments( + self, + model_name: str | None = None, + *, + source: Literal["group"] = "group", + team_id: str | None = None, + model_alias: str | None = None, + request: Mapping[str, object] | None = None, + call_type: str | None = None, + ) -> "list[DeploymentTypedDict] | None": ... # mutable-ok: Router._core_get_model_list's type, the live lists + + @overload + def deployments( + self, + model_name: str | None = None, + *, + source: Literal["alias"], + team_id: str | None = None, + model_alias: str | None = None, + request: Mapping[str, object] | None = None, + call_type: str | None = None, + ) -> "list[DeploymentTypedDict]": ... # mutable-ok: Router._core_get_model_list_from_model_alias's type + + @overload + def deployments( + self, + model_name: str, + *, + source: Literal["all", "routed_group"], + team_id: str | None = None, + model_alias: str | None = None, + request: Mapping[str, object] | None = None, + call_type: str | None = None, + ) -> "list[DeploymentTypedDict]": ... # mutable-ok: the cores' type (get_all_deployments, ...) + + @overload + def deployments( + self, + model_name: str | None = None, + *, + source: Literal["config"], + team_id: str | None = None, + model_alias: str | None = None, + request: Mapping[str, object] | None = None, + call_type: str | None = None, + ) -> Iterator[Mapping[str, object]]: ... + + @overload + def deployments( + self, + model_name: str | None = None, + *, + source: Literal["routing_groups"], + team_id: str | None = None, + model_alias: str | None = None, + request: Mapping[str, object] | None = None, + call_type: str | None = None, + ) -> "Sequence[DeploymentTypedDict]": ... + + @overload + def deployments( + self, + model_name: str, + *, + source: Literal["for_request"], + team_id: str | None = None, + model_alias: str | None = None, + request: Mapping[str, object] | None = None, + call_type: str | None = None, + ) -> "Sequence[DeploymentTypedDict]": ... + + @overload + def deployments( + self, + model_name: str, + *, + source: Literal["named"], + team_id: str | None = None, + model_alias: str | None = None, + request: Mapping[str, object] | None = None, + call_type: str | None = None, + ) -> "tuple[DeploymentTypedDict, ...] | None": ... + + @overload + def deployments( + self, + model_name: str, + *, + source: Literal["routable"], + team_id: str | None = None, + model_alias: str | None = None, + request: Mapping[str, object] | None = None, + call_type: str | None = None, + ) -> "tuple[DeploymentTypedDict, ...]": ... + + @overload + def deployments( + self, + model_name: str, + *, + source: Literal["wildcard"], + team_id: str | None = None, + model_alias: str | None = None, + request: Mapping[str, object] | None = None, + call_type: str | None = None, + ) -> Sequence[Mapping[str, object]]: ... + + @overload + def deployments( + self, + model_name: str, + *, + source: Literal["first_in_group"], + team_id: str | None = None, + model_alias: str | None = None, + request: Mapping[str, object] | None = None, + call_type: str | None = None, + ) -> "Deployment | None": ... + + def deployments( + self, + model_name: str | None = None, + *, + source: DeploymentSource = "group", + team_id: str | None = None, + model_alias: str | None = None, + request: Mapping[str, object] | None = None, + call_type: str | None = None, + ) -> "Sequence[DeploymentTypedDict] | Sequence[Mapping[str, object]] | Iterator[Mapping[str, object]] | Deployment | None": + """ + Deployments by name. ``source`` picks which index answers: + + - ``group``: the group, its aliases, routing groups, then wildcards; every deployment when + ``model_name`` is None (was ``get_model_list``) + - ``all``: every deployment of the group, with ``model_alias`` (was ``get_all_deployments``) + - ``config``: deployments that came from config rather than the DB (was ``config_deployments``) + - ``named``: the group index entry, or None (was ``deployments_named``) + - ``alias`` / ``routing_groups`` / ``routed_group``: (was ``get_model_list_from_model_alias``, + ``get_model_list_from_routing_groups``, ``get_model_list_of_routed_group``) + - ``wildcard``: deployments a wildcard route serves (was ``wildcard_route_deployments``) + - ``routable``: deployments that pass the routing checks for ``request`` and, when given, + the endpoint ``call_type`` (was ``get_routable_deployments``) + - ``first_in_group``: the first deployment of the group (was ``get_deployment_by_model_group_name``) + - ``for_request``: the deployments a request may use (was ``deployments_for_request``) + """ + r: Final = self._router + if source == "group": + return r._core_get_model_list(model_name=model_name, team_id=team_id) + if source == "config": + return r._core_config_deployments() + if source == "alias": + return r._core_get_model_list_from_model_alias(model_name=model_name) + if source == "routing_groups": + return r._core_get_model_list_from_routing_groups(model_name=model_name) + if model_name is None: + raise TypeError(f"registry().deployments(source={source!r}) needs a model_name") + if source == "all": + return r._core_get_all_deployments(model_name=model_name, model_alias=model_alias, team_id=team_id) + if source == "named": + return r._core_deployments_named(model_name) + if source == "routed_group": + return r._core_get_model_list_of_routed_group(model_name, team_id) + if source == "wildcard": + return r._core_wildcard_route_deployments(model_name, team_id) + if source == "routable": + return r._core_get_routable_deployments( + model_name, MappingProxyType({}) if request is None else request, call_type + ) + if source == "first_in_group": + return r._core_get_deployment_by_model_group_name(model_group_name=model_name) + if source == "for_request": + return r._core_deployments_for_request(model_name, request or {}) + raise ValueError(f"unknown deployment source {source!r}") + + def all(self) -> "UntypedList": # a mutable list/dict, as before: the model_list attribute's type, the live list + """The live model list, in configured order (was the ``model_list`` attribute).""" + return self._router._core_model_list + + def includes( + self, + deployment: "DeploymentTypedDict | dict[str, object]", # mutable-ok: the core takes a dict + *, + model_name: str | None = None, + team_id: str | None = None, + ) -> bool: + """ + Whether a deployment serves ``model_name`` for ``team_id`` (was + ``should_include_deployment``), or, without ``model_name``, whether the team may use it at + all (was ``deployment_usable_by_team``). + """ + from litellm.router import Router + + if model_name is None: + return Router._core_deployment_usable_by_team(deployment, team_id) + return self._router._core_should_include_deployment(model_name=model_name, model=deployment, team_id=team_id) + + @overload + def index(self, name: Literal["model_name"]) -> dict[str, list[int]]: ... # mutable-ok: the live index + + @overload + def index(self, name: Literal["team_model"]) -> dict[tuple[str, str], list[int]]: ... # mutable-ok: the live index + + def index(self, name: IndexName) -> Mapping[str, Sequence[int]] | Mapping[tuple[str, str], Sequence[int]]: + """ + The raw name indexes: ``model_name`` maps a group name to model_list positions (was + ``model_name_to_deployment_indices``), ``team_model`` maps (team, public name) keys (was + ``team_model_to_deployment_indices``). + """ + if name == "model_name": + return self._router._core_model_name_to_deployment_indices + if name == "team_model": + return self._router._core_team_model_to_deployment_indices + raise ValueError(f"unknown index {name!r}") + + # -- names ---------------------------------------------------------------------------------- + + @overload + def names( + self, + model_name: str | None = None, + *, + kind: Literal["groups", "ids"] = "groups", + team_id: str | None = None, + exclude_team_models: bool = False, + ) -> list[str]: ... # mutable-ok: the cores' type (get_model_names, get_model_ids), a fresh list + + @overload + def names( + self, + model_name: str | None = None, + *, + kind: Literal["group_set"], + team_id: str | None = None, + exclude_team_models: bool = False, + ) -> set[str]: ... # mutable-ok: the model_names attribute's type, the live set + + @overload + def names( + self, + model_name: str | None = None, + *, + kind: Literal["deployment_names"], + team_id: str | None = None, + exclude_team_models: bool = False, + ) -> "UntypedList": ... # a mutable list/dict, as before: the deployment_names attribute's type, the live list + + @overload + def names( + self, + model_name: str | None = None, + *, + kind: Literal["team_public"], + team_id: str | None = None, + exclude_team_models: bool = False, + ) -> frozenset[str]: ... + + @overload + def names( + self, + model_name: str, + *, + kind: Literal["wildcard", "route_candidates"], + team_id: str | None = None, + exclude_team_models: bool = False, + ) -> frozenset[str]: ... + + @overload + def names( + self, + model_name: str, + *, + kind: Literal["litellm_models", "litellm_base_models"], + team_id: str | None = None, + exclude_team_models: bool = False, + ) -> tuple[str, ...]: ... + + def names( + self, + model_name: str | None = None, + *, + kind: NameKind = "groups", + team_id: str | None = None, + exclude_team_models: bool = False, + ) -> Sequence[str] | AbstractSet[str]: + """ + Names known to the router. ``kind``: + + - ``groups``: every routable model name, with team public names for ``team_id`` + (was ``get_model_names``) + - ``group_set``: the set of configured group names (was the ``model_names`` attribute) + - ``ids``: deployment ids, of one group or all (was ``get_model_ids``) + - ``deployment_names``: (was the ``deployment_names`` attribute) + - ``team_public``: team public model names (was ``team_public_model_names``) + - ``litellm_models``: upstream model names a group resolves to (was ``resolved_litellm_models``) + - ``litellm_base_models``: the same, with each deployment's ``base_model`` where it sets one + (was ``resolved_litellm_models(..., use_base_model=True)``) + - ``wildcard``: names a wildcard route serves (was ``wildcard_routed_models``) + - ``route_candidates``: deployment ids a request for the name may reach + (was ``get_candidate_model_ids_for_route``) + """ + r: Final = self._router + if kind == "groups": + return r._core_get_model_names(team_id=team_id) + if kind == "group_set": + return r._core_model_names + if kind == "ids": + return r._core_get_model_ids(model_name=model_name, exclude_team_models=exclude_team_models) + if kind == "deployment_names": + return r._core_deployment_names + if kind == "team_public": + return r._core_team_public_model_names + if model_name is None: + raise TypeError(f"registry().names(kind={kind!r}) needs a model_name") + if kind == "litellm_models": + return r._core_resolved_litellm_models(model_name, team_id=team_id) + if kind == "litellm_base_models": + return r._core_resolved_litellm_models(model_name, team_id=team_id, use_base_model=True) + if kind == "wildcard": + return r._core_wildcard_routed_models(model_name, team_id) + if kind == "route_candidates": + return r._core_get_candidate_model_ids_for_route(model_name, team_id=team_id) + raise ValueError(f"unknown name kind {kind!r}") + + # -- resolving a name ----------------------------------------------------------------------- + + @overload + def resolve( + self, + name: str, + *, + kind: Literal["group", "served"] = "group", + team_id: str | None = None, + custom_llm_provider: str | None = None, + served_model: str | None = None, + ) -> str: ... + + @overload + def resolve( + self, + name: str, + *, + kind: Literal["alias", "upstream"], + team_id: str | None = None, + custom_llm_provider: str | None = None, + served_model: str | None = None, + ) -> str | None: ... + + @overload + def resolve( + self, + name: str, + *, + kind: Literal["search_tool_support"], + team_id: str | None = None, + custom_llm_provider: str | None = None, + served_model: str | None = None, + ) -> tuple[bool | None, ...]: ... + + @overload + def resolve( + self, + name: str | None, + *, + kind: Literal["from_id", "team_model"], + team_id: str | None = None, + custom_llm_provider: str | None = None, + served_model: str | None = None, + ) -> str | None: ... + + @overload + def resolve( + self, + name: str, + *, + kind: Literal["recognized", "wildcard_access_group", "team_model_alternatives"], + team_id: str | None = None, + custom_llm_provider: str | None = None, + served_model: str | None = None, + ) -> bool: ... + + @overload + def resolve( + self, + name: str | None, + *, + kind: Literal["routing_group_alternatives"], + team_id: str | None = None, + custom_llm_provider: str | None = None, + served_model: str | None = None, + ) -> bool: ... + + @overload + def resolve( + self, + name: str, + *, + kind: Literal["group_of_id"], + team_id: str | None = None, + custom_llm_provider: str | None = None, + served_model: str | None = None, + ) -> "UntypedList | None": ... # a mutable list/dict, as before: Router._core_get_model_group's type, unchanged + + def resolve( + self, + name: str | None, + *, + kind: ResolveKind = "group", + team_id: str | None = None, + custom_llm_provider: str | None = None, + served_model: str | None = None, + ) -> str | bool | Sequence[object] | None: + """ + Resolve a requested name. ``kind``: + + - ``group``: the model group a name routes to (was ``routable_model_group``) + - ``alias``: the target of a model_group_alias, or None (was ``get_model_from_alias``) + - ``served``: the name to report for ``served_model`` (was ``resolve_served_model_name``) + - ``from_id``: the model name of a deployment id (was ``resolve_model_name_from_model_id``) + - ``team_model``: a team's public name to its group (was ``map_team_model``; ``team_id`` required) + - ``recognized``: whether the router knows the name (was ``is_recognized_model``) + - ``upstream``: the upstream model a name routes to (was ``get_routable_upstream_model``) + - ``group_of_id``: the group of a deployment id (was ``get_model_group``) + - ``wildcard_access_group``: whether an access group grants a wildcard route + (was ``is_model_access_group_for_wildcard_route``) + - ``team_model_alternatives``: whether a team deployment has siblings to fall back to + (was ``team_model_has_alternatives``) + - ``routing_group_alternatives``: (was ``routing_group_has_alternatives``) + - ``search_tool_support``: per routable deployment, whether it answers Codex's client + ``tool_search`` (was ``get_search_tool_support``) + """ + r: Final = self._router + if kind == "from_id": + return r._core_resolve_model_name_from_model_id(name, custom_llm_provider=custom_llm_provider) + if kind == "team_model": + if team_id is None: + raise TypeError("registry().resolve(kind='team_model') needs a team_id") + return r._core_map_team_model(name, team_id, custom_llm_provider=custom_llm_provider) + if kind == "routing_group_alternatives": + return r._core_routing_group_has_alternatives(name) + if name is None: + raise TypeError(f"registry().resolve(kind={kind!r}) needs a name") + if kind == "group": + return r._core_routable_model_group(name) + if kind == "alias": + return r._core_get_model_from_alias(model=name) + if kind == "served": + # Without a served_model the name is reported as requested, as the core does for a non-match. + return name if served_model is None else r._core_resolve_served_model_name(name, served_model) + if kind == "recognized": + return r._core_is_recognized_model(name) + if kind == "upstream": + return r._core_get_routable_upstream_model(name, team_id=team_id) + if kind == "search_tool_support": + return r._core_get_search_tool_support(name, team_id=team_id) + if kind == "group_of_id": + return r._core_get_model_group(id=name) + if kind == "wildcard_access_group": + return r._core_is_model_access_group_for_wildcard_route(model_access_group=name) + if kind == "team_model_alternatives": + return r._core_team_model_has_alternatives(name) + raise ValueError(f"unknown resolve kind {kind!r}") + + def aliases( + self, + ) -> "dict[str, str | RouterModelGroupAliasItem]": # mutable-ok: the model_group_alias attribute, the live dict + """model_group_alias as configured (was the ``model_group_alias`` attribute).""" + return self._router._core_model_group_alias + + @overload + def patterns(self, *, team: Literal[False] = False) -> "PatternMatchRouter": ... + + @overload + def patterns(self, *, team: Literal[True]) -> "dict[str, PatternMatchRouter]": ... # mutable-ok: the live dict + + @overload + def patterns(self, *, team: bool) -> "PatternMatchRouter | dict[str, PatternMatchRouter]": ... # mutable-ok: same + + def patterns(self, *, team: bool = False) -> "PatternMatchRouter | Mapping[str, PatternMatchRouter]": + """The wildcard pattern router, or the per-team ones (was ``pattern_router`` / ``team_pattern_routers``).""" + return self._router._core_team_pattern_routers if team else self._router._core_pattern_router + + # -- groups --------------------------------------------------------------------------------- + + @overload + def group( + self, + name: str, + *, + field: Literal["info", "cached_info"] = "info", + include_hidden: bool = False, + team_id: str | None = None, + ) -> "ModelGroupInfo | None": ... + + @overload + def group( + self, + name: str, + *, + field: Literal["display_name", "mode"], + include_hidden: bool = False, + team_id: str | None = None, + ) -> str | None: ... + + @overload + def group( + self, + name: str, + *, + field: Literal["service_tiers"], + include_hidden: bool = False, + team_id: str | None = None, + ) -> tuple[object, ...]: ... + + @overload + def group( + self, + name: str, + *, + field: Literal["token_limits"], + include_hidden: bool = False, + team_id: str | None = None, + ) -> tuple[int | None, int | None]: ... + + @overload + def group( + self, + name: str, + *, + field: Literal["listing_info"], + include_hidden: bool = False, + team_id: str | None = None, + ) -> "DeploymentModelListingInfo | None": ... + + def group( + self, + name: str, + *, + field: GroupField = "info", + include_hidden: bool = False, + team_id: str | None = None, + ) -> ( + "ModelGroupInfo | DeploymentModelListingInfo | tuple[int | None, int | None] | tuple[object, ...] | str | None" + ): + """ + What the router knows about a model group. ``field``: + + - ``info``: the group's merged model info (was ``get_model_group_info``) + - ``cached_info``: (was ``cached_model_group_info``) + - ``display_name`` / ``mode`` / ``token_limits`` / ``listing_info``: configured values + (were ``get_configured_display_name``, ``get_configured_mode``, + ``get_configured_token_limits``, ``get_model_listing_info``) + - ``service_tiers``: (was ``get_configured_service_tiers``) + """ + r: Final = self._router + if field == "info": + return r._core_get_model_group_info(name, include_hidden=include_hidden, team_id=team_id) + if field == "cached_info": + return r._core_cached_model_group_info(name) + if field == "display_name": + return r._core_get_configured_display_name(name) + if field == "mode": + return r._core_get_configured_mode(name) + if field == "service_tiers": + return r._core_get_configured_service_tiers(name, team_id=team_id) + if field == "token_limits": + return r._core_get_configured_token_limits(name) + if field == "listing_info": + return r._core_get_model_listing_info(name) + raise ValueError(f"unknown group field {field!r}") + + def routing_groups(self) -> "dict[str, RoutingGroup]": # mutable-ok: the routing_groups attribute, the live dict + """Routing groups by name (was the ``routing_groups`` attribute).""" + return self._router._core_routing_groups + + def access_groups( + self, + model_name: str | None = None, + model_access_group: str | None = None, + team_id: str | None = None, + ) -> dict[str, list[str]]: # mutable-ok: Router._core_get_model_access_groups's type, a fresh dict + """Access group name to model names (was ``get_model_access_groups``).""" + return self._router._core_get_model_access_groups( + model_name=model_name, model_access_group=model_access_group, team_id=team_id + ) + + @overload + def credentials( + self, id: str, *, form: Literal["params"] = "params", team_id: str | None = None + ) -> ( + "UntypedDict | None" + ): ... # a mutable list/dict, as before: Router._core_get_deployment_credentials's type, unchanged + + @overload + def credentials( + self, id: str, *, form: Literal["with_provider"], team_id: str | None = None + ) -> "dict[str, Any] | None": ... # pyright: ignore[reportExplicitAny] # the core's type, unchanged # mutable-ok: same + + @overload + def credentials( + self, id: str, *, form: Literal["deployment"], team_id: str | None = None + ) -> "Deployment | None": ... + + def credentials( + self, id: str, *, form: CredentialForm = "params", team_id: str | None = None + ) -> "Mapping[str, object] | Deployment | None": + """ + A deployment's credentials. ``form``: ``params`` (was ``get_deployment_credentials``), + ``with_provider`` (was ``get_deployment_credentials_with_provider``), ``deployment``, the + deployment that owns the credential (was ``get_credential_deployment``). + """ + r: Final = self._router + if form == "params": + return r._core_get_deployment_credentials(model_id=id) + if form == "with_provider": + return r._core_get_deployment_credentials_with_provider(model_id=id, team_id=team_id) + if form == "deployment": + return r._core_get_credential_deployment(model_id=id, team_id=team_id) + raise ValueError(f"unknown credential form {form!r}") + + @overload + def plugins( + self, kind: Literal["auto"] + ) -> "dict[str, list[TaggedPreRoutingStrategy[AutoRouter]]]": ... # mutable-ok: the live dict + + @overload + def plugins( + self, kind: Literal["complexity"] + ) -> "dict[str, list[TaggedPreRoutingStrategy[ComplexityRouter]]]": ... # mutable-ok: the live dict + + @overload + def plugins( + self, kind: Literal["quality"] + ) -> "dict[str, list[TaggedPreRoutingStrategy[QualityRouter]]]": ... # mutable-ok: the live dict + + @overload + def plugins( + self, kind: Literal["adaptive"] + ) -> "dict[str, list[TaggedPreRoutingStrategy[AdaptiveRouter]]]": ... # mutable-ok: the live dict + + @overload + def plugins(self, kind: Literal["routing"]) -> "list[RoutingPlugin]": ... # mutable-ok: the live list + + @overload + def plugins(self, kind: Literal["budget_limiter"]) -> "RouterBudgetLimiting | None": ... + + @overload + def plugins(self, kind: Literal["prompt_caching_pins"]) -> "DualCache": ... + + def plugins(self, kind: PluginKind) -> "RegistryPlugins": + """ + Plugins configured by the deployments: ``auto``, ``complexity``, ``quality`` and ``adaptive`` + sub-routers by deployment name, ``routing`` plugins, the deployment ``budget_limiter`` and + the ``prompt_caching_pins`` store (were ``auto_routers``, ``complexity_routers``, + ``quality_routers``, ``adaptive_routers``, ``routing_plugins``, + ``get_router_deployment_budget_limiter`` and ``prompt_caching_pin_cache``). + """ + r: Final = self._router + if kind == "auto": + return r._core_auto_routers + if kind == "complexity": + return r._core_complexity_routers + if kind == "quality": + return r._core_quality_routers + if kind == "adaptive": + return r._core_adaptive_routers + if kind == "routing": + return r._core_routing_plugins + if kind == "budget_limiter": + return r._core_get_router_deployment_budget_limiter() + if kind == "prompt_caching_pins": + return r._core_prompt_caching_pin_cache + raise ValueError(f"unknown plugin kind {kind!r}") + + +# Settings a caller may read. Assigning one from outside the router goes through apply(ChangeSet(assign=...)). +SETTING_FIELDS: Final[tuple[str, ...]] = ( + "routing_strategy", + "fallbacks", + "context_window_fallbacks", + "content_policy_fallbacks", + "default_deployment", + "default_litellm_params", + "default_max_parallel_requests", + "cooldown_time", + "allowed_fails", + "allowed_fails_policy", + "disable_cooldowns", + "enable_pre_call_checks", + "enable_health_check_routing", + "health_check_ignore_transient_errors", + "background_health_check_model_groups", + "enable_tag_filtering", + "tag_filtering_match_any", + "tag_routing_prefix", + "router_general_settings", + "provider_budget_config", + "provider_default_deployment_ids", + "fallback_access_check", + "fallback_budget_check", + "kubernetes_pod_discovery", + "search_tools", + "guardrail_list", + "cache_responses", +) + +_MISSING: Final = object() + + +@dataclass(frozen=True) +class RouterSettings: + """ + The router's configuration as one frozen snapshot (``settings()``). Fields hold the router's + own objects, not copies: mutating a list in here mutates the router's. + A field the router never set reads as ``None``. + + Each field has the type of the Router attribute it reads (``_core_``), the type the old + ``router.`` attribute had; the mutable ones are the router's live objects. ``of`` fills + every field. The ``None`` defaults exist for snapshots built directly (tests), which set only the + fields they need; the fields whose Router attribute cannot be ``None`` say so on their line. + """ + + routing_strategy: str | None = None + fallbacks: "list | list[dict[str, list[str] | list | None]] | None" = None # mutable-ok: the router's live list + context_window_fallbacks: list | None = None # mutable-ok: the router's live list + content_policy_fallbacks: list | None = None # mutable-ok: the router's live list + default_deployment: object = None + default_litellm_params: dict = None # pyright: ignore[reportAssignmentType] # unset only when built directly (see above) # mutable-ok: the router's live dict + default_max_parallel_requests: int | None = None + cooldown_time: float = None # pyright: ignore[reportAssignmentType] # unset only when built directly (see above) + allowed_fails: int = None # pyright: ignore[reportAssignmentType] # unset only when built directly (see above) + allowed_fails_policy: "AllowedFailsPolicy | None" = None + disable_cooldowns: bool | None = None + enable_pre_call_checks: bool = None # pyright: ignore[reportAssignmentType] # unset only when built directly (see above) + enable_health_check_routing: bool = None # pyright: ignore[reportAssignmentType] # unset only when built directly (see above) + health_check_ignore_transient_errors: bool = None # pyright: ignore[reportAssignmentType] # unset only when built directly (see above) + background_health_check_model_groups: frozenset[str] | None = None + enable_tag_filtering: bool = None # pyright: ignore[reportAssignmentType] # unset only when built directly (see above) + tag_filtering_match_any: bool = None # pyright: ignore[reportAssignmentType] # unset only when built directly (see above) + tag_routing_prefix: str = None # pyright: ignore[reportAssignmentType] # unset only when built directly (see above) + router_general_settings: "RouterGeneralSettings" = None # pyright: ignore[reportAssignmentType] # unset only when built directly (see above) + provider_budget_config: "dict[str, BudgetConfig] | None" = None # mutable-ok: the router's live dict + provider_default_deployment_ids: list[str] = None # pyright: ignore[reportAssignmentType] # unset only when built directly (see above) # mutable-ok: the router's live list + fallback_access_check: "FallbackAccessCheck | None" = None + fallback_budget_check: "FallbackBudgetCheck | None" = None + kubernetes_pod_discovery: "KubernetesPodDiscovery" = None # pyright: ignore[reportAssignmentType] # unset only when built directly (see above) + search_tools: "list[SearchToolTypedDict]" = None # pyright: ignore[reportAssignmentType] # unset only when built directly (see above) # mutable-ok: the router's live list + guardrail_list: "list[GuardrailTypedDict]" = None # pyright: ignore[reportAssignmentType] # unset only when built directly (see above) # mutable-ok: the router's live list + cache_responses: bool | None = None + _router: "Router | None" = field(default=None, repr=False, compare=False) + _unset: frozenset[str] = field(default=frozenset(), repr=False, compare=False) + + @classmethod + def of(cls, router: "Router") -> "RouterSettings": + read: Final = {name: getattr(router, "_core_" + name, _MISSING) for name in SETTING_FIELDS} + values: Final = {name: None if value is _MISSING else value for name, value in read.items()} + unset: Final = frozenset(name for name, value in read.items() if value is _MISSING) + # Each value is the Router's own ``_core_`` attribute, typed as the field (or None when unset). + return cls(**values, _router=router, _unset=unset) # pyright: ignore[reportArgumentType] # see above + + def _owner(self, member: str) -> "Router": + if self._router is None: + raise AttributeError(f"'NoneType' object has no attribute {member!r}") + return self._router + + def is_set(self, name: str) -> bool: + """Whether the router has this setting at all (some exist only in some configurations).""" + return name in SETTING_FIELDS and name not in self._unset + + def as_dict(self) -> Mapping[str, object]: + """The runtime-updatable settings and routing groups as plain values (was ``get_settings``).""" + return self._owner("_core_get_settings")._core_get_settings() + + def allowed_fails_for(self, exception: Exception) -> int | None: + """Allowed fails for this exception under allowed_fails_policy (was ``get_allowed_fails_from_policy``).""" + return self._owner("_core_get_allowed_fails_from_policy")._core_get_allowed_fails_from_policy( + exception=exception + ) + + def guardrail(self, name: str) -> "GuardrailTypedDict": + """The guardrail configured under this name (was ``get_available_guardrail``).""" + return self._owner("_core_get_available_guardrail")._core_get_available_guardrail(name) + + +class RouterHealth: + """Cooldowns, blocked deployments, health checks and usage (``health()``).""" + + __slots__ = ("_router",) + + def __init__(self, router: "Router") -> None: + self._router = router + + @property + def store(self) -> "DualCache": + """ + The cache that holds routing state (cooldowns, fail counts, usage counters, latency): + in-memory plus the router's Redis when configured. Read and write it with the router's own + key names. Replaces the ``cache`` attribute. + """ + return self._router._core_cache + + @property + def cooldowns(self) -> "CooldownCache": + """The cooldown store (was ``cooldown_cache``).""" + return self._router._core_cooldown_cache + + @property + def health_checks(self) -> "DeploymentHealthCache": + """Background health check results (was ``health_state_cache``).""" + return self._router._core_health_state_cache + + @property + def latency( + self, + ) -> "UntypedDict": # a mutable list/dict, as before: the deployment_latency_map attribute's type, the live dict + """Latency samples by deployment (was ``deployment_latency_map``).""" + return self._router._core_deployment_latency_map + + @staticmethod + def is_blocked(deployment: "Deployment") -> bool: + """Whether an admin paused this deployment (was ``is_deployment_blocked``).""" + from litellm.router import Router + + return Router._core_is_deployment_blocked(deployment) + + @staticmethod + def all_blocked(deployments: "Sequence[DeploymentTypedDict]") -> bool: + """Whether every deployment in the list is paused (was ``are_all_deployments_blocked``).""" + from litellm.router import Router + + return Router._core_are_all_deployments_blocked(deployments) + + def fully_blocked_model_names(self) -> set[str]: # mutable-ok: the core's type, a fresh set + """Groups whose every deployment is paused (was ``get_fully_blocked_model_names``).""" + return self._router._core_get_fully_blocked_model_names() + + async def fully_unhealthy_model_names(self) -> set[str]: # mutable-ok: the core's type, a fresh set + """Groups whose every deployment failed its health check (was ``async_get_fully_unhealthy_model_names``).""" + return await self._router._core_async_get_fully_unhealthy_model_names() + + @overload + async def usage( + self, model_group: str, *, kind: Literal["current"] = "current" + ) -> tuple[int | None, int | None]: ... + + @overload + async def usage( + self, model_group: str, *, kind: Literal["remaining"] + ) -> dict[str, int]: ... # mutable-ok: Router._core_get_remaining_model_group_usage's type, a fresh dict + + async def usage( + self, model_group: str, *, kind: UsageKind = "current" + ) -> tuple[int | None, int | None] | Mapping[str, int]: + """ + tpm/rpm usage of a group this minute (``current``, was ``get_model_group_usage``) or what is + left of its limits (``remaining``, was ``get_remaining_model_group_usage``). + """ + if kind == "current": + return await self._router._core_get_model_group_usage(model_group=model_group) + if kind == "remaining": + return await self._router._core_get_remaining_model_group_usage(model_group=model_group) + raise ValueError(f"unknown usage kind {kind!r}") + + def team_ptu_ceiling(self, team_id: str, model_group: str) -> "PTUTeamCeiling | None": + """A team's PTU share ceiling on a group (was ``team_ptu_ceiling``).""" + return self._router._core_team_ptu_ceiling(team_id, model_group) + + def split_priority(self, model: str, kwargs: Mapping[str, object]) -> tuple[int | None, Mapping[str, object]]: + """The request priority and the kwargs without it (was ``split_request_priority``).""" + return self._router._core_split_request_priority(model, kwargs) + + +@dataclass +class ChangeSet: + """ + Runtime changes for ``apply``. Applied in this order: ``assign``, ``settings``, ``upsert``, + ``delete``, ``redis_cache``. + + - ``assign``: replace settings fields as they are (what ``router. = value`` did) + - ``settings``: validated runtime settings (what ``update_settings(**settings)`` did) + - ``upsert``: add or replace deployments (``upsert_deployment``) + - ``delete``: remove deployments by id (``delete_deployment``) + - ``redis_cache``: point the routing state at a new Redis cache (``update_redis_cache``) + - ``refresh_model_info``: re-read discovered model info (``arefresh_model_info``; ``aapply`` only) + """ + + assign: Mapping[str, object] = field(default_factory=dict) + settings: Mapping[str, object] = field(default_factory=dict) + upsert: Sequence["Deployment"] = () + delete: Sequence[str] = () + redis_cache: "RedisCache | None" = None + refresh_model_info: bool = False + refresh_client: "AsyncHTTPHandler | None" = None + + +@dataclass +class ApplyResult: + """What ``apply`` did: the registry version after it, and per upsert/delete the old deployment.""" + + version: int + upserted: "Sequence[Deployment | None]" = field(default_factory=list) + deleted: "Sequence[Deployment | None]" = field(default_factory=list) + + +@dataclass(frozen=True) +class RoutingObservers: + """The router's routing observers (``observe()``).""" + + slack_alerting_logger: "SlackAlerting | None" = None + + +def apply_changes(router: "Router", changes: ChangeSet) -> ApplyResult: + if changes.refresh_model_info: + raise TypeError("ChangeSet(refresh_model_info=True) needs aapply()") + unknown: Final = sorted(set(changes.assign) - set(SETTING_FIELDS)) + if unknown: + raise ValueError(f"not assignable router settings: {unknown}") + for name, value in changes.assign.items(): + setattr(router, "_core_" + name, value) + if changes.settings: + router._core_update_settings(**changes.settings) + upserted: Final = [router._core_upsert_deployment(deployment=d) for d in changes.upsert] + deleted: Final = [router._core_delete_deployment(id=i) for i in changes.delete] + if changes.redis_cache is not None: + router._core_update_redis_cache(cache=changes.redis_cache) + return ApplyResult(version=router._registry_version, upserted=upserted, deleted=deleted) + + +async def aapply_changes(router: "Router", changes: ChangeSet) -> ApplyResult: + refresh: Final = changes.refresh_model_info + result: Final = apply_changes( + router, + ChangeSet( + assign=changes.assign, + settings=changes.settings, + upsert=changes.upsert, + delete=changes.delete, + redis_cache=changes.redis_cache, + ), + ) + if refresh: + await router._core_arefresh_model_info(client=changes.refresh_client) + return result + + +class RoutingContext: + """ + What a routing plugin (strategy, pre-call check, sub-router) may use of the router. Plugins get + one instead of the Router itself. + """ + + __slots__ = ("_router",) + + def __init__(self, router: "Router") -> None: + self._router = router + + def registry(self) -> RouterRegistry: + return self._router.registry() + + def settings(self) -> RouterSettings: + return self._router.settings() + + def health(self) -> RouterHealth: + return self._router.health() + + def pre_call_checks( + self, + model: str, + healthy_deployments: list[object], # mutable-ok: Router._pre_call_checks takes and filters a list + messages: list[dict[str, str]] | None = None, # mutable-ok: Router._pre_call_checks takes a list + input: str | list[object] | None = None, # mutable-ok: Router._pre_call_checks takes a list + request_kwargs: dict[str, object] | None = None, # mutable-ok: Router._pre_call_checks takes a dict + input_token_count: int | None = None, + skip_inline_token_count: bool = False, + request_deployments: "Sequence[DeploymentTypedDict] | None" = None, + ) -> Sequence[object]: + return self._router._pre_call_checks( + model=model, + healthy_deployments=healthy_deployments, + messages=messages, + input=input, + request_kwargs=request_kwargs, + input_token_count=input_token_count, + skip_inline_token_count=skip_inline_token_count, + request_deployments=request_deployments, + ) + + def count_pre_call_check_tokens( + self, + messages: Sequence[Mapping[str, object]] | None, + input: str | list[object] | None, # mutable-ok: Router._count_pre_call_check_tokens takes a list + request_kwargs: Mapping[str, object] | None = None, + ) -> int: + return self._router._count_pre_call_check_tokens(messages, input, request_kwargs) + + def update_kwargs_before_fallbacks( + self, + model: str, + kwargs: dict[str, object], # mutable-ok: the core updates the request kwargs in place + metadata_variable_name: str | None = "metadata", + ) -> None: + return self._router._update_kwargs_before_fallbacks(model, kwargs, metadata_variable_name) + + def arm_read_prefetch( + self, + model: str, + request_kwargs: dict[str, object] | None = None, # mutable-ok: the core takes a dict + ) -> None: + """(was ``arm_routing_read_prefetch``)""" + return self._router._core_arm_routing_read_prefetch(model, request_kwargs) + + async def pre_routing_hook( + self, + model: str, + request_kwargs: dict[str, object], # mutable-ok: Router._core_async_pre_routing_hook takes a dict + messages: list[dict[str, object]] | None = None, # mutable-ok: the core takes a list + input: str | list[object] | None = None, # mutable-ok: Router._core_async_pre_routing_hook takes a list + specific_deployment: bool | None = False, + ) -> "PreRoutingHookResponse | None": + """(was ``async_pre_routing_hook``)""" + return await self._router._core_async_pre_routing_hook( + model=model, + request_kwargs=request_kwargs, + messages=messages, + input=input, + specific_deployment=specific_deployment, + ) + + def log_retry( + self, + kwargs: dict[str, object], # mutable-ok: Router._core_log_retry records the retry into the request kwargs + e: Exception, + ) -> Mapping[str, object]: + return self._router._core_log_retry(kwargs=kwargs, e=e) + + +__all__ = [ + "SETTING_FIELDS", + "ApplyResult", + "ChangeSet", + "RouterHealth", + "RouterRegistry", + "RouterSettings", + "RoutingContext", + "RoutingObservers", + "deployment_id", +] diff --git a/litellm/router_deprecated.py b/litellm/router_deprecated.py new file mode 100644 index 00000000000..0847c41273f --- /dev/null +++ b/litellm/router_deprecated.py @@ -0,0 +1,852 @@ +""" +DeprecatedRouterMembers: the Router members that outside code used before the entry points. + +``litellm.Router`` mixes this class in so existing callers keep working while they move to the +entry points (``registry()``, ``settings()``, ``health()``, ``select()``, ``apply()``, +``observe()``, ``register_plugin()``, ``retire()``; see litellm/router_api.py). Every member here +returns what it returned before, with the type it had before, and names its replacement in a +``DeprecationWarning``. Reads go to the same Router implementation (``Router._core_``) the +entry point uses; writes go through ``apply`` / ``aapply`` so the registry version moves. + +Access from the router itself and from its own modules (``litellm.router*``) is not counted: +those are the router's internals and move behind the core interface separately. Every other access +is counted in ``DEPRECATED_ROUTER_MEMBER_USES`` so the remaining users show up before this class is +deleted (planned about a month after the core swap). +""" + +# pyright: reportPrivateUsage=false, reportMissingTypeArgument=false, reportUnknownParameterType=false +# pyright: reportUnknownMemberType=false, reportUnknownVariableType=false, reportUnknownArgumentType=false +# pyright: reportUnknownLambdaType=false +# This shim is part of the router (it reads Router's ``_core_*`` members, as Router's own methods do) +# and repeats each old member's signature exactly, untyped parts included (``dict``, ``list``), so +# callers keep the types they had. Typing those parts is the core's job, not the shim's. + +import contextvars +import functools +import inspect +import sys +import warnings +from collections import Counter +from collections.abc import Awaitable, Callable, Iterator, Mapping, Sequence +from types import MappingProxyType +from typing import TYPE_CHECKING, Final, Generic, TypeVar, overload + +from litellm.router_api import SETTING_FIELDS, ChangeSet + +if TYPE_CHECKING: + from typing import Any # noqa: TID251 # only to repeat two old signatures that had Any, unchanged + + from opentelemetry.trace import Span + + from litellm.caching.caching import RedisCache + from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLogging + from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler + from litellm.router import Router + from litellm.router_strategy.budget_limiter import RouterBudgetLimiting + from litellm.router_utils.kubernetes_pod_discovery import KubernetesPodDiscovery + from litellm.router_utils.ptu_shares import PTUTeamCeiling + from litellm.types.router import ( + CustomRoutingStrategyBase, + Deployment, + DeploymentModelListingInfo, + DeploymentTypedDict, + GuardrailTypedDict, + ModelGroupInfo, + PreRoutingHookResponse, + SearchToolTypedDict, + ) + from litellm.types.utils import ModelInfo as ModelMapInfo + +_T: Final = TypeVar("_T") + +DEPRECATED_ROUTER_MEMBER_USES: Final[Counter[str]] = Counter() # mutable-ok: the live tally _note increments + +_INTERNAL_PREFIX: Final = "litellm.router" + + +# Router's own attribute hooks: a write or delete through an old name passes through them first. +_ATTRIBUTE_HOOKS: Final = frozenset({"__setattr__", "__delattr__"}) + + +def _note(name: str, replacement: str) -> None: + depth = 2 # rebind-ok: grows by one per attribute hook frame below + caller = sys._getframe(depth) # rebind-ok: follows depth up past those hooks + while caller.f_code.co_name in _ATTRIBUTE_HOOKS and caller.f_globals.get("__name__") == "litellm.router": + depth += 1 + caller = sys._getframe(depth) + module: Final = caller.f_globals.get("__name__") + if isinstance(module, str) and module.startswith(_INTERNAL_PREFIX): + return + DEPRECATED_ROUTER_MEMBER_USES[name] += 1 + warnings.warn( + f"Router.{name} is deprecated; use Router.{replacement}", + DeprecationWarning, + stacklevel=depth + 1, + ) + + +def _as_router(obj: "DeprecatedRouterMembers") -> "Router": + """The Router this mixin is part of (it is mixed into ``litellm.Router`` only).""" + from litellm.router import Router + + if isinstance(obj, Router): + return obj + raise TypeError(f"{type(obj).__name__} is not a litellm.Router; DeprecatedRouterMembers is mixed into Router only") + + +def _public_attribute_error(obj: object, core_name: str, public_name: str, error: AttributeError) -> AttributeError: + """ + ``error`` renamed to the public attribute when it is the router's own ``core_name`` that is + missing, so the message is the one reading the old attribute raised; any other error unchanged. + """ + if error.name not in (core_name, public_name) or error.obj is not obj: + return error + return AttributeError(f"'{type(obj).__name__}' object has no attribute '{public_name}'", name=public_name, obj=obj) + + +class _DeprecatedAttribute(Generic[_T]): + """A former Router attribute, read through an entry point. Assignment still replaces it.""" + + def __init__(self, replacement: str, read: Callable[["Router"], _T]) -> None: + self.replacement = replacement + self.read = read + self.name = "" + + def __set_name__(self, owner: type, name: str) -> None: + # An alias (``_routing_groups = routing_groups``) keeps the first name. + self.name = self.name or name + + @overload + def __get__(self, obj: None, owner: type) -> object: ... + + @overload + def __get__(self, obj: "DeprecatedRouterMembers", owner: type) -> _T: ... + + def __get__(self, obj: "DeprecatedRouterMembers | None", owner: type) -> object: + if obj is None: + return getattr(owner, "_core_" + self.name, self) + _note(self.name, self.replacement) + router: Final = _as_router(obj) + try: + return self.read(router) + except AttributeError as e: + raise _public_attribute_error(router, "_core_" + self.name, self.name, e) from None + + def __set__(self, obj: "DeprecatedRouterMembers", value: _T) -> None: + _note(self.name, self.replacement) + if self.name in SETTING_FIELDS: + _as_router(obj).apply(ChangeSet(assign={self.name: value})) + else: + setattr(obj, "_core_" + self.name, value) + + def __delete__(self, obj: "DeprecatedRouterMembers") -> None: + _note(self.name, self.replacement) + try: + delattr(obj, "_core_" + self.name) + except AttributeError: + # The only attribute this deletes; Python's own message for it, which sets no name/obj. + raise AttributeError(f"'{type(obj).__name__}' object has no attribute '{self.name}'") from None + + +def _setting(name: str, read: Callable[["Router"], _T]) -> _DeprecatedAttribute[_T]: + """A former settings attribute: ``read`` returns the router's own value, typed as before.""" + + def read_setting(router: "Router") -> _T: + if not router.settings().is_set(name): + raise AttributeError(f"'{type(router).__name__}' object has no attribute {name!r}", name=name, obj=router) + return read(router) + + return _DeprecatedAttribute(f"settings().{name}", read_setting) + + +def _attr(replacement: str, read: Callable[["Router"], _T]) -> _DeprecatedAttribute[_T]: + return _DeprecatedAttribute(replacement, read) + + +# (id(router), member name) pairs whose subclass override is running in this context. +_OVERRIDES_RUNNING: Final[contextvars.ContextVar[frozenset[tuple[int, str]]]] = contextvars.ContextVar( + "router_overrides_running", default=frozenset() +) + + +def _enter_override(name: str, override: Callable[..., object]) -> Callable[..., object]: + """ + A direct call of a subclass override (``router.()``) marks it running, so its + ``super().()`` reaches the parent once instead of re-entering the override through + ``_core_``. + """ + if inspect.iscoroutinefunction(override): + async_override: Final[Callable[..., Awaitable[object]]] = override + + @functools.wraps(override) + async def entered_async( + router: object, + *args: object, + **kwargs: object, # kwargs-ok: passes the override's arguments through unchanged + ) -> object: + token: Final = _OVERRIDES_RUNNING.set(_OVERRIDES_RUNNING.get() | {(id(router), name)}) + try: + return await async_override(router, *args, **kwargs) + finally: + _OVERRIDES_RUNNING.reset(token) + + return entered_async + + @functools.wraps(override) + def entered( + router: object, + *args: object, + **kwargs: object, # kwargs-ok: passes the override's arguments through unchanged + ) -> object: + token: Final = _OVERRIDES_RUNNING.set(_OVERRIDES_RUNNING.get() | {(id(router), name)}) + try: + return override(router, *args, **kwargs) + finally: + _OVERRIDES_RUNNING.reset(token) + + return entered + + +def _private_alias(name: str, member: Callable[..., object]) -> Callable[..., object]: + """ + A private alias of an old member (``_get_all_deployments``). It was bound to Router's own + method, so it never reached a subclass override of ``name``, and still doesn't. + """ + return _enter_override(name, member) + + +def _route_override(cls: type, name: str, override: Callable[..., object]) -> Callable[..., object]: + """ + Make the router's own calls to ``_core_`` reach a subclass override of ````. + + When the override calls ``super().()``, the call comes back to ``_core_``; it is + then sent on to the parent's implementation instead of back into the override. + """ + core: Final = "_core_" + name + + def parent(router: object) -> Callable[..., object]: + return getattr(super(cls, router), core) + + if inspect.iscoroutinefunction(override): + async_override: Final[Callable[..., Awaitable[object]]] = override + + @functools.wraps(override) + async def routed_async( + router: object, + *args: object, + **kwargs: object, # kwargs-ok: passes the override's arguments through unchanged + ) -> object: + key: Final = (id(router), name) + running: Final = _OVERRIDES_RUNNING.get() + if key in running: + parent_async: Final[Callable[..., Awaitable[object]]] = getattr(super(cls, router), core) + return await parent_async(*args, **kwargs) + token: Final = _OVERRIDES_RUNNING.set(running | {key}) + try: + return await async_override(router, *args, **kwargs) + finally: + _OVERRIDES_RUNNING.reset(token) + + return routed_async + + @functools.wraps(override) + def routed( + router: object, + *args: object, + **kwargs: object, # kwargs-ok: passes the override's arguments through unchanged + ) -> object: + key: Final = (id(router), name) + running: Final = _OVERRIDES_RUNNING.get() + if key in running: + return parent(router)(*args, **kwargs) + token: Final = _OVERRIDES_RUNNING.set(running | {key}) + try: + return override(router, *args, **kwargs) + finally: + _OVERRIDES_RUNNING.reset(token) + + return routed + + +class DeprecatedRouterMembers: + """Old Router members, kept for about a month. Each names its replacement.""" + + def __init_subclass__(cls, **kwargs: object) -> None: # kwargs-ok: class keywords, passed on to super() + # A Router subclass that overrides an old member (say async_pre_routing_hook) keeps + # having its override called by the router, which now calls _core_ internally. + super().__init_subclass__(**kwargs) + for name, value in list(vars(cls).items()): + if ( + name.startswith("_core_") + or name not in vars(DeprecatedRouterMembers) + or not hasattr(cls, "_core_" + name) + ): + continue + if isinstance(value, staticmethod): + # Its super() call reaches the old member here, which calls Router's own implementation. + setattr(cls, "_core_" + name, value) + elif inspect.isfunction(value): + setattr(cls, "_core_" + name, _route_override(cls, name, value)) + setattr(cls, name, _enter_override(name, value)) + + # -- registry: attributes -------------------------------------------------------------------- + model_list = _attr("registry().all()", lambda r: r._core_model_list) + model_names = _attr("registry().names(kind='group_set')", lambda r: r._core_model_names) + deployment_names = _attr("registry().names(kind='deployment_names')", lambda r: r._core_deployment_names) + team_public_model_names = _attr("registry().names(kind='team_public')", lambda r: r._core_team_public_model_names) + model_name_to_deployment_indices = _attr( + "registry().index('model_name')", lambda r: r._core_model_name_to_deployment_indices + ) + team_model_to_deployment_indices = _attr( + "registry().index('team_model')", lambda r: r._core_team_model_to_deployment_indices + ) + cached_deployment_model_info = _attr( + "registry().deployment_model_info(id, model_name, cached=True)", + lambda r: r._core_cached_deployment_model_info, + ) + model_group_alias = _attr("registry().aliases()", lambda r: r._core_model_group_alias) + pattern_router = _attr("registry().patterns()", lambda r: r._core_pattern_router) + team_pattern_routers = _attr("registry().patterns(team=True)", lambda r: r._core_team_pattern_routers) + routing_groups = _attr("registry().routing_groups()", lambda r: r._core_routing_groups) + _routing_groups = routing_groups + auto_routers = _attr("registry().plugins('auto')", lambda r: r._core_auto_routers) + complexity_routers = _attr("registry().plugins('complexity')", lambda r: r._core_complexity_routers) + quality_routers = _attr("registry().plugins('quality')", lambda r: r._core_quality_routers) + adaptive_routers = _attr("registry().plugins('adaptive')", lambda r: r._core_adaptive_routers) + routing_plugins = _attr("registry().plugins('routing')", lambda r: r._core_routing_plugins) + prompt_caching_pin_cache = _attr( + "registry().plugins('prompt_caching_pins')", lambda r: r._core_prompt_caching_pin_cache + ) + + # -- health and selection: attributes -------------------------------------------------------- + cache = _attr("health().store", lambda r: r._core_cache) + cooldown_cache = _attr("health().cooldowns", lambda r: r._core_cooldown_cache) + health_state_cache = _attr("health().health_checks", lambda r: r._core_health_state_cache) + deployment_latency_map = _attr("health().latency", lambda r: r._core_deployment_latency_map) + async_get_available_deployment = _attr( + "select(model, request_kwargs)", + lambda r: r._core_async_get_available_deployment, + ) + + # -- observers ------------------------------------------------------------------------------ + slack_alerting_logger = _attr("observe().slack_alerting_logger", lambda r: r._core_slack_alerting_logger) + + # -- settings ------------------------------------------------------------------------------- + routing_strategy = _setting("routing_strategy", lambda r: r._core_routing_strategy) + context_window_fallbacks = _setting("context_window_fallbacks", lambda r: r._core_context_window_fallbacks) + content_policy_fallbacks = _setting("content_policy_fallbacks", lambda r: r._core_content_policy_fallbacks) + default_deployment = _setting("default_deployment", lambda r: r._core_default_deployment) + default_litellm_params = _setting("default_litellm_params", lambda r: r._core_default_litellm_params) + default_max_parallel_requests = _setting( + "default_max_parallel_requests", lambda r: r._core_default_max_parallel_requests + ) + cooldown_time = _setting("cooldown_time", lambda r: r._core_cooldown_time) + allowed_fails = _setting("allowed_fails", lambda r: r._core_allowed_fails) + allowed_fails_policy = _setting("allowed_fails_policy", lambda r: r._core_allowed_fails_policy) + disable_cooldowns = _setting("disable_cooldowns", lambda r: r._core_disable_cooldowns) + enable_pre_call_checks = _setting("enable_pre_call_checks", lambda r: r._core_enable_pre_call_checks) + enable_health_check_routing = _setting("enable_health_check_routing", lambda r: r._core_enable_health_check_routing) + health_check_ignore_transient_errors = _setting( + "health_check_ignore_transient_errors", lambda r: r._core_health_check_ignore_transient_errors + ) + background_health_check_model_groups = _setting( + "background_health_check_model_groups", lambda r: r._core_background_health_check_model_groups + ) + enable_tag_filtering = _setting("enable_tag_filtering", lambda r: r._core_enable_tag_filtering) + tag_filtering_match_any = _setting("tag_filtering_match_any", lambda r: r._core_tag_filtering_match_any) + tag_routing_prefix = _setting("tag_routing_prefix", lambda r: r._core_tag_routing_prefix) + router_general_settings = _setting("router_general_settings", lambda r: r._core_router_general_settings) + provider_budget_config = _setting("provider_budget_config", lambda r: r._core_provider_budget_config) + provider_default_deployment_ids = _setting( + "provider_default_deployment_ids", lambda r: r._core_provider_default_deployment_ids + ) + fallback_access_check = _setting("fallback_access_check", lambda r: r._core_fallback_access_check) + fallback_budget_check = _setting("fallback_budget_check", lambda r: r._core_fallback_budget_check) + # These three are declared with the attribute type Router gave them rather than the descriptor's: + # protocols in router_utils and the proxy match Router on them, and protocol matching does not + # see through a descriptor. Reads and writes still go through _setting at runtime. + fallbacks: list | list[dict[str, list[str] | list | None]] | None = _setting( # pyright: ignore[reportAssignmentType] # see above # mutable-ok: the old type + "fallbacks", lambda r: r._core_fallbacks + ) + kubernetes_pod_discovery: "KubernetesPodDiscovery" = _setting( # pyright: ignore[reportAssignmentType] # see above + "kubernetes_pod_discovery", lambda r: r._core_kubernetes_pod_discovery + ) + search_tools: "list[SearchToolTypedDict]" = _setting( # pyright: ignore[reportAssignmentType] # see above # mutable-ok: the old type + "search_tools", lambda r: r._core_search_tools + ) + guardrail_list = _setting("guardrail_list", lambda r: r._core_guardrail_list) + cache_responses = _setting("cache_responses", lambda r: r._core_cache_responses) + + # -- registry: one deployment --------------------------------------------------------------- + def get_deployment(self, model_id: str) -> "Deployment | None": + _note("get_deployment", "registry().deployment(id)") + return _as_router(self)._core_get_deployment(model_id=model_id) + + def has_model_id(self, candidate_id: str) -> bool: + _note("has_model_id", "registry().has_deployment(id)") + return _as_router(self)._core_has_model_id(candidate_id) + + def get_model_info(self, id: str) -> dict | None: # mutable-ok: the old return type, unchanged + _note("get_model_info", "registry().deployment_dict(id)") + return _as_router(self)._core_get_model_info(id=id) + + def get_deployment_model_info(self, model_id: str, model_name: str) -> "ModelMapInfo | None": + _note("get_deployment_model_info", "registry().deployment_model_info(id, model_name)") + return _as_router(self)._core_get_deployment_model_info(model_id=model_id, model_name=model_name) + + @overload + def get_router_model_info( + self, + deployment: "dict | Deployment", # mutable-ok: the old parameter type, unchanged + received_model_name: str, + id: None = None, + ) -> "ModelMapInfo": ... + + @overload + def get_router_model_info(self, deployment: None, received_model_name: str, id: str) -> "ModelMapInfo": ... + + def get_router_model_info( + self, + deployment: "dict | Deployment | None", # mutable-ok: the old parameter type, unchanged + received_model_name: str, + id: str | None = None, + ) -> "ModelMapInfo": + _note("get_router_model_info", "registry().router_model_info(...)") + router: Final = _as_router(self) + # The core's overloads split (deployment, id) by which one is None; this forwards either form unchanged. + return router._core_get_router_model_info( # pyright: ignore[reportCallIssue] # see above + deployment=deployment, # pyright: ignore[reportArgumentType] # see above + received_model_name=received_model_name, + id=id, # pyright: ignore[reportArgumentType] # see above + ) + + def get_discovered_model_info(self, model_id: str | None) -> Mapping[str, int]: + _note("get_discovered_model_info", "registry().discovered_model_info(id)") + return _as_router(self)._core_get_discovered_model_info(model_id) + + # -- registry: many deployments ------------------------------------------------------------- + def get_model_list( + self, model_name: str | None = None, team_id: str | None = None + ) -> "list[DeploymentTypedDict] | None": # mutable-ok: the old return type, unchanged + _note("get_model_list", "registry().deployments(model_name, team_id=...)") + return _as_router(self)._core_get_model_list(model_name=model_name, team_id=team_id) + + def get_all_deployments( + self, model_name: str, model_alias: str | None = None, team_id: str | None = None + ) -> "list[DeploymentTypedDict]": # mutable-ok: the old return type, unchanged + _note("get_all_deployments", "registry().deployments(model_name, source='all')") + return _as_router(self)._core_get_all_deployments( + model_name=model_name, model_alias=model_alias, team_id=team_id + ) + + _get_all_deployments = _private_alias("get_all_deployments", get_all_deployments) + + def config_deployments(self) -> Iterator[Mapping[str, object]]: + _note("config_deployments", "registry().deployments(source='config')") + return _as_router(self)._core_config_deployments() + + def deployments_named(self, model_name: str) -> "tuple[DeploymentTypedDict, ...] | None": + _note("deployments_named", "registry().deployments(model_name, source='named')") + return _as_router(self)._core_deployments_named(model_name) + + def get_model_list_from_model_alias( + self, model_name: str | None = None + ) -> "list[DeploymentTypedDict]": # mutable-ok: the old return type, unchanged + _note("get_model_list_from_model_alias", "registry().deployments(model_name, source='alias')") + return _as_router(self)._core_get_model_list_from_model_alias(model_name=model_name) + + def get_model_list_from_routing_groups(self, model_name: str | None = None) -> "Sequence[DeploymentTypedDict]": + _note("get_model_list_from_routing_groups", "registry().deployments(model_name, source='routing_groups')") + return _as_router(self)._core_get_model_list_from_routing_groups(model_name=model_name) + + def get_model_list_of_routed_group( + self, model_group: str, team_id: str | None = None + ) -> "list[DeploymentTypedDict]": # mutable-ok: the old return type, unchanged + _note("get_model_list_of_routed_group", "registry().deployments(model_group, source='routed_group')") + return _as_router(self)._core_get_model_list_of_routed_group(model_group, team_id) + + def wildcard_route_deployments(self, model: str, team_id: str | None) -> Sequence[Mapping[str, object]]: + _note("wildcard_route_deployments", "registry().deployments(model, source='wildcard')") + return _as_router(self)._core_wildcard_route_deployments(model, team_id) + + def get_routable_deployments( + self, + model: str, + request_kwargs: Mapping[str, object] = MappingProxyType({}), + call_type: str | None = None, + ) -> "tuple[DeploymentTypedDict, ...]": + _note( + "get_routable_deployments", "registry().deployments(model, source='routable', request=..., call_type=...)" + ) + return _as_router(self)._core_get_routable_deployments(model, request_kwargs, call_type) + + def get_deployment_by_model_group_name(self, model_group_name: str) -> "Deployment | None": + _note("get_deployment_by_model_group_name", "registry().deployments(name, source='first_in_group')") + return _as_router(self)._core_get_deployment_by_model_group_name(model_group_name=model_group_name) + + def deployments_for_request( + self, model: str, request_kwargs: Mapping[str, object] + ) -> "Sequence[DeploymentTypedDict]": + _note("deployments_for_request", "registry().deployments(model, source='for_request', request=...)") + return _as_router(self)._core_deployments_for_request(model, request_kwargs) + + def should_include_deployment( + self, + model_name: str, + model: "DeploymentTypedDict | dict", # mutable-ok: the old parameter type, unchanged + team_id: str | None = None, + ) -> bool: + _note("should_include_deployment", "registry().includes(deployment, model_name=..., team_id=...)") + return _as_router(self)._core_should_include_deployment(model_name=model_name, model=model, team_id=team_id) + + @staticmethod + def deployment_usable_by_team(model: "Mapping[str, object] | Deployment", team_id: str | None) -> bool: + _note("deployment_usable_by_team", "registry().includes(deployment, team_id=...)") + from litellm.router import Router + + return Router._core_deployment_usable_by_team(model, team_id) + + _deployment_usable_by_team = deployment_usable_by_team + + # -- registry: names ------------------------------------------------------------------------- + def get_model_names(self, team_id: str | None = None) -> list[str]: # mutable-ok: the old return type, unchanged + _note("get_model_names", "registry().names(team_id=...)") + return _as_router(self)._core_get_model_names(team_id=team_id) + + def get_model_ids( + self, model_name: str | None = None, exclude_team_models: bool = False + ) -> list[str]: # mutable-ok: the old return type, unchanged + _note("get_model_ids", "registry().names(model_name, kind='ids')") + return _as_router(self)._core_get_model_ids(model_name=model_name, exclude_team_models=exclude_team_models) + + def resolved_litellm_models( + self, model_name: str, team_id: str | None = None, *, use_base_model: bool = False + ) -> tuple[str, ...]: + _note("resolved_litellm_models", "registry().names(model_name, kind='litellm_models' or 'litellm_base_models')") + return _as_router(self)._core_resolved_litellm_models( + model_name, team_id=team_id, use_base_model=use_base_model + ) + + def wildcard_routed_models(self, model: str, team_id: str | None) -> frozenset[str]: + _note("wildcard_routed_models", "registry().names(model, kind='wildcard')") + return _as_router(self)._core_wildcard_routed_models(model, team_id) + + def get_candidate_model_ids_for_route(self, model: str, team_id: str | None = None) -> frozenset[str]: + _note("get_candidate_model_ids_for_route", "registry().names(model, kind='route_candidates')") + return _as_router(self)._core_get_candidate_model_ids_for_route(model, team_id=team_id) + + # -- registry: resolving names --------------------------------------------------------------- + def routable_model_group(self, model_name: str) -> str: + _note("routable_model_group", "registry().resolve(name)") + return _as_router(self)._core_routable_model_group(model_name) + + def get_model_from_alias(self, model: str) -> str | None: + _note("get_model_from_alias", "registry().resolve(name, kind='alias')") + return _as_router(self)._core_get_model_from_alias(model=model) + + _get_model_from_alias = _private_alias("get_model_from_alias", get_model_from_alias) + + def resolve_served_model_name(self, model_name: str, served_model: str) -> str: + _note("resolve_served_model_name", "registry().resolve(name, kind='served', served_model=...)") + return _as_router(self)._core_resolve_served_model_name(model_name, served_model) + + def resolve_model_name_from_model_id( + self, model_id: str | None, custom_llm_provider: str | None = None + ) -> str | None: + _note("resolve_model_name_from_model_id", "registry().resolve(id, kind='from_id')") + return _as_router(self)._core_resolve_model_name_from_model_id( + model_id, custom_llm_provider=custom_llm_provider + ) + + def map_team_model( + self, team_model_name: str | None, team_id: str, custom_llm_provider: str | None = None + ) -> str | None: + _note("map_team_model", "registry().resolve(name, kind='team_model', team_id=...)") + return _as_router(self)._core_map_team_model(team_model_name, team_id, custom_llm_provider=custom_llm_provider) + + def is_recognized_model(self, model: str) -> bool: + _note("is_recognized_model", "registry().resolve(name, kind='recognized')") + return _as_router(self)._core_is_recognized_model(model) + + def get_routable_upstream_model(self, model_name: str, team_id: str | None = None) -> str | None: + _note("get_routable_upstream_model", "registry().resolve(name, kind='upstream')") + return _as_router(self)._core_get_routable_upstream_model(model_name, team_id=team_id) + + def get_search_tool_support(self, model_name: str, team_id: str | None = None) -> tuple[bool | None, ...]: + _note("get_search_tool_support", "registry().resolve(name, kind='search_tool_support')") + return _as_router(self)._core_get_search_tool_support(model_name, team_id=team_id) + + def get_model_group(self, id: str) -> list | None: # mutable-ok: the old return type, unchanged + _note("get_model_group", "registry().resolve(id, kind='group_of_id')") + return _as_router(self)._core_get_model_group(id=id) + + def is_model_access_group_for_wildcard_route(self, model_access_group: str) -> bool: + _note("is_model_access_group_for_wildcard_route", "registry().resolve(group, kind='wildcard_access_group')") + return _as_router(self)._core_is_model_access_group_for_wildcard_route(model_access_group=model_access_group) + + _is_model_access_group_for_wildcard_route = _private_alias( + "is_model_access_group_for_wildcard_route", is_model_access_group_for_wildcard_route + ) + + def team_model_has_alternatives(self, deployment_id: str) -> bool: + _note("team_model_has_alternatives", "registry().resolve(id, kind='team_model_alternatives')") + return _as_router(self)._core_team_model_has_alternatives(deployment_id) + + def routing_group_has_alternatives(self, model_group: str | None) -> bool: + _note("routing_group_has_alternatives", "registry().resolve(group, kind='routing_group_alternatives')") + return _as_router(self)._core_routing_group_has_alternatives(model_group) + + # -- registry: groups, access groups, credentials, plugins ----------------------------------- + def get_model_group_info( + self, model_group: str, *, include_hidden: bool = False, team_id: str | None = None + ) -> "ModelGroupInfo | None": + _note("get_model_group_info", "registry().group(name)") + return _as_router(self)._core_get_model_group_info(model_group, include_hidden=include_hidden, team_id=team_id) + + def cached_model_group_info(self, model_group: str) -> "ModelGroupInfo | None": + _note("cached_model_group_info", "registry().group(name, field='cached_info')") + return _as_router(self)._core_cached_model_group_info(model_group) + + def get_configured_display_name(self, model_name: str) -> str | None: + _note("get_configured_display_name", "registry().group(name, field='display_name')") + return _as_router(self)._core_get_configured_display_name(model_name) + + def get_configured_mode(self, model_name: str) -> str | None: + _note("get_configured_mode", "registry().group(name, field='mode')") + return _as_router(self)._core_get_configured_mode(model_name) + + def get_configured_service_tiers(self, model_name: str, team_id: str | None = None) -> tuple[object, ...]: + _note("get_configured_service_tiers", "registry().group(name, field='service_tiers')") + return _as_router(self)._core_get_configured_service_tiers(model_name, team_id=team_id) + + def get_configured_token_limits(self, model_name: str) -> tuple[int | None, int | None]: + _note("get_configured_token_limits", "registry().group(name, field='token_limits')") + return _as_router(self)._core_get_configured_token_limits(model_name) + + def get_model_listing_info(self, model_name: str) -> "DeploymentModelListingInfo | None": + _note("get_model_listing_info", "registry().group(name, field='listing_info')") + return _as_router(self)._core_get_model_listing_info(model_name) + + def get_model_access_groups( + self, + model_name: str | None = None, + model_access_group: str | None = None, + team_id: str | None = None, + ) -> dict[str, list[str]]: # mutable-ok: the old return type, unchanged + _note("get_model_access_groups", "registry().access_groups(...)") + return _as_router(self)._core_get_model_access_groups( + model_name=model_name, model_access_group=model_access_group, team_id=team_id + ) + + def get_deployment_credentials(self, model_id: str) -> dict | None: # mutable-ok: the old return type, unchanged + _note("get_deployment_credentials", "registry().credentials(id)") + return _as_router(self)._core_get_deployment_credentials(model_id=model_id) + + def get_deployment_credentials_with_provider( + self, model_id: str, team_id: str | None = None + ) -> "dict[str, Any] | None": # pyright: ignore[reportExplicitAny] # the old return type, unchanged # mutable-ok: same + _note("get_deployment_credentials_with_provider", "registry().credentials(id, form='with_provider')") + return _as_router(self)._core_get_deployment_credentials_with_provider(model_id=model_id, team_id=team_id) + + def get_credential_deployment(self, model_id: str, team_id: str | None = None) -> "Deployment | None": + _note("get_credential_deployment", "registry().credentials(id, form='deployment')") + return _as_router(self)._core_get_credential_deployment(model_id=model_id, team_id=team_id) + + def get_router_deployment_budget_limiter(self) -> "RouterBudgetLimiting | None": + _note("get_router_deployment_budget_limiter", "registry().plugins('budget_limiter')") + return _as_router(self)._core_get_router_deployment_budget_limiter() + + _get_router_deployment_budget_limiter = _private_alias( + "get_router_deployment_budget_limiter", get_router_deployment_budget_limiter + ) + + @staticmethod + def generate_model_id(model_group: str, litellm_params: dict) -> str: # mutable-ok: the old parameter type + _note("generate_model_id", "litellm.router_api.deployment_id(model_group, litellm_params)") + from litellm.router_api import deployment_id + + return deployment_id(model_group, litellm_params) + + # -- settings: methods ----------------------------------------------------------------------- + def get_settings(self) -> dict[str, object]: # mutable-ok: Router._core_get_settings's type, a fresh dict + _note("get_settings", "settings().as_dict()") + return _as_router(self)._core_get_settings() + + def get_allowed_fails_from_policy(self, exception: Exception) -> int | None: + _note("get_allowed_fails_from_policy", "settings().allowed_fails_for(exception)") + return _as_router(self)._core_get_allowed_fails_from_policy(exception=exception) + + def get_available_guardrail(self, guardrail_name: str) -> "GuardrailTypedDict": + _note("get_available_guardrail", "settings().guardrail(name)") + return _as_router(self)._core_get_available_guardrail(guardrail_name) + + # -- health ---------------------------------------------------------------------------------- + @staticmethod + def is_deployment_blocked(deployment: "Deployment") -> bool: + _note("is_deployment_blocked", "health().is_blocked(deployment)") + from litellm.router_api import RouterHealth + + return RouterHealth.is_blocked(deployment) + + _is_deployment_blocked = is_deployment_blocked + + @staticmethod + def are_all_deployments_blocked(deployments: "Sequence[DeploymentTypedDict]") -> bool: + _note("are_all_deployments_blocked", "health().all_blocked(deployments)") + from litellm.router_api import RouterHealth + + return RouterHealth.all_blocked(deployments) + + _are_all_deployments_blocked = are_all_deployments_blocked + + def get_fully_blocked_model_names(self) -> set[str]: # mutable-ok: the old return type, unchanged + _note("get_fully_blocked_model_names", "health().fully_blocked_model_names()") + return _as_router(self)._core_get_fully_blocked_model_names() + + async def async_get_fully_unhealthy_model_names(self) -> set[str]: # mutable-ok: the old return type, unchanged + _note("async_get_fully_unhealthy_model_names", "health().fully_unhealthy_model_names()") + return await _as_router(self)._core_async_get_fully_unhealthy_model_names() + + async def get_model_group_usage(self, model_group: str) -> tuple[int | None, int | None]: + _note("get_model_group_usage", "health().usage(group)") + return await _as_router(self)._core_get_model_group_usage(model_group=model_group) + + async def get_remaining_model_group_usage( + self, model_group: str + ) -> dict[str, int]: # mutable-ok: the old return type, unchanged + _note("get_remaining_model_group_usage", "health().usage(group, kind='remaining')") + return await _as_router(self)._core_get_remaining_model_group_usage(model_group=model_group) + + def team_ptu_ceiling(self, team_id: str, model_group: str) -> "PTUTeamCeiling | None": + _note("team_ptu_ceiling", "health().team_ptu_ceiling(team_id, group)") + return _as_router(self)._core_team_ptu_ceiling(team_id, model_group) + + def split_request_priority( + self, model: str, kwargs: Mapping[str, object] + ) -> tuple[int | None, Mapping[str, object]]: + _note("split_request_priority", "health().split_priority(model, kwargs)") + return _as_router(self)._core_split_request_priority(model, kwargs) + + # -- selection ------------------------------------------------------------------------------- + async def async_get_healthy_deployments( + self, + model: str, + request_kwargs: dict, # mutable-ok: the old parameter type, unchanged + messages: list[dict[str, str]] | None = None, # mutable-ok: the old parameter type, unchanged + input: str | list | None = None, # mutable-ok: the old parameter type, unchanged + specific_deployment: bool | None = False, + parent_otel_span: "Span | None" = None, + health_check_probe: bool = False, + ) -> list[dict] | dict: # mutable-ok: the old return type, unchanged + _note("async_get_healthy_deployments", "select(model, request_kwargs, kind='healthy')") + return await _as_router(self)._core_async_get_healthy_deployments( + model=model, + request_kwargs=request_kwargs, + messages=messages, + input=input, + specific_deployment=specific_deployment, + parent_otel_span=parent_otel_span, + health_check_probe=health_check_probe, + ) + + def get_available_deployment_for_pass_through( # noqa: ANN201 # the old member had no return annotation; its inferred type is kept by inferring it from the core + self, + model: str, + messages: list[dict[str, str]] | None = None, # mutable-ok: the old parameter type, unchanged + input: str | list | None = None, # mutable-ok: the old parameter type, unchanged + specific_deployment: bool | None = False, + request_kwargs: dict | None = None, # mutable-ok: the old parameter type, unchanged + ): + _note("get_available_deployment_for_pass_through", "select(model, kind='pass_through')") + return _as_router(self)._core_get_available_deployment_for_pass_through( + model=model, + messages=messages, + input=input, + specific_deployment=specific_deployment, + request_kwargs=request_kwargs, + ) + + # -- writes ---------------------------------------------------------------------------------- + def upsert_deployment(self, deployment: "Deployment") -> "Deployment | None": + _note("upsert_deployment", "apply(ChangeSet(upsert=[deployment]))") + return _as_router(self).apply(ChangeSet(upsert=[deployment])).upserted[0] + + def delete_deployment(self, id: str) -> "Deployment | None": + _note("delete_deployment", "apply(ChangeSet(delete=[id]))") + return _as_router(self).apply(ChangeSet(delete=[id])).deleted[0] + + def update_settings(self, **kwargs: object) -> None: # kwargs-ok: the old signature, unchanged + _note("update_settings", "apply(ChangeSet(settings={...}))") + _as_router(self).apply(ChangeSet(settings=kwargs)) + + def update_redis_cache(self, cache: "RedisCache") -> None: + _note("update_redis_cache", "apply(ChangeSet(redis_cache=cache))") + _as_router(self).apply(ChangeSet(redis_cache=cache)) + + _update_redis_cache = _private_alias("update_redis_cache", update_redis_cache) + + async def arefresh_model_info(self, *, client: "AsyncHTTPHandler | None" = None) -> None: + _note("arefresh_model_info", "aapply(ChangeSet(refresh_model_info=True))") + await _as_router(self).aapply(ChangeSet(refresh_model_info=True, refresh_client=client)) + + # -- plugins, observers, lifecycle ----------------------------------------------------------- + def set_custom_routing_strategy(self, CustomRoutingStrategy: "CustomRoutingStrategyBase") -> None: + _note("set_custom_routing_strategy", "register_plugin(strategy)") + _as_router(self).register_plugin(CustomRoutingStrategy) + + def serve_through_request_logging(self) -> None: + _note("serve_through_request_logging", "observe(request_scoped=True)") + _as_router(self).observe(request_scoped=True) + + def bind_to_request_logging(self, logging_obj: "LiteLLMLogging") -> None: + _note("bind_to_request_logging", "observe(logging_obj)") + # Straight to the core, as before: observe() skips binding when logging_obj is None, the old member did not. + _as_router(self)._core_bind_to_request_logging(logging_obj) + + def discard(self) -> None: + _note("discard", "retire()") + _as_router(self).retire() + + # -- plugin context (router strategies) ------------------------------------------------------ + def arm_routing_read_prefetch( + self, + model: str, + request_kwargs: dict[str, object] | None = None, # mutable-ok: the old parameter type, unchanged + ) -> None: + _note("arm_routing_read_prefetch", "RoutingContext.arm_read_prefetch") + _as_router(self)._core_arm_routing_read_prefetch(model, request_kwargs) + + async def async_pre_routing_hook( + self, + model: str, + request_kwargs: dict[str, object], # mutable-ok: the old parameter type, unchanged + messages: "list[dict[str, Any]] | None" = None, # pyright: ignore[reportExplicitAny] # the old type # mutable-ok: same + input: str | list | None = None, # mutable-ok: the old parameter type, unchanged + specific_deployment: bool | None = False, + ) -> "PreRoutingHookResponse | None": + _note("async_pre_routing_hook", "RoutingContext.pre_routing_hook") + return await _as_router(self)._core_async_pre_routing_hook( + model=model, + request_kwargs=request_kwargs, + messages=messages, + input=input, + specific_deployment=specific_deployment, + ) + + def log_retry(self, kwargs: dict, e: Exception) -> dict: # mutable-ok: the old signature, unchanged + _note("log_retry", "RoutingContext.log_retry") + return _as_router(self)._core_log_retry(kwargs=kwargs, e=e) + + +def _name_members_after_router() -> None: + # The old members were Router methods, so errors that name the method (a wrong-arity TypeError, + # or "litellm.router.Router.() argument after ** must be a mapping") keep naming it the + # way they did before the members moved here. + for member in vars(DeprecatedRouterMembers).values(): + function = member.__func__ if isinstance(member, staticmethod) else member + if inspect.isfunction(function): + function.__qualname__ = f"Router.{function.__name__}" + function.__module__ = "litellm.router" + + +_name_members_after_router() diff --git a/litellm/router_utils/endpoint_eligibility.py b/litellm/router_utils/endpoint_eligibility.py new file mode 100644 index 00000000000..4a87e582c97 --- /dev/null +++ b/litellm/router_utils/endpoint_eligibility.py @@ -0,0 +1,120 @@ +from collections.abc import Generator, Mapping, Sequence +from contextlib import contextmanager +from contextvars import ContextVar +from types import MappingProxyType +from typing import Final, TypeVar + +from pydantic import BaseModel, ConfigDict, TypeAdapter, ValidationError, field_validator + +from litellm._logging import verbose_router_logger +from litellm.exceptions import EndpointNotSupportedByModelError + +CHAT_ENDPOINT_BY_CALL: Final[Mapping[str, str]] = MappingProxyType( + { + "completion": "/v1/chat/completions", + "acompletion": "/v1/chat/completions", + "_completion": "/v1/chat/completions", + "_acompletion": "/v1/chat/completions", + "text_completion": "/v1/completions", + "atext_completion": "/v1/completions", + "_atext_completion": "/v1/completions", + "anthropic_messages": "/v1/messages", + "aresponses": "/v1/responses", + "agenerate_content": "generateContent", + "agenerate_content_stream": "streamGenerateContent", + "acreate_interaction": "/v1beta/interactions", + "litellm.interactions.main.acreate": "/v1beta/interactions", + "litellm.interactions.main.create": "/v1beta/interactions", + "_aresponses_websocket": "/v1/responses", + "acompact_responses": "/v1/responses/compact", + } +) + + +def _endpoint_path(endpoint: str) -> str: + rooted: Final = "/" + endpoint.strip().strip("/").casefold() + path: Final = rooted.removeprefix("/openai").removeprefix("/v1beta").removeprefix("/v1") + return "/" + path.rsplit(":", 1)[-1] if ":" in path else path + + +CHAT_ENDPOINTS: Final = frozenset(_endpoint_path(endpoint) for endpoint in CHAT_ENDPOINT_BY_CALL.values()) +_Deployment: Final = TypeVar("_Deployment", bound=Mapping[str, object]) +_ENFORCE_FLAG: Final = TypeAdapter(bool | None) +_REQUEST_SELECTION: Final[ContextVar[tuple[str | None, str | None]]] = ContextVar( + "router_request_endpoint", default=(None, None) +) + + +class _EndpointDeclaration(BaseModel): + model_config = ConfigDict(frozen=True) + + enforce_supported_endpoints: bool | None = None + supported_endpoints: tuple[str, ...] | None = None + + @field_validator("supported_endpoints", mode="before") + @classmethod + def _one_endpoint_as_a_list(cls, value: object) -> object: + return (value,) if isinstance(value, str) else value + + +@contextmanager +def endpoint_selection(call_type: object, module: object = None, model: object = None) -> Generator[None]: + qualified_call: Final = f"{module}.{call_type}" if module == "litellm.interactions.main" else call_type + endpoint: Final = CHAT_ENDPOINT_BY_CALL.get(qualified_call) if isinstance(qualified_call, str) else None + token: Final = _REQUEST_SELECTION.set((endpoint, model if isinstance(model, str) else None)) + try: + yield + finally: + _REQUEST_SELECTION.reset(token) + + +def _enforced_endpoints(model_info: object) -> tuple[str, ...] | None: + try: + declaration: Final = _EndpointDeclaration.model_validate(model_info) + except ValidationError: + return None + return declaration.supported_endpoints if declaration.enforce_supported_endpoints else None + + +def warn_on_unenforced_endpoint_flag(model_name: str, model_info: Mapping[str, object]) -> str | None: + try: + flagged: Final = _ENFORCE_FLAG.validate_python(model_info.get("enforce_supported_endpoints")) + except ValidationError: + return None + if not flagged or _enforced_endpoints(model_info) is not None: + return None + warning: Final = ( + f"Deployment '{model_name}' sets enforce_supported_endpoints without a supported_endpoints list of " + 'strings, so litellm enforces nothing for it. Set supported_endpoints, e.g. ["/v1/embeddings"].' + ) + verbose_router_logger.warning(warning) + return warning + + +def non_chat_endpoints(deployment: Mapping[str, object]) -> frozenset[str] | None: + declared: Final = _enforced_endpoints(deployment.get("model_info")) + if declared is None: + return None + endpoints: Final = frozenset(declared) + normalized: Final = frozenset(_endpoint_path(endpoint) for endpoint in endpoints) + return endpoints if normalized.isdisjoint(CHAT_ENDPOINTS) else None + + +def eligible_deployments( + model: str, deployments: Sequence[_Deployment] +) -> tuple[_Deployment, ...] | EndpointNotSupportedByModelError: + endpoint, requested_model = _REQUEST_SELECTION.get() + if endpoint is None or not deployments: + return tuple(deployments) + eligible: Final = tuple(deployment for deployment in deployments if non_chat_endpoints(deployment) is None) + if eligible: + return eligible + requested: Final = requested_model or model + supported: Final = ", ".join( + sorted(frozenset[str]().union(*(non_chat_endpoints(deployment) or frozenset() for deployment in deployments))) + ) + return EndpointNotSupportedByModelError( + message=f"{endpoint} is not supported by model={requested}. Supported endpoints: {supported or 'none'}", + model=model, + llm_provider="", + ) diff --git a/litellm/router_utils/reasoning_effort_capability.py b/litellm/router_utils/reasoning_effort_capability.py index 2bd4d82b1ff..d2fe9134a22 100644 --- a/litellm/router_utils/reasoning_effort_capability.py +++ b/litellm/router_utils/reasoning_effort_capability.py @@ -399,6 +399,27 @@ def _log_remap( logged_deployment_id: Final = _single_line(deployment_id) logged_model_group: Final = _single_line(model_group) logged_deployment_model: Final = _single_line(deployment_model) + extra: Final = MappingProxyType( + { + "reasoning_effort_deployment_id": logged_deployment_id, + "reasoning_effort_model_group": logged_model_group, + "reasoning_effort_deployment_model": logged_deployment_model, + "reasoning_effort_requested": logged_requested_level, + "reasoning_effort_sent": logged_sent_level, + "reasoning_effort_rounding": rounding, + } + ) + if sent_level is None and requested_level != _DEFAULT_EFFORT: + verbose_logger.warning( + "Dropping reasoning level %s (drop_params=True) for deployment %s, model group %s, model %s because its " + "model_info.reasoning_effort_levels does not list that level. List every level the deployment supports.", + logged_requested_level, + logged_deployment_id, + logged_model_group, + logged_deployment_model, + extra=extra, + ) + return verbose_logger.info( "reasoning_effort %s sent as %s (rounding %s) to deployment %s, model group %s, model %s", logged_requested_level, @@ -407,14 +428,7 @@ def _log_remap( logged_deployment_id, logged_model_group, logged_deployment_model, - extra={ - "reasoning_effort_deployment_id": logged_deployment_id, - "reasoning_effort_model_group": logged_model_group, - "reasoning_effort_deployment_model": logged_deployment_model, - "reasoning_effort_requested": logged_requested_level, - "reasoning_effort_sent": logged_sent_level, - "reasoning_effort_rounding": rounding, - }, + extra=extra, ) diff --git a/litellm/rust_bridge/_native.pyi b/litellm/rust_bridge/_native.pyi index 7364a1d72f2..62b48621c05 100644 --- a/litellm/rust_bridge/_native.pyi +++ b/litellm/rust_bridge/_native.pyi @@ -14,6 +14,7 @@ class ForkedAfterNativeRuntimeStarted(RuntimeError): ... class ProcessReservedForForking(RuntimeError): ... def trace_encode_error(message: str) -> bytes: ... +def bundled_model_cost_map() -> bytes: ... @final class NativeClickHouseSpendConfig: @@ -220,6 +221,7 @@ __all__ = [ "aocr", "aresponses", "atranscription", + "bundled_model_cost_map", "completion", "embedding", "gil_stats", diff --git a/litellm/secret_managers/main.py b/litellm/secret_managers/main.py index 588bdb1409c..02cf53d00ec 100644 --- a/litellm/secret_managers/main.py +++ b/litellm/secret_managers/main.py @@ -5,7 +5,7 @@ import time import traceback from collections.abc import Callable from dataclasses import dataclass -from typing import Final +from typing import Final, TypeAlias import httpx from pydantic import TypeAdapter, ValidationError @@ -13,6 +13,7 @@ from pydantic import TypeAdapter, ValidationError import litellm from litellm._logging import verbose_logger from litellm.caching.caching import DualCache +from litellm.litellm_core_utils.env_utils import get_env_float from litellm.litellm_core_utils.single_flight import single_flight_lru_cache from litellm.llms.custom_httpx.http_handler import HTTPHandler from litellm.secret_managers.get_azure_ad_token_provider import ( @@ -167,12 +168,37 @@ def get_secret_str( LOCAL_OIDC_SECRET_PREFIXES: Final = ("oidc/file/", "oidc/env/") +GOOGLE_OIDC_SECRET_PREFIX: Final = "oidc/google/" +API_KEY_OIDC_SECRET_PREFIXES: Final = (*LOCAL_OIDC_SECRET_PREFIXES, GOOGLE_OIDC_SECRET_PREFIX) +_GOOGLE_METADATA_DEFAULT_HOST: Final = "metadata.google.internal" +_GOOGLE_METADATA_DEFAULT_TIMEOUT_SECONDS: Final = 3.0 +_GOOGLE_TOKEN_UNAVAILABLE: Final = "the metadata server did not return an ID token" + + +def _google_metadata_identity_url() -> str: + host: Final = os.getenv("GCE_METADATA_HOST") or _GOOGLE_METADATA_DEFAULT_HOST + return f"http://{host}/computeMetadata/v1/instance/service-accounts/default/identity" + + +def _google_metadata_timeout() -> float: + seconds: Final = get_env_float("GCE_METADATA_TIMEOUT", _GOOGLE_METADATA_DEFAULT_TIMEOUT_SECONDS) + return seconds if seconds > 0 else _GOOGLE_METADATA_DEFAULT_TIMEOUT_SECONDS + + +class GoogleMetadataStatusError(ValueError): + def __init__(self, status_code: int) -> None: + super().__init__(f"Google OIDC provider failed with HTTP {status_code}") + self.status_code: Final = status_code @dataclass(frozen=True, slots=True) class UnresolvableSecretReference: kind: str reason: str + retryable: bool = False + + +OidcResolver: TypeAlias = Callable[[str], "str | UnresolvableSecretReference"] _OIDC_PROVIDERS: Final = frozenset({"google", "circleci", "circleci_v2", "github", "azure", "file", "env", "env_path"}) @@ -198,11 +224,86 @@ def resolve_local_oidc_reference(value: str) -> str | UnresolvableSecretReferenc return secret -def _logged_unresolvable(reference: str, kind: str, reason: str) -> UnresolvableSecretReference: +def resolve_api_key_oidc_reference(value: str) -> str | UnresolvableSecretReference: + if not value.startswith(GOOGLE_OIDC_SECRET_PREFIX): + return resolve_local_oidc_reference(value) + try: + token: Final = _google_id_token( + value, value.removeprefix(GOOGLE_OIDC_SECRET_PREFIX), timeout=_google_metadata_timeout() + ) + except (httpx.HTTPError, httpx.InvalidURL, ValueError) as exc: + return _logged_unresolvable( + value, + "oidc/google", + _GOOGLE_TOKEN_UNAVAILABLE, + f"{type(exc).__name__}: {exc}", + retryable=_metadata_server_failed_to_answer(exc), + ) + if not token: + return _logged_unresolvable(value, "oidc/google", "it resolved to an empty value") + return token + + +def _metadata_server_failed_to_answer(exc: httpx.HTTPError | httpx.InvalidURL | ValueError) -> bool: + match exc: + case httpx.TransportError(): + return True + case GoogleMetadataStatusError(status_code=status_code): + return status_code >= 500 + case _: + return False + + +def _google_id_token(reference: str, audience: str, timeout: float | None) -> str: + cached: Final = cached_oidc_token(reference) + if cached is not None: + return cached + oidc_client: Final = _get_oidc_http_handler() + try: + # https://cloud.google.com/compute/docs/instances/verifying-instance-identity#request_signature + response: Final = oidc_client.get( + _google_metadata_identity_url(), + params={"audience": audience}, + headers={"Metadata-Flavor": "Google"}, + timeout=timeout, + ) + finally: + oidc_client.close() + if response.status_code != 200: + raise GoogleMetadataStatusError(response.status_code) + token: Final = response.text + ttl: Final = _oidc_token_cache_ttl(token, 3600 - 60) + if not token: + verbose_logger.warning("Google OIDC token for %s is empty; not caching it", _single_line(reference)) + elif ttl > 0: + oidc_cache.set_cache(key=reference, value=token, ttl=ttl) + else: + verbose_logger.warning( + "Google OIDC token for %s is already expired or expires within %ss; not caching it", + _single_line(reference), + _OIDC_TOKEN_EXPIRY_MARGIN_SECONDS, + ) + return token + + +def cached_oidc_token(reference: str) -> str | None: + cached: Final = oidc_cache.get_cache(key=reference) # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType] # DualCache.get_cache is untyped + return cached if isinstance(cached, str) and cached else None + + +def _logged_unresolvable( + reference: str, kind: str, reason: str, detail: str | None = None, retryable: bool = False +) -> UnresolvableSecretReference: verbose_logger.warning( - "Secret reference %s could not be resolved: %s", reference.replace("\r", "").replace("\n", ""), reason + "Secret reference %s could not be resolved: %s", + _single_line(reference), + reason if detail is None else f"{reason} ({_single_line(detail)})", ) - return UnresolvableSecretReference(kind=kind, reason=reason) + return UnresolvableSecretReference(kind=kind, reason=reason, retryable=retryable) + + +def _single_line(text: str) -> str: + return text.replace("\r", "").replace("\n", "") def _unresolvable_reason(kind: str, exc: OSError | ValueError) -> str: @@ -273,31 +374,7 @@ def get_secret( oidc_aud = "/".join(secret_name_split.split("/")[1:]) # TODO: Add caching for HTTP requests if oidc_provider == "google": - oidc_token = oidc_cache.get_cache(key=secret_name) - if oidc_token is not None: - return oidc_token - - oidc_client = _get_oidc_http_handler() - # https://cloud.google.com/compute/docs/instances/verifying-instance-identity#request_signature - response = oidc_client.get( - "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity", - params={"audience": oidc_aud}, - headers={"Metadata-Flavor": "Google"}, - ) - if response.status_code == 200: - oidc_token = response.text - ttl: Final = _oidc_token_cache_ttl(oidc_token, 3600 - 60) - if ttl > 0: - oidc_cache.set_cache(key=secret_name, value=oidc_token, ttl=ttl) - else: - verbose_logger.warning( - "Google OIDC token for %s is already expired or expires within %ss; not caching it", - secret_name, - _OIDC_TOKEN_EXPIRY_MARGIN_SECONDS, - ) - return oidc_token - else: - raise ValueError("Google OIDC provider failed") + return _google_id_token(secret_name, oidc_aud, timeout=None) elif oidc_provider == "circleci": # https://circleci.com/docs/openid-connect-tokens/ env_secret = os.getenv("CIRCLE_OIDC_TOKEN") diff --git a/litellm/types/integrations/custom_logger.py b/litellm/types/integrations/custom_logger.py index 13542f22f88..77e75dea030 100644 --- a/litellm/types/integrations/custom_logger.py +++ b/litellm/types/integrations/custom_logger.py @@ -97,9 +97,9 @@ class AgenticLoopRequestPatch(LiteLLMBaseModel): model: str | None = None messages: list[dict[str, Any]] | None = None - tools: list[dict[str, Any]] | None = None + tools: list[dict[str, object]] | None = None max_tokens: int | None = None - optional_params: dict[str, Any] = Field(default_factory=dict) + optional_params: dict[str, object] = Field(default_factory=dict) kwargs: dict[str, Any] = Field(default_factory=dict) diff --git a/litellm/types/integrations/datadog_llm_obs.py b/litellm/types/integrations/datadog_llm_obs.py index f4fcabf53ed..f2042cb7967 100644 --- a/litellm/types/integrations/datadog_llm_obs.py +++ b/litellm/types/integrations/datadog_llm_obs.py @@ -5,7 +5,7 @@ API Reference: https://docs.datadoghq.com/llm_observability/setup/api/?tab=examp """ from collections.abc import Sequence -from typing import Any, Literal +from typing import Literal from typing_extensions import ReadOnly, TypedDict @@ -16,7 +16,7 @@ class ToolCall(TypedDict, total=False): """A tool call on a message, as LLM Obs names its fields.""" name: ReadOnly[str] - arguments: ReadOnly[dict[str, Any] | str] # parsed object, or the raw string when it will not parse to one + arguments: ReadOnly[dict[str, object] | str] # parsed object, or the raw string when it will not parse to one tool_id: ReadOnly[str] type: ReadOnly[str] @@ -35,7 +35,7 @@ class ToolDefinition(TypedDict, total=False): name: ReadOnly[str] description: ReadOnly[str] - schema: ReadOnly[dict[str, Any]] + schema: ReadOnly[dict[str, object]] class Message(TypedDict, total=False): @@ -49,13 +49,15 @@ class Message(TypedDict, total=False): class InputMeta(TypedDict): - messages: Sequence[ - Message | dict[str, Any] # changed to fit with tool calls + messages: ReadOnly[ + Sequence[ + Message | dict[str, object] # changed to fit with tool calls + ] ] # Relevant Issue: https://github.com/BerriAI/litellm/issues/9494 class OutputMeta(TypedDict): - messages: Sequence[Any] + messages: ReadOnly[Sequence[object]] class DDLLMObsError(TypedDict, total=False): @@ -71,7 +73,7 @@ class Meta(TypedDict, total=False): kind: Literal["llm", "tool", "task", "embedding", "retrieval"] input: InputMeta # The span's input information. output: OutputMeta # The span's output information. - metadata: dict[str, Any] + metadata: ReadOnly[dict[str, object]] error: DDLLMObsError | None # Error information on the span tool_definitions: ReadOnly[Sequence[ToolDefinition]] # The tools offered to the model on this request diff --git a/litellm/types/llms/anthropic.py b/litellm/types/llms/anthropic.py index de83e6ea66e..50c4a2340a3 100644 --- a/litellm/types/llms/anthropic.py +++ b/litellm/types/llms/anthropic.py @@ -832,6 +832,7 @@ ANTHROPIC_EFFORT_BETA_HEADER: Final = "effort-2025-11-24" ANTHROPIC_MID_CONVERSATION_OUTPUT_CONFIG_BETA_HEADER: Final = "mid-conversation-output-config-2026-07-01" ANTHROPIC_THINKING_DISPLAY_UPDATES_BETA_HEADER: Final = "thinking-display-updates-2026-08-18" +ANTHROPIC_THINKING_BINDING_CONTROLS_BETA_HEADER: Final = "thinking-binding-controls-2026-08-01" ANTHROPIC_MID_CONVERSATION_TOOL_CHANGES_BETA_HEADER: Final = "mid-conversation-tool-changes-2026-07-01" ANTHROPIC_FINE_GRAINED_TOOL_STREAMING_BETA_HEADER: Final = "fine-grained-tool-streaming-2025-05-14" diff --git a/litellm/types/llms/openai.py b/litellm/types/llms/openai.py index 6ce51abcd8a..d9d86cf54b3 100644 --- a/litellm/types/llms/openai.py +++ b/litellm/types/llms/openai.py @@ -1600,7 +1600,7 @@ class ResponsesAPIResponse(BaseLiteLLMOpenAIResponseObject): @field_validator("reasoning", mode="before") @classmethod - def validate_reasoning_to_dict(cls, value: Any) -> dict[str, Any] | None: + def validate_reasoning_to_dict(cls, value: Any) -> builtins.object: """Accept API reasoning dict (including effort 'none'/'xhigh'); always store as dict.""" if value is None: return None diff --git a/litellm/types/llms/vertex_ai.py b/litellm/types/llms/vertex_ai.py index 7cb5432d009..a938f6a7b1c 100644 --- a/litellm/types/llms/vertex_ai.py +++ b/litellm/types/llms/vertex_ai.py @@ -1,6 +1,6 @@ from collections.abc import Sequence from enum import Enum -from typing import Any, Final, Literal, Protocol +from typing import Final, Literal, Protocol from typing_extensions import ( ReadOnly, @@ -140,7 +140,7 @@ class Schema(TypedDict, total=False): title: str description: str nullable: bool - default: Any + default: ReadOnly[object] items: "Schema" minItems: str maxItems: str @@ -155,7 +155,7 @@ class Schema(TypedDict, total=False): minLength: str maxLength: str pattern: str - example: Any + example: ReadOnly[object] anyOf: list["Schema"] @@ -794,9 +794,9 @@ class VertexVideoGenerationResponse(TypedDict, total=False): name: str done: bool - response: dict[str, Any] - metadata: dict[str, Any] - error: dict[str, Any] + response: ReadOnly[dict[str, object]] + metadata: ReadOnly[dict[str, object]] + error: ReadOnly[dict[str, object]] VERTEX_CREDENTIALS_TYPES = str | dict[str, str] diff --git a/litellm/types/proxy/guardrails/guardrail_hooks/generic_guardrail_api.py b/litellm/types/proxy/guardrails/guardrail_hooks/generic_guardrail_api.py index d38e54986b9..4e4983c53f0 100644 --- a/litellm/types/proxy/guardrails/guardrail_hooks/generic_guardrail_api.py +++ b/litellm/types/proxy/guardrails/guardrail_hooks/generic_guardrail_api.py @@ -48,7 +48,7 @@ class GenericGuardrailAPIMetadata(TypedDict, total=False): class GenericGuardrailAPIOptionalParams(LiteLLMBaseModel): """Optional parameters for the Generic Guardrail API""" - additional_provider_specific_params: dict[str, Any] | None = Field( + additional_provider_specific_params: dict[str, object] | None = Field( default=None, description="Additional provider-specific parameters to send with the guardrail request", ) @@ -161,7 +161,7 @@ class GenericGuardrailAPIRequest(LiteLLMBaseModel): default=None, description="LiteLLM library version running this proxy.", ) - additional_provider_specific_params: dict[str, Any] | None = None + additional_provider_specific_params: dict[str, object] | None = None tool_calls: list[ChatCompletionToolCallChunk] | list[ChatCompletionMessageToolCall] | None = None model: str | None = None # the model being used for the LLM call diff --git a/litellm/types/proxy/guardrails/guardrail_hooks/ibm/ibm_detector.py b/litellm/types/proxy/guardrails/guardrail_hooks/ibm/ibm_detector.py index b58d1bff8a2..b04be71dc9c 100644 --- a/litellm/types/proxy/guardrails/guardrail_hooks/ibm/ibm_detector.py +++ b/litellm/types/proxy/guardrails/guardrail_hooks/ibm/ibm_detector.py @@ -1,7 +1,5 @@ -from typing import Any - from pydantic import Field -from typing_extensions import TypedDict +from typing_extensions import ReadOnly, TypedDict from litellm.types.llms.base import LiteLLMBaseModel @@ -14,41 +12,41 @@ from .base import IBMGuardrailsBaseConfigModel class IBMDetectorRequestBodyDetectorServer(TypedDict): """Request body for calling IBM Detector Server directly""" - contents: list[str] - detector_params: dict[str, Any] + contents: ReadOnly[list[str]] + detector_params: ReadOnly[dict[str, object]] class IBMDetectorRequestBodyOrchestrator(TypedDict): """Request body for calling IBM Detector via FMS Guardrails Orchestrator""" - content: str - detectors: dict[str, dict[str, Any]] + content: ReadOnly[str] + detectors: ReadOnly[dict[str, dict[str, object]]] class IBMDetectorDetection(TypedDict, total=False): """Individual detection from IBM Detector""" - start: int - end: int - text: str - detection: str - detection_type: str - score: float - evidences: list[Any] - metadata: dict[str, Any] - detector_id: str | None # Only present in orchestrator response + start: ReadOnly[int] + end: ReadOnly[int] + text: ReadOnly[str] + detection: ReadOnly[str] + detection_type: ReadOnly[str] + score: ReadOnly[float] + evidences: ReadOnly[list[object]] + metadata: ReadOnly[dict[str, object]] + detector_id: ReadOnly[str | None] # Only present in orchestrator response class IBMDetectorResponseDetectorServer(TypedDict): """Response from IBM Detector Server (returns list of lists)""" - detections: list[list[IBMDetectorDetection]] + detections: ReadOnly[list[list[IBMDetectorDetection]]] class IBMDetectorResponseOrchestrator(TypedDict): """Response from IBM FMS Guardrails Orchestrator""" - detections: list[IBMDetectorDetection] + detections: ReadOnly[list[IBMDetectorDetection]] # Pydantic Config Models @@ -57,12 +55,12 @@ class IBMDetectorResponseOrchestrator(TypedDict): class IBMDetectorOptionalParams(LiteLLMBaseModel): """Optional parameters for IBM Detector guardrail""" - detector_params: dict[str, Any] | None = Field( + detector_params: dict[str, object] | None = Field( default_factory=lambda: {}, description="Dictionary of arguments to pass to the detector.", ) - extra_headers: dict[str, Any] | None = Field( + extra_headers: dict[str, object] | None = Field( default_factory=lambda: {}, description="Dictionary of extra headers to pass to the detector.", ) diff --git a/litellm/types/proxy/management_endpoints/key_management_endpoints.py b/litellm/types/proxy/management_endpoints/key_management_endpoints.py index f4348391710..d4f41b538e6 100644 --- a/litellm/types/proxy/management_endpoints/key_management_endpoints.py +++ b/litellm/types/proxy/management_endpoints/key_management_endpoints.py @@ -1,5 +1,5 @@ from datetime import datetime -from typing import Any, Final, Literal, TypeAlias +from typing import Final, Literal, TypeAlias from pydantic import ConfigDict, model_validator from typing_extensions import ReadOnly, TypedDict @@ -50,14 +50,14 @@ class SuccessfulKeyUpdate(LiteLLMBaseModel): """Successfully updated key with its updated information""" key: str - key_info: dict[str, Any] + key_info: dict[str, object] class FailedKeyUpdate(LiteLLMBaseModel): """Failed key update with reason""" key: str - key_info: dict[str, Any] | None = None + key_info: dict[str, object] | None = None failed_reason: str @@ -78,14 +78,14 @@ class KeyUpdateFields(LiteLLMBaseModel): max_budget: float | None = None budget_id: str | None = None budget_duration: str | None = None - budget_limits: list[Any] | None = None - model_max_budget: dict[str, Any] | None = None + budget_limits: list[object] | None = None + model_max_budget: dict[str, object] | None = None # Rate limits tpm_limit: int | None = None rpm_limit: int | None = None - model_tpm_limit: dict[str, Any] | None = None - model_rpm_limit: dict[str, Any] | None = None + model_tpm_limit: dict[str, object] | None = None + model_rpm_limit: dict[str, object] | None = None max_parallel_requests: int | None = None rpm_limit_type: Literal["guaranteed_throughput", "best_effort_throughput", "dynamic"] | None = None tpm_limit_type: Literal["guaranteed_throughput", "best_effort_throughput", "dynamic"] | None = None @@ -99,7 +99,7 @@ class KeyUpdateFields(LiteLLMBaseModel): # Operational metadata tags: list[str] | None = None - metadata: dict[str, Any] | None = None + metadata: dict[str, object] | None = None @model_validator(mode="after") def validate_temp_budget(self) -> "KeyUpdateFields": diff --git a/litellm/types/router.py b/litellm/types/router.py index fd796ff5b56..f927ae0475b 100644 --- a/litellm/types/router.py +++ b/litellm/types/router.py @@ -69,6 +69,10 @@ from .utils import ( AllowedModelRegion: TypeAlias = Literal["eu", "us"] +class SilentExperimentFlag(TypedDict, total=False): + is_silent_experiment: ReadOnly[bool] + + class FallbackSkipReason(str, enum.Enum): NOT_AUTHORIZED = "not_authorized" OVER_BUDGET = "over_budget" @@ -268,6 +272,7 @@ def _whole_float_as_int(value: object) -> object: class ModelInfo(MirroredPricingParams): + enforce_supported_endpoints: bool | None = None id: str | None # Allow id to be optional on input, but it will always be present as a str in the model instance db_model: bool = False # used for proxy - to separate models which are stored in the db vs. config. updated_at: datetime.datetime | None = None diff --git a/litellm/types/utils.py b/litellm/types/utils.py index 266a6a20131..3bdcba75778 100644 --- a/litellm/types/utils.py +++ b/litellm/types/utils.py @@ -263,10 +263,10 @@ class AgenticLoopParams(TypedDict, total=False): """ model: str - """The model string with provider prefix (e.g., 'bedrock/invoke/...')""" + """The model as the caller requested it, usually with its provider prefix (e.g., 'bedrock/invoke/...')""" custom_llm_provider: str - """The LLM provider name (e.g., 'bedrock', 'anthropic')""" + """The LLM provider name (e.g., 'bedrock', 'anthropic'), when the recorder resolved one""" class OffPeakWindow(TypedDict, total=False): @@ -3374,6 +3374,7 @@ class StandardLoggingMetadata(StandardLoggingUserAPIKeyMetadata): team_alias: str | None team_id: str | None used_client_oauth_token: ReadOnly[bool | None] + is_silent_experiment: ReadOnly[NotRequired[bool]] class AzureSpillover(TypedDict): @@ -4130,11 +4131,28 @@ class CustomPricingLiteLLMParams(MirroredPricingParams): """ return {k: v for k, v in model_info.items() if k not in cls.model_fields} + def custom_pricing_overrides(self) -> Mapping[str, object]: + return MappingProxyType( + self.model_dump(include=set(CustomPricingLiteLLMParams.model_fields), exclude_none=True) + ) + + def per_second_pricing(self) -> Mapping[str, float]: + return MappingProxyType( + self.model_dump( + include={"cost_per_second", "input_cost_per_second", "output_cost_per_second"}, exclude_none=True + ) + ) + DEPLOYMENT_SCOPED_PRICING_FIELDS: Final[frozenset[str]] = frozenset({"off_peak_pricing"}) DEPLOYMENT_SCOPED_CAPABILITY_FIELDS: Final[frozenset[str]] = frozenset( - {"supports_regex_lookaround", "supports_assistant_prefill", "supports_bedrock_native_structured_output"} + { + "supports_regex_lookaround", + "supports_assistant_prefill", + "supports_bedrock_native_structured_output", + "supports_pdf_input", + } ) DEPLOYMENT_SCOPED_TOKEN_COUNT_FIELDS: Final[frozenset[str]] = frozenset({"token_count_framing", "token_count_model"}) @@ -4546,6 +4564,7 @@ class LlmProviders(str, Enum): S3_VECTORS = "s3_vectors" VALKEY = "valkey" MONGODB = "mongodb" + OPENSEARCH = "opensearch" HELICONE = "helicone" HYPERBOLIC = "hyperbolic" RECRAFT = "recraft" @@ -4599,6 +4618,7 @@ class LlmProviders(str, Enum): CURSOR = "cursor" BEDROCK_MANTLE = "bedrock_mantle" GDC = "gdc" + KYMA = "kyma" # Create a set of all provider values for quick lookup diff --git a/litellm/utils.py b/litellm/utils.py index adf195668ee..4a0e4cafb22 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -28,6 +28,7 @@ import textwrap import threading import time import traceback +from concurrent.futures import ThreadPoolExecutor from contextlib import nullcontext from dataclasses import dataclass, field, replace from functools import lru_cache, partial, wraps @@ -91,7 +92,7 @@ from litellm.litellm_core_utils.fallback_generalizations import ( match_capability_generalizations, match_fill_missing_generalizations, ) -from litellm.litellm_core_utils.get_model_cost_map import AliasedCostEntry +from litellm.litellm_core_utils.get_model_cost_map import AliasedCostEntry, GetModelCostMap from litellm.litellm_core_utils.model_param_helper import peek_reasoning_summary_aliases from litellm.litellm_core_utils.offline_mode import is_offline_mode from litellm.litellm_core_utils.sensitive_data_masker import redact_credentials_in_payload @@ -477,11 +478,14 @@ from litellm.llms.base_llm.evals.transformation import BaseEvalsAPIConfig from litellm.llms.base_llm.responses.transformation import BaseResponsesAPIConfig from litellm.llms.base_llm.skills.transformation import BaseSkillsAPIConfig from litellm.secret_managers.main import ( - LOCAL_OIDC_SECRET_PREFIXES, + API_KEY_OIDC_SECRET_PREFIXES, + GOOGLE_OIDC_SECRET_PREFIX, + OidcResolver, UnresolvableSecretReference, + cached_oidc_token, get_secret, oidc_reference_kind, - resolve_local_oidc_reference, + resolve_api_key_oidc_reference, ) from ._logging import is_debugging_on, verbose_logger @@ -784,27 +788,97 @@ def _named_credential_values(kwargs: Mapping[str, object]) -> Mapping[str, objec ) -def resolve_api_key_secret_reference( +async def aresolve_api_key_secret_reference( api_key: str | None, model: object, llm_provider: object, upstream: Mapping[str, object] ) -> str | None: if not isinstance(api_key, AdminApiKeySecretReference): return api_key - return _secret_behind_api_key_reference(api_key, model=model, llm_provider=llm_provider, upstream=upstream) + return await _resolved_off_the_event_loop( + partial(_secret_behind_api_key_reference, api_key, model, llm_provider, upstream), api_key + ) + + +_OIDC_TOKEN_FETCH_EXECUTOR: Final = ThreadPoolExecutor(thread_name_prefix="litellm-oidc-token-fetch") +_PASS_THROUGH_CALL_TYPES: Final = frozenset( + {CallTypes.allm_passthrough_route.value, CallTypes.llm_passthrough_route.value} +) +_Resolved: Final = TypeVar("_Resolved") + + +async def _resolved_off_the_event_loop(resolve: Callable[[OidcResolver], _Resolved], api_key: object) -> _Resolved: + if not isinstance(api_key, AdminApiKeySecretReference) or not api_key.startswith(GOOGLE_OIDC_SECRET_PREFIX): + return resolve(resolve_api_key_oidc_reference) + match cached_oidc_token(api_key): + case str() as token: + return resolve(lambda _reference: token) + case None: + _raise_if_the_reference_may_not_be_used(resolve) + fetched: Final = await asyncio.shield( + _google_token_fetch(asyncio.get_running_loop(), api_key).start_or_join() + ) + return resolve(lambda _reference: fetched) + + +def _raise_if_the_reference_may_not_be_used(resolve: Callable[[OidcResolver], object]) -> None: + _ = resolve(lambda _reference: "") + + +@dataclass(slots=True) +class _GoogleTokenFetch: + reference: str + in_flight: asyncio.Future[str | UnresolvableSecretReference] | None = None + + def start_or_join(self) -> asyncio.Future[str | UnresolvableSecretReference]: + if self.in_flight is not None: + return self.in_flight + caller_context: Final = contextvars.copy_context() + fetch: Final = asyncio.get_running_loop().run_in_executor( + _OIDC_TOKEN_FETCH_EXECUTOR, lambda: caller_context.run(resolve_api_key_oidc_reference, self.reference) + ) + fetch.add_done_callback(self._land) + self.in_flight = fetch + return fetch + + def _land(self, _fetch: asyncio.Future[str | UnresolvableSecretReference]) -> None: + self.in_flight = None + + +@lru_cache(maxsize=256) +def _google_token_fetch(loop: asyncio.AbstractEventLoop, reference: str) -> _GoogleTokenFetch: + return _GoogleTokenFetch(reference) def _secret_behind_api_key_reference( - reference: AdminApiKeySecretReference, model: object, llm_provider: object, upstream: Mapping[str, object] + reference: AdminApiKeySecretReference, + model: object, + llm_provider: object, + upstream: Mapping[str, object], + resolve_oidc: OidcResolver = resolve_api_key_oidc_reference, + call_type: str | None = None, ) -> str: if not reference.may_reach(upstream): raise _secret_reference_error(_unreachable_upstream_message(reference), llm_provider=llm_provider, model=model) - if not reference.startswith(LOCAL_OIDC_SECRET_PREFIXES): + if not reference.startswith(API_KEY_OIDC_SECRET_PREFIXES): raise _secret_reference_error( f"api_key is an {oidc_reference_kind(reference)} secret reference, a kind api_key does not support. " - "Use oidc/file/ or oidc/env/.", + "Use oidc/file/, oidc/env/ or oidc/google/.", llm_provider=llm_provider, model=model, ) - match resolve_local_oidc_reference(reference): + if call_type in _PASS_THROUGH_CALL_TYPES and reference.startswith(GOOGLE_OIDC_SECRET_PREFIX): + raise _secret_reference_error( + "api_key oidc/google secret reference could not be resolved: pass-through routes do not fetch Google ID " + "tokens. Use a static key, oidc/file/ or oidc/env/.", + llm_provider=llm_provider, + model=model, + ) + match resolve_oidc(reference): + case UnresolvableSecretReference(kind=kind, reason=reason, retryable=True): + raise ServiceUnavailableError( + message=f"api_key {kind} secret reference could not be resolved: {reason}", + llm_provider=llm_provider, + model=model, + ) case UnresolvableSecretReference(kind=kind, reason=reason): raise _secret_reference_error( f"api_key {kind} secret reference could not be resolved: {reason}", @@ -836,21 +910,39 @@ def with_named_credential(kwargs: Mapping[str, object]) -> Mapping[str, object]: return {**kwargs, **_named_credential_values(kwargs)} -def load_credentials_from_list(kwargs: dict[str, object]) -> None: +def load_credentials_from_list(kwargs: dict[str, object], call_type: str | None = None) -> None: """ Updates kwargs with the credentials if credential_name in kwarg, then swaps an admin-configured oidc/ api_key reference for the secret it points to, read now so a rotated token is used on the next call """ - kwargs.update(_credential_updates(kwargs)) + kwargs.update(_credential_updates(kwargs, _named_credential_values(kwargs), call_type=call_type)) -def _credential_updates(kwargs: Mapping[str, object]) -> Mapping[str, object]: +async def acredential_updates(kwargs: Mapping[str, object], call_type: str | None = None) -> Mapping[str, object]: credential_values: Final = _named_credential_values(kwargs) - api_key: Final = kwargs.get("api_key", credential_values.get("api_key")) + return await _resolved_off_the_event_loop( + partial(_credential_updates, kwargs, credential_values, call_type=call_type), + _api_key_after_credentials(kwargs, credential_values), + ) + + +def _api_key_after_credentials(kwargs: Mapping[str, object], credential_values: Mapping[str, object]) -> object: + return kwargs.get("api_key", credential_values.get("api_key")) + + +def _credential_updates( + kwargs: Mapping[str, object], + credential_values: Mapping[str, object], + resolve_oidc: OidcResolver = resolve_api_key_oidc_reference, + call_type: str | None = None, +) -> Mapping[str, object]: + api_key: Final = _api_key_after_credentials(kwargs, credential_values) if not isinstance(api_key, AdminApiKeySecretReference): return credential_values secret: Final = _secret_behind_api_key_reference( api_key, + resolve_oidc=resolve_oidc, + call_type=call_type, model=kwargs.get("model"), llm_provider=kwargs.get("custom_llm_provider"), upstream=MappingProxyType( @@ -1540,15 +1632,19 @@ def _schedule_async_success_logging( return def _enqueue_async_logging() -> None: + from litellm.responses.streaming_iterator import spawn_logging_task + logging_obj.mark_async_success_log_enqueued() - asyncio.create_task( + spawn_logging_task( + asyncio.get_running_loop(), _client_async_logging_helper( logging_obj=logging_obj, result=result, start_time=start_time, end_time=end_time, is_completion_with_fallbacks=is_completion_with_fallbacks, - ) + ), + task_name="success callback enqueue", ) if not logging_obj.defer_async_logging: @@ -1936,7 +2032,7 @@ def client(original_function): assert logging_obj is not None, "logging_obj should not be None after function_setup" ## LOAD CREDENTIALS - load_credentials_from_list(kwargs) + load_credentials_from_list(kwargs, call_type) from litellm.llms.github_copilot.per_user_auth import attach_github_copilot_user_session attach_github_copilot_user_session(kwargs) # pyright: ignore[reportUnknownArgumentType] # kwargs is the untyped request dict @@ -2230,7 +2326,7 @@ def client(original_function): kwargs["litellm_logging_obj"] = logging_obj ## LOAD CREDENTIALS - load_credentials_from_list(kwargs) + kwargs.update(await acredential_updates(kwargs, call_type)) # pyright: ignore[reportUnknownMemberType, reportUnknownArgumentType] # kwargs is the untyped request dict from litellm.llms.github_copilot.per_user_auth import aattach_github_copilot_user_session await aattach_github_copilot_user_session(kwargs) # pyright: ignore[reportUnknownArgumentType] # kwargs is the untyped request dict @@ -3852,7 +3948,7 @@ def _resolve_builtin_model_cost_entry(key: str, provider: str) -> _BuiltinCostMa return None -def is_generalized_model_info(model_info: ModelInfo) -> bool: +def is_generalized_model_info(model_info: ModelInfoBase) -> bool: """Whether ``model_info`` came from a fallback-generalization capability rule. Detected as the resolved key missing ``litellm.model_cost`` while matching a @@ -3868,10 +3964,12 @@ def is_generalized_model_info(model_info: ModelInfo) -> bool: _REGISTRATION_ATTEMPTS: Final = 3 +_ModelInfoT: Final = TypeVar("_ModelInfoT", bound=ModelInfoBase) -def lookup_model_info_for_registration(model: str, custom_llm_provider: str | None) -> ModelInfo | ModelInfoLookupMiss: + +def _lookup_mapped_model_info(lookup: Callable[[], _ModelInfoT]) -> _ModelInfoT | ModelInfoLookupMiss: try: - info: Final = get_model_info(model=model, custom_llm_provider=custom_llm_provider) + info: Final = lookup() except ModelNotMappedError: return ModelInfoLookupMiss(tripped=False) except Exception: @@ -3879,6 +3977,16 @@ def lookup_model_info_for_registration(model: str, custom_llm_provider: str | No return ModelInfoLookupMiss(tripped=False) if is_generalized_model_info(info) else info +def lookup_model_info_for_registration(model: str, custom_llm_provider: str | None) -> ModelInfo | ModelInfoLookupMiss: + return _lookup_mapped_model_info(lambda: get_model_info(model=model, custom_llm_provider=custom_llm_provider)) + + +def lookup_catalog_model_info(model: str, custom_llm_provider: str | None) -> ModelInfoBase | ModelInfoLookupMiss: + return _lookup_mapped_model_info( + lambda: get_model_info_helper(model=model, custom_llm_provider=custom_llm_provider, catalog_only=True) + ) + + def _get_builtin_model_info_for_registration( model: str, custom_llm_provider: str | None ) -> ModelInfo | ModelInfoLookupMiss: @@ -5144,6 +5252,46 @@ def _declared_reasoning_efforts(model_info: Mapping[str, object] | None) -> tupl return deployment_reasoning_effort_levels(model_info) or () +_DROPPED_REASONING_PARAMS: Final = ("reasoning_effort", "thinking") + + +def _turns_reasoning_off(value: object) -> bool: + match value: + case "none" | {"effort": "none"} | {"type": "disabled"}: + return True + case _: + return False + + +def is_known_non_reasoning_model(model: str, custom_llm_provider: str | None) -> bool: + info: Final = lookup_catalog_model_info(model, custom_llm_provider) + if isinstance(info, ModelInfoLookupMiss) or info.get("supports_reasoning") is True: + return False + return info.get("supports_reasoning") is False or info["key"] in GetModelCostMap.loaded_model_cost_map() + + +def has_catalog_entry(model: str, custom_llm_provider: str | None) -> bool: + info: Final = lookup_catalog_model_info(model, custom_llm_provider) + return not isinstance(info, ModelInfoLookupMiss) and info["key"] in GetModelCostMap.loaded_model_cost_map() + + +def _dropped_reasoning_warning(model: str, custom_llm_provider: str | None) -> str: + return "".join( + ( + "Dropping %s=%r for model=%r (drop_params=True) because %s does not support %s for it.", + ( + "" + if has_catalog_entry(model, custom_llm_provider) + else " If the deployment is a reasoning model, set model_info.base_model to its underlying model." + ), + ) + ) + + +def _warns_on_dropped_reasoning(value: object, model: str, custom_llm_provider: str | None) -> bool: + return not (_turns_reasoning_off(value) and is_known_non_reasoning_model(model, custom_llm_provider)) + + def get_optional_params( # use the openai defaults # https://platform.openai.com/docs/api-reference/chat/create @@ -5233,19 +5381,30 @@ def get_optional_params( verbose_logger.debug( "\nLiteLLM: Non-Default params passed to completion() %s", _debug_params_repr(non_default_params) ) - unsupported_params: Final = {} - for k in non_default_params: - if k not in supported_params: - if k in PROVIDER_UNVALIDATED_PARAMS: - continue - if k == "n" and n == 1: # langchain sends n=1 as a default value - continue # skip this param - unsupported_params[k] = non_default_params[k] + unsupported_params: Final[MappingProxyType[str, object]] = MappingProxyType( + { + k: non_default_params[k] + for k in non_default_params + if k not in supported_params and k not in PROVIDER_UNVALIDATED_PARAMS and not (k == "n" and n == 1) + } + ) if unsupported_params: if litellm.drop_params is True or (drop_params is not None and drop_params is True): for k in unsupported_params: non_default_params.pop(k, None) + for reasoning_param in _DROPPED_REASONING_PARAMS: + if reasoning_param in unsupported_params and _warns_on_dropped_reasoning( + unsupported_params[reasoning_param], base_model or model, custom_llm_provider + ): + verbose_logger.warning( + _dropped_reasoning_warning(base_model or model, custom_llm_provider), + reasoning_param, + unsupported_params[reasoning_param], + model, + custom_llm_provider, + reasoning_param, + ) else: raise UnsupportedParamsError( status_code=500, @@ -6761,6 +6920,8 @@ def get_model_info_helper( custom_llm_provider: str | None = None, api_base: str | None = None, api_key: str | None = None, + *, + catalog_only: bool = False, ) -> ModelInfoBase: """ Helper for 'get_model_info'. Separated out to avoid infinite loop caused by returning 'supported_openai_param's @@ -6794,7 +6955,7 @@ def get_model_info_helper( model_cost_custom_llm_provider: Final = custom_llm_provider ######################### provider_config: BaseLLMModelInfo | None = None - if custom_llm_provider and custom_llm_provider in LlmProvidersSet: + if not catalog_only and custom_llm_provider and custom_llm_provider in LlmProvidersSet: provider_config = ProviderConfigManager.get_provider_model_info( model=model, provider=LlmProviders(custom_llm_provider) ) @@ -6818,7 +6979,7 @@ def get_model_info_helper( e, ) - if custom_llm_provider == "huggingface": + if not catalog_only and custom_llm_provider == "huggingface": max_tokens: Final = _get_max_position_embeddings(model_name=model) return ModelInfoBase( key=model, @@ -9891,6 +10052,12 @@ class ProviderConfigManager: ) return TencentAnthropicMessagesConfig() + elif litellm.LlmProviders.DASHSCOPE == provider: + from litellm.llms.dashscope.messages.transformation import ( + DashScopeAnthropicMessagesConfig, + ) + + return DashScopeAnthropicMessagesConfig() elif litellm.LlmProviders.GITHUB_COPILOT == provider: if "claude" in model_lower: from litellm.llms.github_copilot.messages.transformation import ( @@ -10395,6 +10562,22 @@ class ProviderConfigManager: return BedrockVectorStore.get_initialized_custom_logger() return None + @staticmethod + def _get_vertex_ai_vector_stores_config(api_type: str | None) -> BaseVectorStoreConfig | None: + if api_type == "rag_api" or api_type is None: + from litellm.llms.vertex_ai.vector_stores.rag_api.transformation import ( + VertexVectorStoreConfig, + ) + + return VertexVectorStoreConfig() + if api_type == "search_api": + from litellm.llms.vertex_ai.vector_stores.search_api.transformation import ( + VertexSearchAPIVectorStoreConfig, + ) + + return VertexSearchAPIVectorStoreConfig() + return None + @staticmethod def get_provider_vector_stores_config( provider: LlmProviders, @@ -10416,18 +10599,7 @@ class ProviderConfigManager: return AzureOpenAIVectorStoreConfig() elif litellm.LlmProviders.VERTEX_AI == provider: - if api_type == "rag_api" or api_type is None: # default to rag_api - from litellm.llms.vertex_ai.vector_stores.rag_api.transformation import ( - VertexVectorStoreConfig, - ) - - return VertexVectorStoreConfig() - elif api_type == "search_api": - from litellm.llms.vertex_ai.vector_stores.search_api.transformation import ( - VertexSearchAPIVectorStoreConfig, - ) - - return VertexSearchAPIVectorStoreConfig() + return ProviderConfigManager._get_vertex_ai_vector_stores_config(api_type) elif litellm.LlmProviders.BEDROCK == provider: from litellm.llms.bedrock.vector_stores.transformation import ( BedrockVectorStoreConfig, @@ -10482,6 +10654,12 @@ class ProviderConfigManager: ) return MongoDBVectorStoreConfig() + elif litellm.LlmProviders.OPENSEARCH == provider: + from litellm.llms.opensearch.vector_stores.transformation import ( + OpenSearchVectorStoreConfig, + ) + + return OpenSearchVectorStoreConfig() return None @staticmethod @@ -10694,6 +10872,7 @@ class ProviderConfigManager: model: str, provider: LlmProviders, drop_params: bool = False, + litellm_params: Mapping[str, object] = MappingProxyType({}), ) -> BaseRealtimeConfig | None: if LlmProviders.GEMINI == provider: from litellm.llms.gemini.realtime.transformation import GeminiRealtimeConfig @@ -10703,6 +10882,10 @@ class ProviderConfigManager: from litellm.llms.meta.realtime.transformation import MetaRealtimeConfig return MetaRealtimeConfig(drop_params=drop_params) + if LlmProviders.SONIOX == provider: + from litellm.llms.soniox.realtime.transformation import SonioxRealtimeConfig + + return SonioxRealtimeConfig.from_litellm_params(litellm_params, drop_params=drop_params) return None @staticmethod diff --git a/migrations/Dockerfile b/migrations/Dockerfile index 2b1fd029143..3ffa9140543 100644 --- a/migrations/Dockerfile +++ b/migrations/Dockerfile @@ -43,6 +43,7 @@ ENV UV_PYTHON_DOWNLOADS=0 COPY pyproject.toml README.md rust-toolchain.toml ./ COPY .cargo ./.cargo COPY litellm-rust ./litellm-rust +COPY model_prices_and_context_window.json ./ RUN --mount=type=cache,target=/root/.cache/uv \ mkdir -p litellm/rust_bridge /wheels && \ diff --git a/model_prices_and_context_window.json b/model_prices_and_context_window.json index aa00ea73f8c..c6bbb63b6fc 100644 --- a/model_prices_and_context_window.json +++ b/model_prices_and_context_window.json @@ -4049,6 +4049,7 @@ "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, "supports_parallel_function_calling": true, + "supports_pdf_input": false, "supports_response_schema": true, "supports_tool_choice": true }, @@ -12437,7 +12438,8 @@ "supported_output_modalities": [ "text" ], - "supports_native_streaming": true + "supports_native_streaming": true, + "supports_pdf_input": false }, "azure_ai/FLUX-1.1-pro": { "deprecation_date": "2027-01-05", @@ -12502,6 +12504,7 @@ "output_cost_per_token": 1.85e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -12516,6 +12519,7 @@ "output_cost_per_token": 3.828e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -12531,6 +12535,7 @@ "output_cost_per_token": 3.52e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -12546,6 +12551,7 @@ "output_cost_per_token": 4.84e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -12560,6 +12566,7 @@ "output_cost_per_token": 4.84e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -12574,6 +12581,7 @@ "output_cost_per_token": 7.26e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -12594,6 +12602,7 @@ "text" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -12616,6 +12625,7 @@ "text" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true, @@ -12638,6 +12648,7 @@ "text" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true, @@ -12660,6 +12671,7 @@ "text" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true, @@ -12687,6 +12699,7 @@ "text" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true, @@ -12703,6 +12716,7 @@ "output_cost_per_token": 1.32e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -12724,6 +12738,7 @@ "text" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true, @@ -12744,6 +12759,7 @@ "text" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true, @@ -12765,6 +12781,7 @@ "text" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -12833,6 +12850,7 @@ "text" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -12847,6 +12865,7 @@ "output_cost_per_token": 7.1e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/foundry-models/concepts/models-sold-directly-by-azure", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true }, "azure_ai/Llama-4-Maverick-17B-128E-Instruct-FP8": { @@ -12859,6 +12878,7 @@ "output_cost_per_token": 1e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": true }, @@ -12872,6 +12892,7 @@ "output_cost_per_token": 7.8e-07, "source": "https://azure.microsoft.com/en-us/blog/introducing-the-llama-4-herd-in-azure-ai-foundry-and-azure-databricks/", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": true }, @@ -12884,6 +12905,7 @@ "mode": "chat", "output_cost_per_token": 3.7e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true }, "azure_ai/Meta-Llama-3.1-70B-Instruct": { @@ -12895,6 +12917,7 @@ "mode": "chat", "output_cost_per_token": 3.54e-06, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true }, "azure_ai/Phi-3-medium-128k-instruct": { @@ -12906,6 +12929,7 @@ "mode": "chat", "output_cost_per_token": 6.8e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": false }, @@ -12918,6 +12942,7 @@ "mode": "chat", "output_cost_per_token": 6.8e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": false }, @@ -12930,6 +12955,7 @@ "mode": "chat", "output_cost_per_token": 5.2e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": false }, @@ -12942,6 +12968,7 @@ "mode": "chat", "output_cost_per_token": 5.2e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": false }, @@ -12954,6 +12981,7 @@ "mode": "chat", "output_cost_per_token": 6e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": false }, @@ -12966,6 +12994,7 @@ "mode": "chat", "output_cost_per_token": 6e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": false }, @@ -12978,6 +13007,7 @@ "mode": "chat", "output_cost_per_token": 6.4e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": false }, @@ -12990,6 +13020,7 @@ "mode": "chat", "output_cost_per_token": 5.2e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": false }, @@ -13002,6 +13033,7 @@ "mode": "chat", "output_cost_per_token": 5.2e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": true }, @@ -13015,6 +13047,7 @@ "output_cost_per_token": 5e-07, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": false }, @@ -13027,7 +13060,8 @@ "mode": "chat", "output_cost_per_token": 3e-07, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", - "supports_function_calling": true + "supports_function_calling": true, + "supports_pdf_input": false }, "azure_ai/Phi-4-multimodal-instruct": { "deprecation_date": "2027-01-17", @@ -13041,6 +13075,7 @@ "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_audio_input": true, "supports_function_calling": true, + "supports_pdf_input": false, "supports_vision": true }, "azure_ai/Phi-4-mini-reasoning": { @@ -13052,7 +13087,8 @@ "mode": "chat", "output_cost_per_token": 3e-07, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", - "supports_function_calling": true + "supports_function_calling": true, + "supports_pdf_input": false }, "azure_ai/Phi-4-reasoning": { "deprecation_date": "2027-01-17", @@ -13064,6 +13100,7 @@ "output_cost_per_token": 5e-07, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true, "supports_reasoning": true }, @@ -13106,6 +13143,7 @@ "output_cost_per_token": 1e-05, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true }, "azure_ai/doc-intelligence/prebuilt-read": { @@ -13144,6 +13182,7 @@ "mode": "chat", "output_cost_per_token": 5.4e-06, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_reasoning": true, "supports_tool_choice": true }, @@ -13202,6 +13241,7 @@ "source": "https://learn.microsoft.com/en-us/azure/foundry/foundry-models/concepts/models-sold-directly-by-azure", "supports_assistant_prefill": true, "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -13217,6 +13257,7 @@ "source": "https://learn.microsoft.com/en-us/azure/foundry/foundry-models/concepts/models-sold-directly-by-azure", "supports_assistant_prefill": true, "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -13230,6 +13271,7 @@ "mode": "chat", "output_cost_per_token": 4.56e-06, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true }, "azure_ai/deepseek-v4-pro": { @@ -13242,6 +13284,7 @@ "output_cost_per_token": 3.48e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_reasoning": true, "supports_tool_choice": true, "cache_read_input_token_cost": 1.45e-07, @@ -13257,6 +13300,7 @@ "output_cost_per_token": 5.1e-07, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_reasoning": true, "supports_tool_choice": true, "cache_read_input_token_cost": 2.8e-08, @@ -13273,6 +13317,7 @@ "output_cost_per_token": 1.32e-06, "source": "https://learn.microsoft.com/en-us/azure/foundry/foundry-models/concepts/models-sold-directly-by-azure", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -13306,6 +13351,7 @@ "output_cost_per_token": 1.5e-05, "source": "https://learn.microsoft.com/en-us/azure/foundry/foundry-models/concepts/models-sold-directly-by-azure", "supports_function_calling": true, + "supports_pdf_input": false, "supports_response_schema": true, "supports_tool_choice": true, "supports_web_search": true @@ -13323,6 +13369,7 @@ "output_cost_per_token_above_200k_tokens": 5e-06, "source": "https://learn.microsoft.com/en-us/azure/foundry/foundry-models/concepts/models-sold-directly-by-azure", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_response_schema": true, @@ -13343,6 +13390,7 @@ "output_cost_per_token_above_200k_tokens": 1.2e-05, "source": "https://azure.microsoft.com/en-us/pricing/details/ai-foundry-models/grok/", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_response_schema": true, @@ -13361,6 +13409,7 @@ "output_cost_per_token": 2.5e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_response_schema": true, "supports_tool_choice": true, @@ -13379,6 +13428,7 @@ "output_cost_per_token": 2.5e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_response_schema": true, "supports_tool_choice": true, @@ -13395,6 +13445,7 @@ "mode": "chat", "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_response_schema": true, "supports_tool_choice": true, "supports_web_search": true @@ -13409,6 +13460,7 @@ "mode": "chat", "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_reasoning": true, "supports_response_schema": true, "supports_tool_choice": true, @@ -13424,6 +13476,7 @@ "output_cost_per_token": 1.5e-06, "source": "https://learn.microsoft.com/en-us/azure/foundry/foundry-models/concepts/models-sold-directly-by-azure", "supports_function_calling": true, + "supports_pdf_input": false, "supports_response_schema": true, "supports_tool_choice": true, "supports_web_search": true @@ -13436,7 +13489,8 @@ "max_output_tokens": 8192, "mode": "chat", "output_cost_per_token": 0.00971, - "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models" + "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false }, "azure_ai/jamba-instruct": { "deprecation_date": "2025-03-01", @@ -13447,6 +13501,7 @@ "mode": "chat", "output_cost_per_token": 7e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", + "supports_pdf_input": false, "supports_tool_choice": true }, "azure_ai/kimi-k2.5": { @@ -13459,6 +13514,7 @@ "output_cost_per_token": 3e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true, "supports_video_input": true, "supports_vision": true, @@ -13482,6 +13538,7 @@ "text" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_reasoning": true, "supports_tool_choice": true, "supports_vision": true, @@ -13498,6 +13555,7 @@ "output_cost_per_token": 4e-08, "source": "https://marketplace.microsoft.com/en/marketplace/apps/000-000.ministral-3b-2410-offer?tab=Overview", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true }, "azure_ai/mistral-large": { @@ -13510,6 +13568,7 @@ "output_cost_per_token": 1.2e-05, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true }, "azure_ai/mistral-large-2407": { @@ -13522,6 +13581,7 @@ "output_cost_per_token": 6e-06, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true }, "azure_ai/mistral-large-latest": { @@ -13533,6 +13593,7 @@ "output_cost_per_token": 6e-06, "source": "https://marketplace.microsoft.com/en/marketplace/apps/000-000.mistral-ai-large-2407-offer?tab=Overview", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true }, "azure_ai/mistral-large-3": { @@ -13544,6 +13605,7 @@ "output_cost_per_token": 1.5e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": true }, @@ -13557,6 +13619,7 @@ "output_cost_per_token": 2e-06, "supports_assistant_prefill": true, "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true }, "azure_ai/mistral-nemo": { @@ -13568,7 +13631,8 @@ "mode": "chat", "output_cost_per_token": 1.5e-07, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", - "supports_function_calling": true + "supports_function_calling": true, + "supports_pdf_input": false }, "azure_ai/mistral-small": { "deprecation_date": "2025-07-31", @@ -13580,6 +13644,7 @@ "output_cost_per_token": 3e-06, "source": "https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/retired-models", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true }, "azure_ai/mistral-small-2503": { @@ -13591,6 +13656,7 @@ "mode": "chat", "output_cost_per_token": 3e-07, "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true, "supports_vision": true }, @@ -52006,6 +52072,7 @@ ], "supports_assistant_prefill": false, "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -52026,6 +52093,7 @@ ], "supports_assistant_prefill": false, "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -52777,6 +52845,7 @@ "output_cost_per_token": 2.5e-06, "source": "https://cloud.google.com/gemini-enterprise-agent-platform/generative-ai/pricing", "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true, "supports_web_search": true }, @@ -52794,6 +52863,7 @@ "global" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_reasoning": true, "supports_tool_choice": true }, @@ -52811,6 +52881,7 @@ "global" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -52989,6 +53060,7 @@ "output_cost_per_token": 3.6e-07, "output_cost_per_token_batches": 1.8e-07, "source": "https://cloud.google.com/gemini-enterprise-agent-platform/generative-ai/pricing", + "supports_pdf_input": false, "supports_reasoning": true }, "vertex_ai/openai/gpt-oss-20b-maas": { @@ -53000,6 +53072,7 @@ "mode": "chat", "output_cost_per_token": 2.5e-07, "source": "https://cloud.google.com/gemini-enterprise-agent-platform/generative-ai/pricing", + "supports_pdf_input": false, "supports_reasoning": true, "cache_read_input_token_cost": 7e-09, "input_cost_per_token_batches": 3.5e-08, @@ -53131,6 +53204,7 @@ "us-south1" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true }, "vertex_ai/qwen/qwen3-coder-480b-a35b-instruct-maas": { @@ -53149,6 +53223,7 @@ "global" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true }, "vertex_ai/qwen/qwen3-next-80b-a3b-instruct-maas": { @@ -53164,6 +53239,7 @@ "global" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true }, "vertex_ai/qwen/qwen3-next-80b-a3b-thinking-maas": { @@ -53179,6 +53255,7 @@ "global" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_tool_choice": true }, "vertex_ai/veo-3.1-generate-preview": { @@ -61177,6 +61254,126 @@ ], "supports_audio_input": true }, + "soniox/stt-rt-v5": { + "input_cost_per_second": 3.333e-05, + "litellm_provider": "soniox", + "metadata": { + "calculation": "$0.12/3600 seconds = $0.00003333 per second", + "original_pricing_per_hour": 0.12 + }, + "mode": "audio_transcription", + "source": "https://soniox.com/pricing", + "supported_endpoints": [ + "/v1/realtime" + ], + "supported_modalities": [ + "audio" + ], + "supported_output_modalities": [ + "text" + ], + "supports_audio_input": true + }, + "soniox/stt-rt-preview": { + "input_cost_per_second": 3.333e-05, + "litellm_provider": "soniox", + "metadata": { + "calculation": "$0.12/3600 seconds = $0.00003333 per second", + "original_pricing_per_hour": 0.12 + }, + "mode": "audio_transcription", + "source": "https://soniox.com/pricing", + "supported_endpoints": [ + "/v1/realtime" + ], + "supported_modalities": [ + "audio" + ], + "supported_output_modalities": [ + "text" + ], + "supports_audio_input": true + }, + "soniox/stt-rt-preview-v2": { + "input_cost_per_second": 3.333e-05, + "litellm_provider": "soniox", + "metadata": { + "calculation": "$0.12/3600 seconds = $0.00003333 per second", + "original_pricing_per_hour": 0.12 + }, + "mode": "audio_transcription", + "source": "https://soniox.com/pricing", + "supported_endpoints": [ + "/v1/realtime" + ], + "supported_modalities": [ + "audio" + ], + "supported_output_modalities": [ + "text" + ], + "supports_audio_input": true + }, + "soniox/stt-rt-v3": { + "input_cost_per_second": 3.333e-05, + "litellm_provider": "soniox", + "metadata": { + "calculation": "$0.12/3600 seconds = $0.00003333 per second", + "original_pricing_per_hour": 0.12 + }, + "mode": "audio_transcription", + "source": "https://soniox.com/pricing", + "supported_endpoints": [ + "/v1/realtime" + ], + "supported_modalities": [ + "audio" + ], + "supported_output_modalities": [ + "text" + ], + "supports_audio_input": true + }, + "soniox/stt-rt-v3-preview": { + "input_cost_per_second": 3.333e-05, + "litellm_provider": "soniox", + "metadata": { + "calculation": "$0.12/3600 seconds = $0.00003333 per second", + "original_pricing_per_hour": 0.12 + }, + "mode": "audio_transcription", + "source": "https://soniox.com/pricing", + "supported_endpoints": [ + "/v1/realtime" + ], + "supported_modalities": [ + "audio" + ], + "supported_output_modalities": [ + "text" + ], + "supports_audio_input": true + }, + "soniox/stt-rt-v4": { + "input_cost_per_second": 3.333e-05, + "litellm_provider": "soniox", + "metadata": { + "calculation": "$0.12/3600 seconds = $0.00003333 per second", + "original_pricing_per_hour": 0.12 + }, + "mode": "audio_transcription", + "source": "https://soniox.com/pricing", + "supported_endpoints": [ + "/v1/realtime" + ], + "supported_modalities": [ + "audio" + ], + "supported_output_modalities": [ + "text" + ], + "supports_audio_input": true + }, "tensormesh/Qwen/Qwen3.5-397B-A17B-FP8": { "litellm_provider": "tensormesh", "mode": "chat", @@ -67202,6 +67399,7 @@ "text" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true, @@ -67216,6 +67414,7 @@ "mode": "chat", "output_cost_per_token": 1.2e-06, "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true, @@ -67258,6 +67457,7 @@ "output_cost_per_token": 1.5e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -67272,6 +67472,7 @@ "output_cost_per_token": 3.1e-07, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -67286,6 +67487,7 @@ "output_cost_per_token": 5.5e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -67300,6 +67502,7 @@ "output_cost_per_token": 6.25e-07, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -67316,6 +67519,7 @@ "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, "supports_parallel_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_response_schema": true, @@ -67331,6 +67535,7 @@ "output_cost_per_token": 3.2e-06, "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", "supports_function_calling": true, + "supports_pdf_input": false, "supports_reasoning": true, "supports_tool_choice": true }, @@ -67349,6 +67554,7 @@ "supported_output_modalities": [ "text" ], + "supports_pdf_input": false, "supports_response_schema": true, "supports_vision": true }, @@ -67358,7 +67564,8 @@ "output_cost_per_token": 5.4e-06, "litellm_provider": "azure_ai", "mode": "chat", - "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'" + "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", + "supports_pdf_input": false }, "azure_ai/deepseek-v3-0324": { "deprecation_date": "2026-07-13", @@ -67366,7 +67573,8 @@ "output_cost_per_token": 4.56e-06, "litellm_provider": "azure_ai", "mode": "chat", - "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'" + "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", + "supports_pdf_input": false }, "azure_ai/deepseek-v3.1": { "deprecation_date": "2026-07-13", @@ -67374,7 +67582,8 @@ "output_cost_per_token": 4.94e-06, "litellm_provider": "azure_ai", "mode": "chat", - "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'" + "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", + "supports_pdf_input": false }, "azure_ai/grok-3": { "deprecation_date": "2026-05-01", @@ -67382,7 +67591,8 @@ "output_cost_per_token": 1.5e-05, "litellm_provider": "azure_ai", "mode": "chat", - "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'" + "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", + "supports_pdf_input": false }, "azure_ai/grok-3-mini": { "deprecation_date": "2026-05-01", @@ -67390,7 +67600,8 @@ "output_cost_per_token": 1.27e-06, "litellm_provider": "azure_ai", "mode": "chat", - "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'" + "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", + "supports_pdf_input": false }, "azure_ai/grok-4-fast-non-reasoning": { "deprecation_date": "2026-05-01", @@ -67398,7 +67609,8 @@ "output_cost_per_token": 5e-07, "litellm_provider": "azure_ai", "mode": "chat", - "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'" + "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", + "supports_pdf_input": false }, "azure_ai/grok-4-fast-reasoning": { "deprecation_date": "2026-05-01", @@ -67406,7 +67618,8 @@ "output_cost_per_token": 5e-07, "litellm_provider": "azure_ai", "mode": "chat", - "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'" + "source": "https://prices.azure.com/api/retail/prices?$filter=serviceName%20eq%20'Foundry%20Models'%20and%20armRegionName%20eq%20'eastus'%20and%20priceType%20eq%20'Consumption'", + "supports_pdf_input": false }, "bedrock/us-gov-west-1/nvidia.nemotron-nano-3-30b": { "input_cost_per_token": 7.2e-08, @@ -73076,6 +73289,7 @@ "source": "https://cloud.google.com/gemini-enterprise-agent-platform/generative-ai/pricing", "supported_regions": ["global"], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_response_schema": true, @@ -73691,6 +73905,7 @@ "output_cost_per_token": 3.5e-06, "source": "https://azure.microsoft.com/en-us/pricing/details/ai-foundry-models/microsoft/", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_tool_choice": true @@ -74745,6 +74960,7 @@ "text" ], "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": false, "supports_reasoning": true, "supports_tool_choice": true, @@ -74993,6 +75209,7 @@ "output_cost_per_token_above_200k_tokens": 1.2e-05, "source": "https://docs.x.ai/developers/models/grok-4.7", "supports_function_calling": true, + "supports_pdf_input": false, "supports_prompt_caching": true, "supports_reasoning": true, "supports_response_schema": true, @@ -78341,6 +78558,1254 @@ "supports_xhigh_reasoning_effort": true, "use_openai_responses_path": true }, + "kyma/claude-fable-5": { + "max_input_tokens": 1000000, + "max_output_tokens": 128000, + "input_cost_per_token": 1.35e-05, + "output_cost_per_token": 6.75e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true, + "supports_adaptive_thinking": true, + "thinking_always_on": true + }, + "kyma/claude-fable-5.1": { + "max_input_tokens": 1000000, + "max_output_tokens": 128000, + "input_cost_per_token": 1.35e-05, + "output_cost_per_token": 6.75e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true, + "supports_adaptive_thinking": true, + "thinking_always_on": true + }, + "kyma/claude-haiku-4-5": { + "max_input_tokens": 200000, + "max_output_tokens": 64000, + "input_cost_per_token": 1.35e-06, + "cache_read_input_token_cost": 1.35e-07, + "output_cost_per_token": 6.75e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false, + "prompt_cache_min_tokens": 4096 + }, + "kyma/claude-haiku-5.5": { + "max_input_tokens": 1000000, + "max_output_tokens": 128000, + "input_cost_per_token": 1.35e-07, + "cache_read_input_token_cost": 1.35e-08, + "output_cost_per_token": 6.75e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true, + "supports_adaptive_thinking": true, + "thinking_always_on": false, + "prompt_cache_min_tokens": 512 + }, + "kyma/claude-opus-4-7": { + "max_input_tokens": 1000000, + "max_output_tokens": 128000, + "input_cost_per_token": 6.75e-06, + "cache_read_input_token_cost": 6.75e-07, + "output_cost_per_token": 3.375e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false, + "supports_adaptive_thinking": true, + "prompt_cache_min_tokens": 2048 + }, + "kyma/claude-opus-5": { + "max_input_tokens": 1000000, + "max_output_tokens": 128000, + "input_cost_per_token": 6.75e-06, + "cache_read_input_token_cost": 6.75e-07, + "output_cost_per_token": 3.375e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true, + "supports_adaptive_thinking": true, + "prompt_cache_min_tokens": 512 + }, + "kyma/claude-opus-5.5": { + "max_input_tokens": 1000000, + "max_output_tokens": 128000, + "input_cost_per_token": 5.4e-06, + "cache_read_input_token_cost": 2.7e-07, + "output_cost_per_token": 2.7e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true, + "supports_adaptive_thinking": true, + "thinking_always_on": true, + "prompt_cache_min_tokens": 512 + }, + "kyma/claude-sonnet-4-6": { + "max_input_tokens": 1000000, + "max_output_tokens": 128000, + "input_cost_per_token": 4.05e-06, + "cache_read_input_token_cost": 4.05e-07, + "output_cost_per_token": 2.025e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false, + "supports_adaptive_thinking": true, + "prompt_cache_min_tokens": 1024 + }, + "kyma/claude-sonnet-5": { + "max_input_tokens": 1000000, + "max_output_tokens": 128000, + "input_cost_per_token": 2.7e-06, + "cache_read_input_token_cost": 2.7e-07, + "output_cost_per_token": 1.35e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true, + "supports_adaptive_thinking": true, + "prompt_cache_min_tokens": 1024 + }, + "kyma/claude-sonnet-5.5": { + "max_input_tokens": 1000000, + "max_output_tokens": 128000, + "input_cost_per_token": 2.7e-06, + "cache_read_input_token_cost": 1.35e-07, + "output_cost_per_token": 1.35e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true, + "supports_adaptive_thinking": true, + "thinking_always_on": true, + "prompt_cache_min_tokens": 512 + }, + "kyma/deepseek-r1": { + "max_input_tokens": 64000, + "max_output_tokens": 32768, + "input_cost_per_token": 7.425e-07, + "output_cost_per_token": 2.957e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/deepseek-v3": { + "max_input_tokens": 160000, + "max_output_tokens": 8192, + "input_cost_per_token": 5.18e-07, + "output_cost_per_token": 7.571e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/deepseek-v4-flash": { + "max_input_tokens": 1000000, + "max_output_tokens": 65536, + "input_cost_per_token": 1.23e-07, + "cache_read_input_token_cost": 2.43e-08, + "output_cost_per_token": 2.458e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/deepseek-v4-flash-vision-exp": { + "max_input_tokens": 1048576, + "max_output_tokens": 384000, + "input_cost_per_token": 3.892e-07, + "output_cost_per_token": 1.168e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/deepseek-v4.1-flash": { + "max_input_tokens": 1048576, + "max_output_tokens": 131072, + "input_cost_per_token": 2.7e-07, + "output_cost_per_token": 8.1e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/gemini-3-flash": { + "max_input_tokens": 1048576, + "max_output_tokens": 8192, + "input_cost_per_token": 6.75e-07, + "cache_read_input_token_cost": 6.75e-08, + "output_cost_per_token": 4.05e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/gemini-3.1-pro": { + "max_input_tokens": 1048576, + "max_output_tokens": 65536, + "input_cost_per_token": 2.7e-06, + "cache_read_input_token_cost": 2.7e-07, + "output_cost_per_token": 1.62e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/gemini-3.5-flash": { + "max_input_tokens": 1048576, + "max_output_tokens": 8192, + "input_cost_per_token": 2.025e-06, + "cache_read_input_token_cost": 2.025e-07, + "output_cost_per_token": 1.215e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/gemini-3.5-flash-lite": { + "max_input_tokens": 1048576, + "max_output_tokens": 8192, + "input_cost_per_token": 4.05e-07, + "cache_read_input_token_cost": 4.05e-08, + "output_cost_per_token": 3.375e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/gemini-3.6-flash": { + "max_input_tokens": 1048576, + "max_output_tokens": 8192, + "input_cost_per_token": 1.013e-06, + "cache_read_input_token_cost": 1.0125e-07, + "output_cost_per_token": 5.063e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": false, + "supports_pdf_input": false + }, + "kyma/gemini-3.7-flash": { + "max_input_tokens": 1048576, + "max_output_tokens": 65536, + "input_cost_per_token": 2.026e-06, + "cache_read_input_token_cost": 1.0125e-07, + "output_cost_per_token": 1.0126e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/gemini-3.8-flash": { + "max_input_tokens": 1048576, + "max_output_tokens": 65536, + "input_cost_per_token": 2.026e-06, + "output_cost_per_token": 1.0126e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/gemini-4-argon": { + "max_input_tokens": 1048576, + "max_output_tokens": 65536, + "input_cost_per_token": 5.4e-06, + "cache_read_input_token_cost": 2.7e-07, + "output_cost_per_token": 2.7e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/gemma-4-31b": { + "max_input_tokens": 128000, + "max_output_tokens": 8192, + "input_cost_per_token": 1.642e-07, + "cache_read_input_token_cost": 1.642e-07, + "output_cost_per_token": 4.689e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/glm-4.5-air": { + "max_input_tokens": 131072, + "max_output_tokens": 8192, + "input_cost_per_token": 1.797e-07, + "cache_read_input_token_cost": 3.375e-08, + "output_cost_per_token": 1.175e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/glm-4.7-flash": { + "max_input_tokens": 203000, + "max_output_tokens": 65536, + "input_cost_per_token": 8.1e-08, + "cache_read_input_token_cost": 1.35e-08, + "output_cost_per_token": 5.4e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/glm-5.1": { + "max_input_tokens": 203000, + "max_output_tokens": 65536, + "input_cost_per_token": 1.89e-06, + "cache_read_input_token_cost": 2.7675e-07, + "output_cost_per_token": 5.94e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/glm-5.2": { + "max_input_tokens": 1000000, + "max_output_tokens": 131072, + "input_cost_per_token": 9.029e-07, + "cache_read_input_token_cost": 2.9835e-07, + "output_cost_per_token": 3.083e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/glm-5.3": { + "max_input_tokens": 1310720, + "max_output_tokens": 131072, + "input_cost_per_token": 1.178e-06, + "output_cost_per_token": 3.702e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/glm-5.3-flash": { + "max_input_tokens": 1310720, + "max_output_tokens": 65536, + "input_cost_per_token": 1.72e-07, + "output_cost_per_token": 5.733e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/gpt-5.6-luna": { + "max_input_tokens": 1050000, + "max_output_tokens": 128000, + "input_cost_per_token": 2.944e-07, + "cache_read_input_token_cost": 2.7e-08, + "output_cost_per_token": 1.767e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/gpt-5.6-luna-pro": { + "max_input_tokens": 1050000, + "max_output_tokens": 128000, + "input_cost_per_token": 2.703e-07, + "cache_read_input_token_cost": 2.7e-08, + "output_cost_per_token": 1.621e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/gpt-5.6-sol": { + "max_input_tokens": 1050000, + "max_output_tokens": 128000, + "input_cost_per_token": 5.4e-06, + "cache_read_input_token_cost": 6.75e-07, + "output_cost_per_token": 2.7e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/gpt-5.6-sol-pro": { + "max_input_tokens": 1050000, + "max_output_tokens": 128000, + "input_cost_per_token": 5.4e-06, + "cache_read_input_token_cost": 6.75e-07, + "output_cost_per_token": 2.7e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/gpt-5.6-terra": { + "max_input_tokens": 1050000, + "max_output_tokens": 8192, + "input_cost_per_token": 2.7e-06, + "cache_read_input_token_cost": 2.7e-07, + "output_cost_per_token": 1.62e-05, + "input_cost_per_token_above_272k_tokens": 5.4e-06, + "output_cost_per_token_above_272k_tokens": 2.43e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/gpt-5.6-terra-pro": { + "max_input_tokens": 1050000, + "max_output_tokens": 128000, + "input_cost_per_token": 2.7e-06, + "cache_read_input_token_cost": 2.7e-07, + "output_cost_per_token": 1.62e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/gpt-6-astra": { + "max_input_tokens": 1050000, + "max_output_tokens": 128000, + "input_cost_per_token": 1.35e-05, + "cache_read_input_token_cost": 1.35e-06, + "output_cost_per_token": 6.75e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/gpt-6-luna": { + "max_input_tokens": 1050000, + "max_output_tokens": 128000, + "input_cost_per_token": 1.639e-07, + "cache_read_input_token_cost": 1.35e-08, + "output_cost_per_token": 8.195e-07, + "input_cost_per_token_above_272k_tokens": 2.7e-07, + "output_cost_per_token_above_272k_tokens": 1.0125e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/gpt-6.1-sol": { + "max_input_tokens": 1050000, + "max_output_tokens": 128000, + "input_cost_per_token": 2.7e-06, + "cache_read_input_token_cost": 1.35e-07, + "output_cost_per_token": 1.35e-05, + "input_cost_per_token_above_272k_tokens": 5.4e-06, + "output_cost_per_token_above_272k_tokens": 2.025e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/gpt-oss-120b": { + "max_input_tokens": 128000, + "max_output_tokens": 8192, + "input_cost_per_token": 4.73e-08, + "output_cost_per_token": 2.303e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/grok-4.20": { + "max_input_tokens": 2000000, + "max_output_tokens": 8192, + "input_cost_per_token": 1.763e-06, + "output_cost_per_token": 3.528e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/grok-4.20-multi-agent": { + "max_input_tokens": 2000000, + "max_output_tokens": 8192, + "input_cost_per_token": 1.767e-06, + "output_cost_per_token": 3.534e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": false, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/grok-4.3": { + "max_input_tokens": 1000000, + "max_output_tokens": 32768, + "input_cost_per_token": 1.92e-06, + "cache_read_input_token_cost": 2.7e-07, + "output_cost_per_token": 3.838e-06, + "input_cost_per_token_above_200k_tokens": 3.375e-06, + "output_cost_per_token_above_200k_tokens": 6.75e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/grok-4.5": { + "max_input_tokens": 500000, + "max_output_tokens": 8192, + "input_cost_per_token": 2.73e-06, + "output_cost_per_token": 8.189e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/grok-4.6": { + "max_input_tokens": 500000, + "max_output_tokens": 8192, + "input_cost_per_token": 2.758e-06, + "output_cost_per_token": 8.274e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/grok-4.7": { + "max_input_tokens": 500000, + "max_output_tokens": 450000, + "input_cost_per_token": 2.16e-06, + "cache_read_input_token_cost": 5.4e-07, + "output_cost_per_token": 6.48e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/grok-build": { + "max_input_tokens": 256000, + "max_output_tokens": 32768, + "input_cost_per_token": 1.389e-06, + "cache_read_input_token_cost": 2.7e-07, + "output_cost_per_token": 2.777e-06, + "input_cost_per_token_above_200k_tokens": 2.7e-06, + "output_cost_per_token_above_200k_tokens": 5.4e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/hy3": { + "max_input_tokens": 262144, + "max_output_tokens": 8192, + "input_cost_per_token": 1.594e-07, + "output_cost_per_token": 6.576e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/hy4-preview": { + "max_input_tokens": 1048576, + "max_output_tokens": 64000, + "input_cost_per_token": 1.1259e-06, + "output_cost_per_token": 3.3764e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/kimi-k2.6": { + "max_input_tokens": 262144, + "max_output_tokens": 32768, + "input_cost_per_token": 5.909e-07, + "output_cost_per_token": 2.758e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/kimi-k2.7-code": { + "max_input_tokens": 262144, + "max_output_tokens": 16384, + "input_cost_per_token": 1.009e-06, + "output_cost_per_token": 4.774e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/kimi-k3": { + "max_input_tokens": 1048576, + "max_output_tokens": 32768, + "input_cost_per_token": 3.052e-06, + "cache_read_input_token_cost": 4.05e-07, + "output_cost_per_token": 1.5258e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/kimi-k3.1-pro": { + "max_input_tokens": 1048576, + "max_output_tokens": 943718, + "input_cost_per_token": 2.7e-06, + "cache_read_input_token_cost": 2.7e-07, + "output_cost_per_token": 1.35e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/llama-3.3-70b": { + "max_input_tokens": 128000, + "max_output_tokens": 8192, + "input_cost_per_token": 1.35e-07, + "output_cost_per_token": 4.32e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": false, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/llama-4-maverick": { + "max_input_tokens": 1048576, + "max_output_tokens": 16384, + "input_cost_per_token": 3.344e-07, + "output_cost_per_token": 1.337e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": false, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/mimo-v2.5": { + "max_input_tokens": 1050000, + "max_output_tokens": 16384, + "input_cost_per_token": 1.859e-07, + "output_cost_per_token": 3.718e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/mimo-v2.6-flash": { + "max_input_tokens": 1050000, + "max_output_tokens": 131072, + "input_cost_per_token": 1.89e-07, + "output_cost_per_token": 3.78e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/mimo-v2.6-pro": { + "max_input_tokens": 1050000, + "max_output_tokens": 131072, + "input_cost_per_token": 5.805e-07, + "output_cost_per_token": 1.1745e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/minimax-m2.5": { + "max_input_tokens": 196608, + "max_output_tokens": 32768, + "input_cost_per_token": 4.188e-07, + "output_cost_per_token": 1.675e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/minimax-m2.7": { + "max_input_tokens": 204800, + "max_output_tokens": 32768, + "input_cost_per_token": 4.05e-07, + "output_cost_per_token": 1.62e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/minimax-m3": { + "max_input_tokens": 1048576, + "max_output_tokens": 32768, + "input_cost_per_token": 3.956e-07, + "cache_read_input_token_cost": 7.56e-08, + "output_cost_per_token": 1.582e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/minimax-m3.1-flash": { + "max_input_tokens": 1048576, + "max_output_tokens": 524288, + "input_cost_per_token": 2.025e-07, + "cache_read_input_token_cost": 2.025e-08, + "output_cost_per_token": 8.1e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/muse-glimmer-30b": { + "max_input_tokens": 131072, + "max_output_tokens": 8192, + "input_cost_per_token": 4.554e-07, + "output_cost_per_token": 1.821e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/muse-spark-1.1": { + "max_input_tokens": 1048576, + "max_output_tokens": 8192, + "input_cost_per_token": 1.542e-06, + "output_cost_per_token": 5.243e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/muse-spark-1.2": { + "max_input_tokens": 1048576, + "max_output_tokens": 8192, + "input_cost_per_token": 1.773e-06, + "output_cost_per_token": 6.025e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/muse-spark-1.3": { + "max_input_tokens": 1048576, + "max_output_tokens": 8192, + "input_cost_per_token": 1.688e-06, + "output_cost_per_token": 5.738e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/muse-spark-open": { + "max_input_tokens": 1048576, + "max_output_tokens": 943718, + "input_cost_per_token": 1.688e-06, + "output_cost_per_token": 5.738e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/muse-wonder": { + "max_input_tokens": 1048576, + "max_output_tokens": 943718, + "input_cost_per_token": 5.4e-06, + "output_cost_per_token": 2.7e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": true + }, + "kyma/nemotron-3-ultra-550b": { + "max_input_tokens": 1000000, + "max_output_tokens": 32768, + "input_cost_per_token": 6.75e-07, + "cache_read_input_token_cost": 1.35e-07, + "output_cost_per_token": 3.375e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/nemotron-3.5-super-vl": { + "max_input_tokens": 1048576, + "max_output_tokens": 235929, + "input_cost_per_token": 8.1e-08, + "cache_read_input_token_cost": 1.35e-08, + "output_cost_per_token": 4.725e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/qwen-3-32b": { + "max_input_tokens": 32768, + "max_output_tokens": 8192, + "input_cost_per_token": 1.08e-07, + "output_cost_per_token": 3.78e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/qwen-3-coder": { + "max_input_tokens": 131072, + "max_output_tokens": 32768, + "input_cost_per_token": 3.546e-07, + "cache_read_input_token_cost": 1.35e-07, + "output_cost_per_token": 1.612e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": false, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/qwen-3.6-plus": { + "max_input_tokens": 1000000, + "max_output_tokens": 65536, + "input_cost_per_token": 4.388e-07, + "output_cost_per_token": 2.633e-06, + "input_cost_per_token_above_256k_tokens": 1.755e-06, + "output_cost_per_token_above_256k_tokens": 5.265e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/qwen-3.7-max": { + "max_input_tokens": 1000000, + "max_output_tokens": 32768, + "input_cost_per_token": 2.071e-06, + "cache_read_input_token_cost": 1.6875e-07, + "output_cost_per_token": 6.207e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": false, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/qwen-3.7-plus": { + "max_input_tokens": 1000000, + "max_output_tokens": 32768, + "input_cost_per_token": 4.325e-07, + "cache_read_input_token_cost": 8.64e-08, + "output_cost_per_token": 1.731e-06, + "input_cost_per_token_above_256k_tokens": 1.296e-06, + "output_cost_per_token_above_256k_tokens": 5.184e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/qwen-3.8-max": { + "max_input_tokens": 1000000, + "max_output_tokens": 131072, + "input_cost_per_token": 2.228e-06, + "cache_read_input_token_cost": 2.781e-07, + "output_cost_per_token": 6.684e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/qwen3.7-flash": { + "max_input_tokens": 1000000, + "max_output_tokens": 8192, + "input_cost_per_token": 4.35e-08, + "cache_read_input_token_cost": 8.1e-09, + "output_cost_per_token": 1.886e-07, + "input_cost_per_token_above_32k_tokens": 1.35e-07, + "output_cost_per_token_above_32k_tokens": 5.4e-07, + "input_cost_per_token_above_256k_tokens": 2.7e-07, + "output_cost_per_token_above_256k_tokens": 1.08e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/qwen3.8-27b": { + "max_input_tokens": 1000000, + "max_output_tokens": 131072, + "input_cost_per_token": 3.826e-07, + "output_cost_per_token": 2.732e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": false, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/qwen3.8-flash": { + "max_input_tokens": 1000000, + "max_output_tokens": 131072, + "input_cost_per_token": 2.045e-07, + "output_cost_per_token": 6.41e-07, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/sonar": { + "max_input_tokens": 127072, + "max_output_tokens": 4096, + "input_cost_per_token": 1.35e-06, + "output_cost_per_token": 1.35e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": false, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": false, + "supports_response_schema": false, + "supports_pdf_input": false + }, + "kyma/sonar-pro": { + "max_input_tokens": 200000, + "max_output_tokens": 8000, + "input_cost_per_token": 4.05e-06, + "output_cost_per_token": 2.025e-05, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": false, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": false, + "supports_response_schema": false, + "supports_pdf_input": false + }, + "kyma/step-3.7-flash": { + "max_input_tokens": 256000, + "max_output_tokens": 8192, + "input_cost_per_token": 2.229e-07, + "cache_read_input_token_cost": 5.4e-08, + "output_cost_per_token": 1.282e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, + "kyma/step-5-preview": { + "max_input_tokens": 1048576, + "max_output_tokens": 64000, + "input_cost_per_token": 1.35e-06, + "cache_read_input_token_cost": 1.35e-07, + "output_cost_per_token": 3.645e-06, + "litellm_provider": "kyma", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "supports_prompt_caching": true, + "supports_reasoning": true, + "supports_response_schema": true, + "supports_pdf_input": false + }, "databricks/databricks-claude-haiku-5-5": { "cache_creation_input_token_cost": 1.2502e-07, "cache_creation_input_token_cost_above_100k_tokens": 6.2503e-07, diff --git a/packaging/litellm-core/pyproject.toml b/packaging/litellm-core/pyproject.toml index 27681d9116c..db50589a675 100644 --- a/packaging/litellm-core/pyproject.toml +++ b/packaging/litellm-core/pyproject.toml @@ -56,6 +56,7 @@ editable-profile = "dev" include = [ { path = "rust-toolchain.toml", format = "sdist" }, { path = ".cargo/config.toml", format = "sdist" }, + { path = "model_prices_and_context_window.json", format = "sdist" }, "litellm/router_strategy/complexity_router/artifacts/*.json", "litellm/router_strategy/complexity_router/fuse_presets.json", "litellm/proxy/model_insights_tasks.json", diff --git a/provider_endpoints_support.json b/provider_endpoints_support.json index 7b0234e28d7..1d487f33835 100644 --- a/provider_endpoints_support.json +++ b/provider_endpoints_support.json @@ -3724,7 +3724,7 @@ "ocr": false, "search": false, "moderations": false, - "realtime": false, + "realtime": true, "video_generations": false, "batches": false, "messages": false, @@ -4910,6 +4910,33 @@ "vector_stores_search": true } }, + "opensearch": { + "display_name": "Amazon OpenSearch (`opensearch`)", + "url": "https://docs.litellm.ai/docs/providers/opensearch_vector_stores", + "endpoints": { + "chat_completions": false, + "responses": false, + "text_completion": false, + "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": false, + "interactions": false, + "systemone": false, + "a2a": false, + "generateContent": false, + "vector_stores_search": true + } + }, "helicone": { "display_name": "Helicone (`helicone`)", "url": "https://docs.litellm.ai/docs/providers/helicone", @@ -5327,6 +5354,32 @@ "generateContent": false } }, + "kyma": { + "display_name": "Kyma API (`kyma`)", + "url": "https://docs.litellm.ai/docs/providers/kyma", + "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 + } + }, "bigmodel": { "display_name": "BigModel (Zhipu AI) (`bigmodel`)", "url": "https://docs.bigmodel.cn/cn/guide/develop/responses/introduction", diff --git a/pyproject.toml b/pyproject.toml index 200bb463313..e6d34a3d90c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -335,6 +335,7 @@ include = [ "litellm/proxy/public_endpoints/whats_new.json", "litellm/proxy/common_utils/codex_base_instructions.md", "litellm/proxy/common_utils/codex_bundled_models_0.159.3.json", + { path = "model_prices_and_context_window.json", format = "sdist" }, ] exclude = [ "litellm/proxy/enterprise", @@ -357,6 +358,7 @@ cache-keys = [ { file = "litellm-rust/Cargo.lock" }, { file = "litellm-rust/Cargo.toml" }, { file = "litellm-rust/crates/**/*" }, + { file = "model_prices_and_context_window.json" }, ] constraint-dependencies = [ "tornado>=6.5.8", diff --git a/scripts/build_core_distribution.py b/scripts/build_core_distribution.py index 36ec20a9031..41d7208d2c3 100644 --- a/scripts/build_core_distribution.py +++ b/scripts/build_core_distribution.py @@ -9,7 +9,15 @@ from pathlib import Path from typing import Final ROOT: Final = Path(__file__).resolve().parents[1] -SOURCES: Final = ("litellm", "litellm-rust", ".cargo", "rust-toolchain.toml", "README.md", "LICENSE") +SOURCES: Final = ( + "litellm", + "litellm-rust", + ".cargo", + "rust-toolchain.toml", + "model_prices_and_context_window.json", + "README.md", + "LICENSE", +) def stage_core_distribution(source: Path, destination: Path) -> None: diff --git a/scripts/pre_commit_lint.sh b/scripts/pre_commit_lint.sh index 371884fb791..95b5942fa8b 100755 --- a/scripts/pre_commit_lint.sh +++ b/scripts/pre_commit_lint.sh @@ -18,7 +18,8 @@ # -> ruff over ruff-tests.toml + `make lint-test-quality` (test-linting.yml's # test-tree ruff and test-quality gate steps) # - dashboard -> prettier + eslint + lint budgets (test-linting.yml's ui job) -# - proxy/types -> regenerate the lazy OpenAPI snapshot and dashboard API types, fail on drift (test-linting.yml's ui-api-types job) +# - spec_pattern -> regenerate the lazy OpenAPI snapshot and dashboard API types, fail on drift +# (test-linting.yml's ui-api-types job) # # Each block is skipped when no matching files are in scope, so unrelated commits # stay fast. This is intentionally not auto-installed as a git hook (see @@ -98,7 +99,7 @@ existing_files() { litellm_py_pattern='^litellm/.*\.py$' e2e_py_pattern='^tests/e2e(_harness)?/.*\.py$' test_tree_pattern='^(tests/.*\.py|ruff-tests\.toml|scripts/(check_test_quality|test_quality_gate)\.py)$' -spec_pattern='^(litellm/(proxy|types)/.*|ui/litellm-dashboard/(scripts/gen-api-types\.mjs|package\.json|package-lock\.json|src/lib/http/schema\.d\.ts))$' +spec_pattern='^(litellm/.*|enterprise/.*|pyproject\.toml|uv\.lock|ui/litellm-dashboard/(scripts/gen-api-types\.mjs|package\.json|package-lock\.json|src/lib/http/schema\.d\.ts))$' ui_prettier_pattern='^ui/litellm-dashboard/.*\.(js|jsx|ts|tsx|mjs|cjs|json|css|scss|md|mdx|yml|yaml|html)$' ui_eslint_pattern='^ui/litellm-dashboard/.*\.(js|jsx|ts|tsx|mjs|cjs)$' @@ -110,9 +111,6 @@ e2e_py_files=$(scope_match "$e2e_py_pattern") test_tree_files=$(scope_match "$test_tree_pattern") # ruff format (and CI's format step) skip enterprise; the rest of make lint covers it. fmt_files=$(printf '%s\n' "$litellm_py_files" | grep -v '^litellm/enterprise/' | existing_files) -# test-linting.yml triggers on any file under litellm/proxy or litellm/types -# (Prisma schema and configs included, not just Python) plus the generator and its -# lockfiles, so match that whole trigger set rather than a Python subset. spec_files=$(scope_match "$spec_pattern") # CI's ui job runs prettier over a wider extension set than eslint; keep that # split so this flags exactly what the job would. @@ -337,7 +335,7 @@ summary_item "tests/e2e checks (basedpyright + raw HTTP client ban)" "$e2e_py_fi summary_item "test-tree lint (ruff-tests.toml + test-quality gate)" "$test_tree_files" \ "no tests/ Python files or test-tree lint inputs in scope" summary_item "dashboard lint (prettier + eslint + lint budgets)" "$ui_prettier_changed$ui_eslint_changed" "no dashboard files in scope" -summary_item "dashboard API-type sync (npm run gen:api)" "$spec_files" "no litellm/proxy, litellm/types, or generator files in scope" +summary_item "dashboard API-type sync (npm run gen:api)" "$spec_files" "no litellm/, enterprise/, dependency, or generator files in scope" if [ -z "$litellm_py_files$e2e_py_files$test_tree_files$ui_prettier_changed$ui_eslint_changed$spec_files" ]; then echo "check: NOTE - no gating lint check matches the files in scope, so nothing ran:" >&2 diff --git a/tests/base_sdk_tests/test_core_distribution.py b/tests/base_sdk_tests/test_core_distribution.py index ba68d08e4d1..c22fa4e54fc 100644 --- a/tests/base_sdk_tests/test_core_distribution.py +++ b/tests/base_sdk_tests/test_core_distribution.py @@ -132,7 +132,9 @@ def test_core_artifacts_exclude_local_configuration(distributions: tuple[Path, P assert not any(member.name.endswith(f"/{name}") for member in archive.getmembers()) -@pytest.mark.parametrize("relative_path", ["rust-toolchain.toml", ".cargo/config.toml"]) +@pytest.mark.parametrize( + "relative_path", ["rust-toolchain.toml", ".cargo/config.toml", "model_prices_and_context_window.json"] +) def test_core_sdist_preserves_native_build_configuration(distribution_directory: Path, relative_path: str) -> None: sdists: Final = tuple(distribution_directory.glob("litellm_core-*.tar.gz")) assert len(sdists) == 1 diff --git a/tests/code_coverage_tests/router_code_coverage.py b/tests/code_coverage_tests/router_code_coverage.py index c7781819ae4..7e05e98d917 100644 --- a/tests/code_coverage_tests/router_code_coverage.py +++ b/tests/code_coverage_tests/router_code_coverage.py @@ -186,6 +186,14 @@ ignored_function_names = [ ] +def public_name(function_name): + """ + The deprecated public name a ``_core_`` router member is reached through. Tests that call the + old name run the core member, since ``DeprecatedRouterMembers`` forwards it there. + """ + return function_name.removeprefix("_core_") + + def main(): router_file = [ "./litellm/router.py", @@ -205,12 +213,16 @@ def main(): router_functions.extend(get_functions_from_router(file)) print("router_functions: ", router_functions) called_functions_in_tests = get_all_functions_called_in_tests(tests_dir) - untested_functions = [fn for fn in router_functions if fn not in called_functions_in_tests] + untested_functions = [ + fn + for fn in router_functions + if fn not in called_functions_in_tests and public_name(fn) not in called_functions_in_tests + ] if untested_functions: all_untested_functions = [] for func in untested_functions: - if func not in ignored_function_names: + if func not in ignored_function_names and public_name(func) not in ignored_function_names: all_untested_functions.append(func) untested_perc = (len(all_untested_functions)) / len(router_functions) print("untested_perc: ", untested_perc) diff --git a/tests/code_coverage_tests/router_surface_check.py b/tests/code_coverage_tests/router_surface_check.py index 0b35bac5240..40046391a14 100644 --- a/tests/code_coverage_tests/router_surface_check.py +++ b/tests/code_coverage_tests/router_surface_check.py @@ -37,9 +37,11 @@ ALLOWLIST = os.path.join(os.path.dirname(__file__), "router_surface_allowlist.js ROUTER_FILES = { "litellm/router.py", + "litellm/router_api.py", + "litellm/router_deprecated.py", } -ROUTER_CLASS_FILES = ["litellm/router.py"] -ROUTER_CLASS_NAMES = {"Router"} +ROUTER_CLASS_FILES = ["litellm/router.py", "litellm/router_deprecated.py"] +ROUTER_CLASS_NAMES = {"Router", "DeprecatedRouterMembers"} SCAN_ROOTS = ["litellm", "enterprise"] SKIP_DIRS = { diff --git a/tests/e2e/coverage_registry/llm_nonconversational.yaml b/tests/e2e/coverage_registry/llm_nonconversational.yaml index 330ddf88fc5..367135eae26 100644 --- a/tests/e2e/coverage_registry/llm_nonconversational.yaml +++ b/tests/e2e/coverage_registry/llm_nonconversational.yaml @@ -2,6 +2,7 @@ - {id: llm.completions.openai.basic.nonstream.works, module: llm, tier: P0, subject_endpoint: completions, route: openai, capability: basic, streaming: nonstream, assertions: [works], source: "test_completions_endpoint_e2e.py", rationale: "Legacy text /completions endpoint, second-highest production request volume"} - {id: llm.embeddings.openai.basic.nonstream.works, module: llm, tier: P0, subject_endpoint: embeddings, route: openai, capability: basic, streaming: nonstream, assertions: [works], source: "test_embeddings_endpoint_e2e.py:23", rationale: "Core endpoint, live vector response"} - {id: llm.embeddings.openai.input_validation.nonstream.works, module: llm, tier: P1, subject_endpoint: embeddings, route: openai, capability: input_validation, streaming: nonstream, assertions: [works], source: "vendor strategy §9.3 / LIT-4778", rationale: "Missing model/input on /embeddings return client errors"} +- {id: llm.embeddings.openai.supported_endpoints_gate.nonstream.works, module: llm, tier: P1, subject_endpoint: embeddings, route: openai, capability: input_validation, streaming: nonstream, assertions: [works], fail_before_fix: proven, source: "test_embedding_only_supported_endpoints_e2e.py", rationale: "Explicit endpoint restrictions preserve real embeddings, refuse chat with a gateway 400, and route chat through a live fallback; zero-provider-call contracts live in integration/routing/test_non_chat_model_endpoint_gate.py"} - {id: llm.embeddings.openai.basic.nonstream.cost_logged, module: llm, tier: P0, subject_endpoint: embeddings, route: openai, capability: basic, streaming: nonstream, assertions: [cost_logged], source: "SPEND_TRACKING_COVERAGE_MATRIX.md:34", rationale: "Cost tracking on embeddings"} - {id: llm.embeddings.openai.passthrough.nonstream.cost_logged, module: llm, tier: P0, subject_endpoint: embeddings, route: openai, capability: basic, streaming: nonstream, assertions: [cost_logged], source: "test_passthrough_e2e.py", rationale: "POST /openai_passthrough/v1/embeddings is costed; the route wrote no spend row at all, so budgets never saw traffic OpenAI was billing for (GitHub issue #36646)"} - {id: llm.embeddings.azure_openai.basic.nonstream.works, module: llm, tier: P0, subject_endpoint: embeddings, route: azure_openai, capability: basic, streaming: nonstream, assertions: [works], source: "llms/azure/azure.py", rationale: "Azure embeddings via translation"} diff --git a/tests/e2e/llm_translation/test_embedding_only_supported_endpoints_e2e.py b/tests/e2e/llm_translation/test_embedding_only_supported_endpoints_e2e.py new file mode 100644 index 00000000000..c45adfeeb84 --- /dev/null +++ b/tests/e2e/llm_translation/test_embedding_only_supported_endpoints_e2e.py @@ -0,0 +1,119 @@ +"""Live provider checks for embeddings, the chat refusal, and chat fallback from an explicitly restricted model.""" + +from __future__ import annotations + +import dataclasses +from typing import Final + +import pytest +from e2e_config import unique_marker +from e2e_metadata import Domain, Mode, Provider, Route, Subject, meta +from lifecycle import ResourceManager +from models import LiteLLMParamsBody, ModelInfoBody, ModelMode, ModelNewBody +from openai import BadRequestError, OpenAI +from proxy_client import ProxyClient +from sdk_clients import NO_PROXY_CACHE, SdkClients + +pytestmark = pytest.mark.e2e + +EMBEDDING_BACKEND: Final = "openai/text-embedding-3-small" +CHAT_FALLBACK_BACKEND: Final = "openai/gpt-5.4-nano" +EMBEDDINGS_ONLY: Final = ("/v1/embeddings",) +PROMPT: Final = "Say this is a test!" + + +def _subject(route: Route) -> Subject: + return Subject( + domain=Domain.LLM_TRANSLATION, + route=route, + providers=(Provider.OPENAI,), + models=(EMBEDDING_BACKEND,), + mode=Mode.NONSTREAM, + ) + + +def _register( + proxy: ProxyClient, + resources: ResourceManager, + prefix: str, + supported_endpoints: tuple[str, ...] | None, + backend: str = EMBEDDING_BACKEND, + mode: ModelMode = "embedding", +) -> str: + model: Final = f"{prefix}-{unique_marker()}" + model_id: Final = proxy.register_model( + ModelNewBody( + model_name=model, + litellm_params=LiteLLMParamsBody(model=backend, api_key="os.environ/OPENAI_API_KEY"), + model_info=ModelInfoBody( + enforce_supported_endpoints=supported_endpoints is not None, + mode=mode, + supported_endpoints=None if supported_endpoints is None else list(supported_endpoints), + ), + ) + ) + resources.defer(lambda: proxy.delete_model(model_id)) + return model + + +def _openai_at(sdk: SdkClients, api_prefix: str, key: str) -> OpenAI: + return dataclasses.replace(sdk, base_url=f"{sdk.base_url}{api_prefix}").openai(key) + + +class TestEmbeddingOnlySupportedEndpoints: + @pytest.mark.covers("llm.embeddings.openai.supported_endpoints_gate.nonstream.works") + @meta(_subject(Route.EMBEDDINGS)) + @pytest.mark.parametrize( + ("prefix", "supported_endpoints"), + ( + pytest.param("e2e-embed-only", EMBEDDINGS_ONLY, id="embed-only"), + pytest.param("e2e-embed-control", None, id="control"), + ), + ) + def test_embeddings_return_a_vector( + self, + proxy: ProxyClient, + resources: ResourceManager, + sdk: SdkClients, + prefix: str, + supported_endpoints: tuple[str, ...] | None, + ) -> None: + model: Final = _register(proxy, resources, prefix, supported_endpoints) + embeddings: Final = sdk.openai(resources.key()).embeddings.create( + model=model, input=PROMPT, extra_body=NO_PROXY_CACHE + ) + assert embeddings.data, f"/embeddings returned no data for {model}: {embeddings!r}" + assert any(component != 0.0 for component in embeddings.data[0].embedding), ( + f"/embeddings returned an empty or all-zero vector for {model}" + ) + + @pytest.mark.covers("llm.embeddings.openai.supported_endpoints_gate.nonstream.works") + @meta(dataclasses.replace(_subject(Route.CHAT_COMPLETIONS), models=(EMBEDDING_BACKEND, CHAT_FALLBACK_BACKEND))) + def test_chat_completions_with_a_fallback_are_answered_by_the_fallback( + self, proxy: ProxyClient, resources: ResourceManager, sdk: SdkClients + ) -> None: + model: Final = _register(proxy, resources, "e2e-embed-only", EMBEDDINGS_ONLY) + fallback: Final = _register(proxy, resources, "e2e-chat-fallback", None, CHAT_FALLBACK_BACKEND, "chat") + completion: Final = _openai_at(sdk, "/v1", resources.key()).chat.completions.create( + model=model, + messages=[{"role": "user", "content": PROMPT}], + extra_body={**NO_PROXY_CACHE, "fallbacks": [fallback]}, + ) + assert completion.choices and completion.choices[0].message.content, ( + f"the fallback {fallback} should answer a chat call to {model}, got {completion!r}" + ) + + @pytest.mark.covers("llm.embeddings.openai.supported_endpoints_gate.nonstream.works") + @meta(_subject(Route.CHAT_COMPLETIONS)) + def test_chat_completions_without_a_fallback_are_refused_by_the_gateway( + self, proxy: ProxyClient, resources: ResourceManager, sdk: SdkClients + ) -> None: + model: Final = _register(proxy, resources, "e2e-embed-only", EMBEDDINGS_ONLY) + with pytest.raises(BadRequestError) as refused: + _ = _openai_at(sdk, "/v1", resources.key()).chat.completions.create( + model=model, messages=[{"role": "user", "content": PROMPT}], extra_body=NO_PROXY_CACHE + ) + assert ( + f"/v1/chat/completions is not supported by model={model}. Supported endpoints: /v1/embeddings" + in refused.value.message + ), f"chat on {model} should be refused by the gateway before any provider call, got {refused.value!r}" diff --git a/tests/e2e/models.py b/tests/e2e/models.py index afddc7e25d6..504adec5401 100644 --- a/tests/e2e/models.py +++ b/tests/e2e/models.py @@ -1416,7 +1416,7 @@ class LiteLLMParamsBody(BaseModel): order: int | None = None -ModelMode = Literal["chat", "batch", "realtime", "image_generation"] +ModelMode = Literal["chat", "embedding", "batch", "realtime", "image_generation"] class TokenCountModelBody(BaseModel, frozen=True): @@ -1425,6 +1425,7 @@ class TokenCountModelBody(BaseModel, frozen=True): class ModelInfoBody(BaseModel): + enforce_supported_endpoints: bool | None = None # id is left unset so the proxy assigns a unique model_id per deployment. # Pinning it to the model_name made re-registrations of a fixed-name model # (e.g. the batch suite's openai-batch) collide on the model_id unique diff --git a/tests/integration/_support/client.py b/tests/integration/_support/client.py index b8ea11f3d2f..a759126fcd4 100644 --- a/tests/integration/_support/client.py +++ b/tests/integration/_support/client.py @@ -280,12 +280,14 @@ class Scenario: self.gateway.rows('SELECT model_id FROM "LiteLLM_ProxyModelTable" WHERE model_id = %s', (identity,)) == [] ) - def model(self, *, model_info: Mapping[str, JsonValue] | None = None, **parameters: JsonValue) -> str: - name: Final = f"integration-{uuid.uuid4().hex}" + def model( + self, *, name: str | None = None, model_info: Mapping[str, JsonValue] | None = None, **parameters: JsonValue + ) -> str: + model_name: Final = name if name is not None else f"integration-{uuid.uuid4().hex}" created: Final = self.gateway.post( "/model/new", { - "model_name": name, + "model_name": model_name, "litellm_params": { "model": "openai/gpt-4o-mini", "api_key": "integration-provider-key", @@ -298,8 +300,8 @@ class Scenario: identity: Final = string_value(object_value(created["model_info"])["id"]) self.cleanups.callback(self.delete_model, identity) if self.gateway.workers > 1: - self._await_every_worker(name, identity) - return name + self._await_every_worker(model_name, identity) + return model_name def _await_every_worker(self, name: str, identity: str) -> None: from litellm.constants import PROXY_CONFIG_RELOAD_INTERVAL_SECONDS diff --git a/tests/integration/caching/response_cache_case.py b/tests/integration/caching/response_cache_case.py index 312f29faeb3..7c2d1e1d2c7 100644 --- a/tests/integration/caching/response_cache_case.py +++ b/tests/integration/caching/response_cache_case.py @@ -275,11 +275,11 @@ def prompt() -> str: return f"cache probe {uuid.uuid4().hex}" -def redis_store() -> Redis: +def redis_store() -> Redis[bytes]: return Redis(host=os.environ["REDIS_HOST"], port=int(os.environ["REDIS_PORT"])) -def entry_keys(store: Redis, needle: str) -> Iterator[str]: +def entry_keys(store: Redis[bytes], needle: str) -> Iterator[str]: for raw_key in store.scan_iter(count=1000): key: Final = raw_key.decode() if not CACHE_KEY.match(key) or store.type(key) != b"string": @@ -289,12 +289,12 @@ def entry_keys(store: Redis, needle: str) -> Iterator[str]: yield key -def entry_key(store: Redis, needle: str) -> str | None: +def entry_key(store: Redis[bytes], needle: str) -> str | None: """The response cache key (a bare sha256) whose stored value names ``needle``, or ``None``.""" return next(entry_keys(store, needle), None) -def await_entry(store: Redis, needle: str) -> str: +def await_entry(store: Redis[bytes], needle: str) -> str: key: Final = eventually(lambda: entry_key(store, needle), lambda found: found is not None) assert key is not None return key diff --git a/tests/integration/caching/test_response_cache_model_group_key.py b/tests/integration/caching/test_response_cache_model_group_key.py new file mode 100644 index 00000000000..59565b75d9a --- /dev/null +++ b/tests/integration/caching/test_response_cache_model_group_key.py @@ -0,0 +1,192 @@ +"""The response cache keys an answer by the router's model group on every endpoint the router serves. + +The router names the model group in ``metadata`` on ``/v1/chat/completions`` and in ``litellm_metadata`` on +``/v1/responses`` and ``/v1/messages``. Every cell gives each model group its own scripted upstream scenario on one +shared provider model, fills one group's entry, and reads which scenario the identical request reached next and which +answer the caller got: an answer served from the cache repeats the id of the request that filled it. +""" + +from __future__ import annotations + +import uuid +from collections.abc import Callable, Mapping +from dataclasses import dataclass +from pathlib import Path +from typing import Final, TypeAlias + +import pytest +import yaml +from anthropic import Anthropic +from openai import OpenAI +from pydantic import JsonValue + +from tests.integration._support.client import JSON_OBJECT, Gateway, Scenario, object_value +from tests.integration._support.process import OwnedState, graceful_stop_seconds, owned_proxy_process +from tests.integration._support.upstream import ScenarioHandle +from tests.integration.caching.response_cache_case import ( + CHAT, + CHAT_MODEL, + MESSAGE, + MESSAGES_MODEL, + PROVIDER_KEY, + RESPONSE, + Upstream, + await_entry, + json_response, + prompt, + redis_store, + scripted, +) + +Ask: TypeAlias = Callable[[Gateway, str, str, Mapping[str, JsonValue]], str] + + +def _root(gateway: Gateway) -> str: + return str(gateway.client.base_url).rstrip("/") + + +def _metadata(metadata: Mapping[str, JsonValue]) -> dict[str, JsonValue] | None: + return {"metadata": dict(metadata)} if metadata else None + + +def _chat(gateway: Gateway, model: str, text: str, metadata: Mapping[str, JsonValue]) -> str: + with OpenAI(base_url=f"{_root(gateway)}/v1", api_key=gateway.key, max_retries=0) as client: + return client.chat.completions.create( + model=model, messages=[{"role": "user", "content": text}], extra_body=_metadata(metadata) + ).id + + +def _responses(gateway: Gateway, model: str, text: str, metadata: Mapping[str, JsonValue]) -> str: + with OpenAI(base_url=f"{_root(gateway)}/v1", api_key=gateway.key, max_retries=0) as client: + identity: Final = client.responses.create(model=model, input=text, extra_body=_metadata(metadata)).output[0].id + assert identity is not None + return identity + + +def _messages(gateway: Gateway, model: str, text: str, metadata: Mapping[str, JsonValue]) -> str: + with Anthropic(base_url=_root(gateway), api_key=gateway.key, max_retries=0) as client: + return client.messages.create( + model=model, + max_tokens=16, + messages=[{"role": "user", "content": text}], + extra_body=_metadata(metadata), + ).id + + +@dataclass(frozen=True, slots=True) +class Endpoint: + provider_model: str + sibling_model: str + answer: Mapping[str, JsonValue] + ask: Ask + + +@dataclass(frozen=True, slots=True) +class Group: + name: str + upstream: ScenarioHandle + + +ENDPOINTS: Final = { + "chat_completions": Endpoint(CHAT_MODEL, "openai/gpt-5.6", CHAT, _chat), + "responses": Endpoint(CHAT_MODEL, "openai/gpt-5.6", RESPONSE, _responses), + "messages": Endpoint(MESSAGES_MODEL, "anthropic/claude-sonnet-5-5", MESSAGE, _messages), +} + + +def _group(scenario: Scenario, endpoint: Endpoint, name: str | None = None) -> Group: + handle: Final = scripted(scenario, json_response(endpoint.answer)) + return Group(scenario.model(name=name, model=endpoint.provider_model, api_base=handle.api_base()), handle) + + +@pytest.mark.parametrize("endpoint", ENDPOINTS.values(), ids=ENDPOINTS.keys()) +def test_identical_requests_to_two_groups_on_one_model_each_reach_their_own_deployment( + gateway: Gateway, endpoint: Endpoint +) -> None: + upstream: Final = Upstream(gateway.upstream_url) + store: Final = redis_store() + text: Final = prompt() + with gateway.scenario() as scenario: + first: Final = _group(scenario, endpoint) + second: Final = _group(scenario, endpoint) + filled: Final = endpoint.ask(gateway, first.name, text, {}) + await_entry(store, filled) + answered: Final = endpoint.ask(gateway, second.name, text, {}) + assert answered != filled, f"{second.name} was served the answer cached for {first.name}" + assert upstream.calls(second.upstream.scenario_id) == 1 + assert endpoint.ask(gateway, first.name, text, {}) == filled + assert upstream.calls(first.upstream.scenario_id) == 1 + + +PLANTS: Final[Mapping[str, Callable[[str], Mapping[str, JsonValue]]]] = { + "model_group": lambda target: {"model_group": target}, + "caching_groups": lambda target: {"caching_groups": [target]}, +} + + +@pytest.mark.parametrize("plant", PLANTS.values(), ids=PLANTS.keys()) +@pytest.mark.parametrize("endpoint", ENDPOINTS.values(), ids=ENDPOINTS.keys()) +def test_a_caller_naming_another_group_in_metadata_never_fills_that_groups_entry( + gateway: Gateway, endpoint: Endpoint, plant: Callable[[str], Mapping[str, JsonValue]] +) -> None: + upstream: Final = Upstream(gateway.upstream_url) + store: Final = redis_store() + text: Final = prompt() + with gateway.scenario() as scenario: + caller: Final = _group(scenario, endpoint) + target: Final = _group(scenario, endpoint, name=f"{caller.name}-premium") + planted: Final = endpoint.ask(gateway, caller.name, text, plant(target.name)) + assert upstream.calls(caller.upstream.scenario_id) == 1 + await_entry(store, planted) + answered: Final = endpoint.ask(gateway, target.name, text, {}) + assert answered != planted, f"{target.name} was served the answer {caller.name} planted under its name" + assert upstream.calls(target.upstream.scenario_id) == 1 + await_entry(store, answered) + assert endpoint.ask(gateway, target.name, text, {}) == answered + + +def _caching_groups_config(directory: Path, endpoint: Endpoint, first: Group, second: Group) -> Path: + rig: Final = JSON_OBJECT.validate_python( + yaml.safe_load((Path(__file__).resolve().parents[1] / "proxy_config.yaml").read_text()) + ) + router_settings: Final = object_value(rig.get("router_settings") or {}) + config: Final = { + **rig, + "model_list": [ + { + "model_name": group.name, + "litellm_params": {"model": model, "api_key": PROVIDER_KEY, "api_base": group.upstream.api_base()}, + } + for group, model in ((first, endpoint.provider_model), (second, endpoint.sibling_model)) + ], + "router_settings": {**router_settings, "caching_groups": [[first.name, second.name]]}, + } + path: Final = directory / f"proxy_config-{uuid.uuid4().hex[:8]}.yaml" + path.write_text(yaml.safe_dump(config, sort_keys=False)) + return path + + +def _configured_group(scenario: Scenario, endpoint: Endpoint) -> Group: + return Group(f"cache-group-{uuid.uuid4().hex[:8]}", scripted(scenario, json_response(endpoint.answer))) + + +@pytest.mark.timeout(2 * graceful_stop_seconds() + 120) +@pytest.mark.parametrize("endpoint", ENDPOINTS.values(), ids=ENDPOINTS.keys()) +def test_groups_in_one_caching_group_on_different_models_share_one_entry( + gateway: Gateway, endpoint: Endpoint, tmp_path: Path +) -> None: + upstream: Final = Upstream(gateway.upstream_url) + store: Final = redis_store() + text: Final = prompt() + with gateway.scenario() as scenario: + first: Final = _configured_group(scenario, endpoint) + second: Final = _configured_group(scenario, endpoint) + config: Final = _caching_groups_config(tmp_path, endpoint, first, second) + with owned_proxy_process( + gateway, tmp_path, {}, config=config, workers=1, state=OwnedState.SHARED_WITH_WORKER + ) as owned: + filled: Final = endpoint.ask(owned.gateway, first.name, text, {}) + await_entry(store, filled) + assert endpoint.ask(owned.gateway, second.name, text, {}) == filled + assert upstream.calls(second.upstream.scenario_id) == 0 + assert upstream.calls(first.upstream.scenario_id) == 1 diff --git a/tests/integration/caching/test_response_cache_stream_replay.py b/tests/integration/caching/test_response_cache_stream_replay.py new file mode 100644 index 00000000000..e6179144327 --- /dev/null +++ b/tests/integration/caching/test_response_cache_stream_replay.py @@ -0,0 +1,208 @@ +"""A chat stream served from the response cache carries the reasoning, citations, audio and tool calls it first had. + +Every cell scripts one chat stream for its own deployment, streams it once through the OpenAI SDK so the proxy +assembles and caches it, then streams the identical request again until an answer arrives without an upstream call +and reads the replayed deltas the way a client would. +""" + +from __future__ import annotations + +import json +from collections.abc import Iterator +from typing import Final + +import pytest +from openai import OpenAI +from pydantic import JsonValue + +from tests.integration._support.client import Gateway +from tests.integration.caching.response_cache_case import ( + CHAT_CHUNK, + Upstream, + await_entry, + prompt, + redis_store, + scripted, +) +from tests.integration.cost_calculation.cost_tracking_case import SseResponse + +REPLAY_ATTEMPTS: Final = 8 +CITATION: Final = ( + '{"type":"url_citation","url_citation":{"url":"https://example.com/apples","title":"Apples",' + '"start_index":0,"end_index":19}}' +) +STOP: Final = CHAT_CHUNK + '[{"index":0,"delta":{},"finish_reason":"stop"}]}' +REASONING_STREAM: Final = SseResponse( + content_type="text/event-stream", + frames=( + CHAT_CHUNK + '[{"index":0,"delta":{"role":"assistant","reasoning_content":"Count the apples "},' + '"finish_reason":null}]}', + CHAT_CHUNK + '[{"index":0,"delta":{"reasoning_content":"in each basket"},"finish_reason":null}]}', + CHAT_CHUNK + '[{"index":0,"delta":{"content":"There are "},"finish_reason":null}]}', + CHAT_CHUNK + '[{"index":0,"delta":{"content":"14 apples"},"finish_reason":null}]}', + CHAT_CHUNK + '[{"index":0,"delta":{"annotations":[' + CITATION + ']},"finish_reason":null}]}', + STOP, + "data: [DONE]", + ), +) +AUDIO_STREAM: Final = SseResponse( + content_type="text/event-stream", + frames=( + CHAT_CHUNK + '[{"index":0,"delta":{"role":"assistant","audio":{"id":"audio_$UNIQUE_ID","transcript":"Hello"}},' + '"finish_reason":null}]}', + CHAT_CHUNK + '[{"index":0,"delta":{"audio":{"data":"UklGRg=="}},"finish_reason":null}]}', + CHAT_CHUNK + '[{"index":0,"delta":{"audio":{"expires_at":1700000600}},"finish_reason":null}]}', + STOP, + "data: [DONE]", + ), +) + +TOOL_CALL: Final = ( + '{"index":0,"id":"call_$UNIQUE_ID","type":"function","function":{"name":"count_apples","arguments":"{}"}}' +) +TOOL_STREAM: Final = SseResponse( + content_type="text/event-stream", + frames=( + CHAT_CHUNK + '[{"index":0,"delta":{"role":"assistant","reasoning_content":"I need the basket totals"},' + '"finish_reason":null}]}', + CHAT_CHUNK + '[{"index":0,"delta":{"content":"Let me count "},"finish_reason":null}]}', + CHAT_CHUNK + '[{"index":0,"delta":{"content":"the apples"},"finish_reason":null}]}', + CHAT_CHUNK + '[{"index":0,"delta":{"tool_calls":[' + TOOL_CALL + ']},"finish_reason":null}]}', + CHAT_CHUNK + '[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}', + "data: [DONE]", + ), +) +TOOL_ONLY_STREAM: Final = SseResponse( + content_type="text/event-stream", + frames=( + CHAT_CHUNK + '[{"index":0,"delta":{"role":"assistant","reasoning_content":"I need the basket totals"},' + '"finish_reason":null}]}', + CHAT_CHUNK + '[{"index":0,"delta":{"tool_calls":[' + TOOL_CALL + ']},"finish_reason":null}]}', + CHAT_CHUNK + '[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}', + "data: [DONE]", + ), +) + +WHITESPACE_TOOL_STREAM: Final = SseResponse( + content_type="text/event-stream", + frames=( + CHAT_CHUNK + '[{"index":0,"delta":{"role":"assistant","reasoning_content":"I need the basket totals"},' + '"finish_reason":null}]}', + CHAT_CHUNK + '[{"index":0,"delta":{"content":"\\n\\n"},"finish_reason":null}]}', + CHAT_CHUNK + '[{"index":0,"delta":{"tool_calls":[' + TOOL_CALL + ']},"finish_reason":null}]}', + CHAT_CHUNK + '[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}', + "data: [DONE]", + ), +) + +_Stream = tuple[frozenset[str], tuple[dict[str, JsonValue], ...]] + + +def _stream(gateway: Gateway, model: str, text: str) -> _Stream: + """The chunk ids and every choice's delta with its finish reason, after the whole stream was read.""" + with ( + OpenAI( + base_url=f"{str(gateway.client.base_url).rstrip('/')}/v1", api_key=gateway.key, max_retries=0 + ) as client, + client.chat.completions.create( + model=model, messages=[{"role": "user", "content": text}], stream=True + ) as stream, + ): + chunks: Final = tuple(stream) + deltas: Final = tuple( + {**choice.delta.model_dump(exclude_none=True), "finish_reason": choice.finish_reason} + for chunk in chunks + for choice in chunk.choices + ) # comprehension-ok: flattens chunk choices + return frozenset(chunk.id for chunk in chunks), deltas + + +def _attempt(gateway: Gateway, upstream: Upstream, identity: str, model: str, text: str) -> _Stream | None: + before: Final = upstream.calls(identity) + answer: Final = _stream(gateway, model, text) + return answer if upstream.calls(identity) == before else None + + +def _attempts(gateway: Gateway, upstream: Upstream, identity: str, model: str, text: str) -> Iterator[_Stream | None]: + return (_attempt(gateway, upstream, identity, model, text) for _ in range(REPLAY_ATTEMPTS)) + + +def _replayed(gateway: Gateway, script: SseResponse) -> tuple[dict[str, JsonValue], ...]: + """The deltas of the first identical stream served without an upstream call, after one stream filled the cache. + + A streamed request can reach the upstream once per proxy worker before a replay is served, since a worker keys a + stream only once its router resolves the model the other worker registered. + """ + upstream: Final = Upstream(gateway.upstream_url) + text: Final = prompt() + with gateway.scenario() as scenario: + handle: Final = scripted(scenario, script) + model: Final = scenario.model(api_base=handle.api_base()) + first_ids, _ = _stream(gateway, model, text) + assert upstream.calls(handle.scenario_id) == 1 + await_entry(redis_store(), next(iter(first_ids))) + replay: Final = next( + (answer for answer in _attempts(gateway, upstream, handle.scenario_id, model, text) if answer), None + ) + if replay is None: + pytest.fail(f"no stream was replayed from the cache in {REPLAY_ATTEMPTS} attempts") + return replay[1] + + +def _joined(deltas: tuple[dict[str, JsonValue], ...], field: str) -> str: + return "".join(str(delta[field]) for delta in deltas if delta.get(field)) + + +def test_a_replayed_chat_stream_sends_the_reasoning_before_the_text_and_the_citation_after_it( + gateway: Gateway, +) -> None: + deltas: Final = _replayed(gateway, REASONING_STREAM) + reasoning: Final = [index for index, delta in enumerate(deltas) if delta.get("reasoning_content")] + text: Final = [index for index, delta in enumerate(deltas) if delta.get("content")] + cited: Final = [index for index, delta in enumerate(deltas) if delta.get("annotations")] + assert _joined(deltas, "reasoning_content") == "Count the apples in each basket", deltas + assert _joined(deltas, "content") == "There are 14 apples", deltas + assert reasoning and text and max(reasoning) < min(text), deltas + assert cited == [max(text)] and deltas[max(text)]["annotations"] == [json.loads(CITATION)], deltas + assert deltas[-1]["finish_reason"] == "stop", deltas + + +def test_a_replayed_chat_stream_without_text_sends_its_role_and_audio(gateway: Gateway) -> None: + deltas: Final = _replayed(gateway, AUDIO_STREAM) + audio: Final = [delta["audio"] for delta in deltas if delta.get("audio")] + assert deltas[0].get("role") == "assistant", deltas + assert len(audio) == 1, deltas + assert isinstance(audio[0], dict) and audio[0]["data"] == "UklGRg==" and audio[0]["transcript"] == "Hello", deltas + assert deltas[-1]["finish_reason"] == "stop", deltas + + +def test_a_replayed_chat_stream_sends_the_tool_call_after_the_reasoning_and_the_text(gateway: Gateway) -> None: + deltas: Final = _replayed(gateway, TOOL_STREAM) + reasoning: Final = [index for index, delta in enumerate(deltas) if delta.get("reasoning_content")] + text: Final = [index for index, delta in enumerate(deltas) if delta.get("content")] + called: Final = [index for index, delta in enumerate(deltas) if delta.get("tool_calls")] + assert _joined(deltas, "content") == "Let me count the apples", deltas + assert reasoning and text and max(reasoning) < min(text), deltas + assert called == [max(text) + 1], deltas + assert deltas[-1]["finish_reason"] == "tool_calls", deltas + + +def test_a_replayed_chat_stream_without_text_sends_the_reasoning_before_the_tool_call(gateway: Gateway) -> None: + deltas: Final = _replayed(gateway, TOOL_ONLY_STREAM) + reasoning: Final = [index for index, delta in enumerate(deltas) if delta.get("reasoning_content")] + called: Final = [index for index, delta in enumerate(deltas) if delta.get("tool_calls")] + assert _joined(deltas, "reasoning_content") == "I need the basket totals", deltas + assert reasoning and called and max(reasoning) < min(called), deltas + assert deltas[-1]["finish_reason"] == "tool_calls", deltas + + +def test_a_replayed_chat_stream_keeps_whitespace_only_text_between_the_reasoning_and_the_tool_call( + gateway: Gateway, +) -> None: + deltas: Final = _replayed(gateway, WHITESPACE_TOOL_STREAM) + reasoning: Final = [index for index, delta in enumerate(deltas) if delta.get("reasoning_content")] + text: Final = [index for index, delta in enumerate(deltas) if delta.get("content")] + called: Final = [index for index, delta in enumerate(deltas) if delta.get("tool_calls")] + assert _joined(deltas, "content") == "\n\n", deltas + assert reasoning and text and called and max(reasoning) < min(text) and max(text) < min(called), deltas + assert deltas[-1]["finish_reason"] == "tool_calls", deltas diff --git a/tests/integration/database/migrations/test_request_log_indexes.py b/tests/integration/database/migrations/test_request_log_indexes.py index f558c355c84..d06f0b13cff 100644 --- a/tests/integration/database/migrations/test_request_log_indexes.py +++ b/tests/integration/database/migrations/test_request_log_indexes.py @@ -16,7 +16,7 @@ from typing import Final import psycopg import pytest -from litellm_proxy_extras import migration_data_checks, request_log_indexes +from litellm_proxy_extras import migration_data_checks, migration_dry_run, request_log_indexes from litellm_proxy_extras.migration_lock import MIGRATION_LOCK_KEY, migration_lock from litellm_proxy_extras.migration_recovery import roll_back_failed_inert_migration from litellm_proxy_extras.request_log_indexes import ( @@ -967,33 +967,25 @@ _HASH_LEGACY_KEYS: Final = ( @requires_db @RESOLVERS @pytest.mark.parametrize("release", (API_KEY_INDEX_MIGRATION,), ids=("v1.102.1",)) -def test_the_migration_job_refuses_before_the_first_migration_when_spend_log_keys_are_too_long_for_the_index( +def test_the_migration_job_applies_every_migration_and_only_the_index_build_waits_for_too_long_keys( scratch_database: str, use_v2_resolver: bool ) -> None: + """Rows never stop an upgrade. The migrations go through, and the one request-log index + whose keys do not fit is left for the next build with the rows named in the log.""" with psycopg.connect(scratch_database, autocommit=True) as conn: _insert_spend_log_with_key(conn, "legacy-1", _legacy_bearer_token()) _insert_spend_log_with_key(conn, "legacy-2", _legacy_bearer_token()) _insert_spend_log_with_key(conn, "hashed-1", "a" * 64) - _insert_spend_log_with_key(conn, "repetitive-1", "z" * 3000) ledger_before: Final = _ledger(scratch_database) - with pytest.raises(RuntimeError) as refused: - ProxyExtrasDBManager.run_migration_job(use_migrate=True, use_v2_resolver=use_v2_resolver) - - refusal: Final = str(refused.value) - assert ( - '"LiteLLM_SpendLogs".api_key, startTime: 2 row(s) with an index entry over 2704 bytes, largest 3232 bytes ' - "(api_key up to 3207 bytes)" - ) in refusal, refusal - assert f'blocks: CREATE INDEX "{API_KEY_INDEX}"' in refusal, refusal - assert _ledger(scratch_database) == ledger_before, "a refused job must not apply a single migration" + assert ProxyExtrasDBManager.run_migration_job(use_migrate=True, use_v2_resolver=use_v2_resolver) is False + assert len(_ledger(scratch_database)) > len(ledger_before), "the rows must not hold back a single migration" assert API_KEY_INDEX not in _index_validity(scratch_database, "api_key_startTime_idx") with psycopg.connect(scratch_database, autocommit=True) as conn: conn.execute(_HASH_LEGACY_KEYS) assert ProxyExtrasDBManager.run_migration_job(use_migrate=True, use_v2_resolver=use_v2_resolver) is True assert _index_validity(scratch_database, "api_key_startTime_idx") == {API_KEY_INDEX: True} - assert len(_ledger(scratch_database)) > len(ledger_before) @requires_db @@ -1017,49 +1009,6 @@ def test_a_serving_proxy_index_build_that_meets_too_long_keys_reports_the_rows_i assert API_KEY_INDEX not in _index_validity(scratch_database, "api_key_startTime_idx") -_PROBE_STATEMENTS: Final = ( - 'CREATE UNIQUE INDEX "probe_b_key" ON "preflight_probe" ("b")', - 'ALTER TABLE "preflight_probe" ALTER COLUMN "b" SET NOT NULL', - 'ALTER TABLE "preflight_probe" ADD COLUMN "c" INTEGER NOT NULL', - 'ALTER TABLE "preflight_probe" ADD CONSTRAINT "probe_r_fkey" FOREIGN KEY ("r") REFERENCES "preflight_parent"("id")', - 'ALTER TABLE "preflight_probe" ADD CONSTRAINT "probe_a_check" CHECK ("a" <> \'bad\')', - 'CREATE INDEX "probe_a_idx" ON "preflight_probe" ("a")', -) - - -@requires_db -def test_each_check_kind_counts_exactly_the_rows_postgres_would_reject(scratch_database: str) -> None: - with psycopg.connect(scratch_database, autocommit=True) as conn: - conn.execute('CREATE TABLE "preflight_parent" ("id" INTEGER PRIMARY KEY)') - conn.execute('CREATE TABLE "preflight_probe" ("a" TEXT, "b" INTEGER, "r" INTEGER)') - conn.execute('INSERT INTO "preflight_parent" VALUES (1)') - conn.execute( - 'INSERT INTO "preflight_probe" VALUES (%s, 1, 1), (%s, 1, 1), (%s, NULL, 2), (%s, 2, NULL), (%s, 3, 1)', - ("ok", "ok", "bad", "bad", secrets.token_hex(1400)), - ) - checks: Final = tuple( - check - for statement in _PROBE_STATEMENTS - for check in migration_data_checks.checks_for_statement(statement, "probe") - ) - findings: Final = migration_data_checks.find_blockers(conn, "public", checks) - for statement in _PROBE_STATEMENTS: - with conn.transaction(force_rollback=True), pytest.raises(psycopg.Error): - conn.execute(statement) # pyright: ignore[reportArgumentType] # test SQL - - counted: Final = { - (f.check.kind, f.check.columns): f.rows for f in findings if isinstance(f, migration_data_checks.BlockedChange) - } - assert counted == { - ("duplicates", ("b",)): 1, - ("nulls", ("b",)): 1, - ("any_row", ("c",)): 5, - ("orphans", ("r",)): 1, - ("check_violations", ()): 2, - ("too_long", ("a",)): 1, - } - - @requires_db @pytest.mark.parametrize( ("index_columns", "s_value", "t_value", "longest_fitting_key", "described_as"), @@ -1098,7 +1047,7 @@ def test_the_entry_size_check_draws_the_line_exactly_where_postgres_does( ), ) findings: Final = migration_data_checks.find_blockers( - conn, "public", migration_data_checks.checks_for_statement(create_index.as_string(conn), "probe") + conn, "public", migration_data_checks.checks_for_index("entry_probe", "entry_probe_idx", index_columns) ) with pytest.raises(psycopg.errors.ProgramLimitExceeded, match="exceeds btree version 4 maximum 2704"): conn.execute(create_index) @@ -1122,7 +1071,7 @@ def test_a_value_postgres_compresses_under_the_limit_is_not_counted_while_random conn.execute('CREATE TABLE "compress_probe" ("k" TEXT)') conn.execute('INSERT INTO "compress_probe" ("k") VALUES (%s), (%s)', ("a" * 3000, secrets.token_hex(1500))) findings: Final = migration_data_checks.find_blockers( - conn, "public", migration_data_checks.checks_for_statement(create_index.as_string(conn), "probe") + conn, "public", migration_data_checks.checks_for_index("compress_probe", "compress_probe_idx", ("k",)) ) with pytest.raises(psycopg.errors.ProgramLimitExceeded, match="index row size 3016 exceeds"): conn.execute(create_index) @@ -1154,7 +1103,6 @@ def test_the_check_flags_exactly_the_compressed_keys_postgres_rejects_at_the_ent check and through Postgres one by one. The check must flag a key exactly when Postgres rejects it.""" noise: Final = "".join(hashlib.sha256(str(i).encode()).hexdigest() for i in range(40)) keys: Final = tuple(noise[:n] + "a" * 1500 for n in range(2400, 2500)) - create_index: Final = sql.SQL('CREATE INDEX "stored_probe_idx" ON "stored_probe" ("k")') with psycopg.connect(scratch_database, autocommit=True) as conn: conn.execute('CREATE TABLE "stored_probe" ("k" TEXT)') conn.execute('INSERT INTO "stored_probe" ("k") SELECT unnest(%s::text[])', (list(keys),)) @@ -1162,7 +1110,7 @@ def test_the_check_flags_exactly_the_compressed_keys_postgres_rejects_at_the_ent 'SELECT count(*) FROM "stored_probe" WHERE pg_column_size("k") < octet_length("k") AND octet_length("k") > 2704' ).fetchone() findings: Final = migration_data_checks.find_blockers( - conn, "public", migration_data_checks.checks_for_statement(create_index.as_string(conn), "probe") + conn, "public", migration_data_checks.checks_for_index("stored_probe", "stored_probe_idx", ("k",)) ) rejected: Final = tuple(key for key in keys if _postgres_rejects_an_index_on(conn, key)) @@ -1176,14 +1124,176 @@ def test_the_check_flags_exactly_the_compressed_keys_postgres_rejects_at_the_ent @requires_db def test_a_scan_that_cannot_finish_in_time_is_reported_as_unverified_rather_than_passed(scratch_database: str) -> None: with psycopg.connect(scratch_database, autocommit=True) as conn: - conn.execute('CREATE TABLE "preflight_slow" ("a" INTEGER)') - conn.execute('INSERT INTO "preflight_slow" SELECT generate_series(1, 50)') - (check,) = migration_data_checks.checks_for_statement( - 'ALTER TABLE "preflight_slow" ADD CONSTRAINT "slow" CHECK (pg_sleep(0.1) IS NULL)', "probe" + conn.execute('CREATE TABLE "preflight_slow" ("k" TEXT)') + conn.execute( + "INSERT INTO \"preflight_slow\" SELECT md5(i::text) || repeat('a', 1500) FROM generate_series(1, 200000) AS i" ) - findings: Final = migration_data_checks.find_blockers(conn, "public", (check,), timeout_seconds=0.5) + (check,) = migration_data_checks.checks_for_index("preflight_slow", "preflight_slow_idx", ("k",)) + findings: Final = migration_data_checks.find_blockers(conn, "public", (check,), timeout_seconds=0.01) timeout_afterwards: Final = conn.execute("SHOW statement_timeout").fetchone() - assert findings == (migration_data_checks.CouldNotVerify(check, 0.5),) - assert timeout_afterwards == ("0",), "the preflight must hand the session back with the timeout it found" - assert "could not be checked within 0.5 s" in migration_data_checks.report(findings) + assert findings == (migration_data_checks.CouldNotVerify(check, 0.01),) + assert timeout_afterwards == ("0",), "the check must hand the session back with the timeout it found" + assert "could not be checked within 0.01 s" in migration_data_checks.report(findings) + + +CURRENT_RELEASE: Final = "99999999999999" +REQUIRE_TEAM_ALIAS: Final = "20991231000000_require_team_alias" + + +def _extras_with_scratch_migration(tmp_path: Path, name: str, migration_sql: str) -> Path: + """A copy of the shipped package dir with one more migration folder, the shape `LITELLM_MIGRATION_DIR` points at.""" + extras: Final = tmp_path / "extras" + shutil.copytree(PACKAGE / "migrations", extras / "migrations") + shutil.copy(PACKAGE / "schema.prisma", extras / "schema.prisma") + (extras / "migrations" / name).mkdir() + (extras / "migrations" / name / "migration.sql").write_text(migration_sql) + return extras + + +def _team_alias_is_nullable(database_url: str) -> bool: + with psycopg.connect(database_url) as conn: + row: Final = conn.execute( + "SELECT is_nullable FROM information_schema.columns " + "WHERE table_name = 'LiteLLM_TeamTable' AND column_name = 'team_alias'" + ).fetchone() + return row == ("YES",) + + +@requires_db +@pytest.mark.parametrize("release", (API_KEY_INDEX_MIGRATION,), ids=("v1.102.1",)) +def test_the_dry_run_applies_every_pending_migration_in_order_and_rolls_all_of_it_back(scratch_database: str) -> None: + ledger_before: Final = _ledger(scratch_database) + with psycopg.connect(scratch_database, autocommit=True) as conn: + pending: Final = migration_data_checks.pending_migrations(conn, "public", PACKAGE / "migrations") + result: Final = migration_dry_run.dry_run(conn, "public", PACKAGE / "migrations") + + assert result.rejected is None, result + assert tuple(applied.name for applied in result.applied) == tuple(name for name, _ in pending) + assert len(result.applied) > 1 + assert _ledger(scratch_database) == ledger_before, "a dry run must leave the ledger alone" + assert _index_validity(scratch_database, "api_key_startTime_idx") == {}, "a dry run must leave no index behind" + names: Final = tuple(name for name, _ in pending) + assert all(set(applied.replayed) <= set(names[: names.index(applied.name)]) for applied in result.applied), ( + "a migration is only ever replayed behind a later one that depends on it" + ) + assert "applied one at a time in a dry run" in migration_dry_run.report(result) + + +@requires_db +@pytest.mark.parametrize("release", (CURRENT_RELEASE,), ids=("current",)) +def test_the_dry_run_reports_postgres_own_error_for_the_row_a_migration_cannot_take( + scratch_database: str, tmp_path: Path +) -> None: + extras: Final = _extras_with_scratch_migration( + tmp_path, REQUIRE_TEAM_ALIAS, 'ALTER TABLE "LiteLLM_TeamTable" ALTER COLUMN "team_alias" SET NOT NULL;\n' + ) + ledger_before: Final = _ledger(scratch_database) + with psycopg.connect(scratch_database, autocommit=True) as conn: + conn.execute('INSERT INTO "LiteLLM_TeamTable" ("team_id") VALUES (%s)', ("no-alias",)) + result: Final = migration_dry_run.dry_run(conn, "public", extras / "migrations") + + assert result.rejected is not None, result + assert result.rejected.name == REQUIRE_TEAM_ALIAS + assert 'column "team_alias" of relation "LiteLLM_TeamTable" contains null values' in result.rejected.error + assert result.applied == () + assert _team_alias_is_nullable(scratch_database), "a rejected dry run must leave the schema as it was" + assert _ledger(scratch_database) == ledger_before + + +@requires_db +@pytest.mark.parametrize("release", (CURRENT_RELEASE,), ids=("current",)) +def test_the_cli_summary_turns_from_postgres_error_to_timings_once_the_rows_are_fixed( + scratch_database: str, tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + extras: Final = _extras_with_scratch_migration( + tmp_path, REQUIRE_TEAM_ALIAS, 'ALTER TABLE "LiteLLM_TeamTable" ALTER COLUMN "team_alias" SET NOT NULL;\n' + ) + monkeypatch.setenv("LITELLM_MIGRATION_DIR", str(extras)) + with psycopg.connect(scratch_database, autocommit=True) as conn: + conn.execute('INSERT INTO "LiteLLM_TeamTable" ("team_id") VALUES (%s)', ("no-alias",)) + + ok, summary = ProxyExtrasDBManager.migration_dry_run_summary(use_migrate=True) + assert ok is False + assert summary.startswith(f"Migration {REQUIRE_TEAM_ALIAS} fails on this database after "), summary + assert "contains null values" in summary + + with psycopg.connect(scratch_database, autocommit=True) as conn: + conn.execute('UPDATE "LiteLLM_TeamTable" SET "team_alias" = \'filled\' WHERE "team_alias" IS NULL') + ok_after, summary_after = ProxyExtrasDBManager.migration_dry_run_summary(use_migrate=True) + assert ok_after is True + assert summary_after.startswith("1 pending migration(s) applied one at a time in a dry run"), summary_after + assert f"\n {REQUIRE_TEAM_ALIAS}: " in summary_after + assert _team_alias_is_nullable(scratch_database) + + +@requires_db +@pytest.mark.parametrize("release", (CURRENT_RELEASE,), ids=("current",)) +def test_a_migration_that_depends_on_the_one_before_it_is_replayed_with_it_while_independent_ones_run_alone( + scratch_database: str, tmp_path: Path +) -> None: + extras: Final = _extras_with_scratch_migration( + tmp_path, "20991231000000_add_team_region", 'ALTER TABLE "LiteLLM_TeamTable" ADD COLUMN "region" TEXT;\n' + ) + (extras / "migrations" / "20991231000001_index_team_region").mkdir() + (extras / "migrations" / "20991231000001_index_team_region" / "migration.sql").write_text( + 'CREATE INDEX CONCURRENTLY "LiteLLM_TeamTable_region_idx" ON "LiteLLM_TeamTable"("region");\n' + ) + (extras / "migrations" / "20991231000002_index_team_alias").mkdir() + (extras / "migrations" / "20991231000002_index_team_alias" / "migration.sql").write_text( + 'CREATE INDEX "LiteLLM_TeamTable_alias_idx" ON "LiteLLM_TeamTable"("team_alias");\n' + ) + with psycopg.connect(scratch_database, autocommit=True) as conn: + result: Final = migration_dry_run.dry_run(conn, "public", extras / "migrations") + region: Final = conn.execute( + "SELECT 1 FROM information_schema.columns WHERE table_name = 'LiteLLM_TeamTable' AND column_name = 'region'" + ).fetchone() + + assert result.rejected is None, result + assert tuple((applied.name, applied.replayed) for applied in result.applied) == ( + ("20991231000000_add_team_region", ()), + ("20991231000001_index_team_region", ("20991231000000_add_team_region",)), + ("20991231000002_index_team_alias", ()), + ) + assert region is None, "every transaction must have been rolled back" + with psycopg.connect(scratch_database) as conn: + probe_indexes: Final = conn.execute( + "SELECT indexname FROM pg_indexes " + "WHERE indexname IN ('LiteLLM_TeamTable_region_idx', 'LiteLLM_TeamTable_alias_idx')" + ).fetchall() + assert probe_indexes == [], probe_indexes + + +@requires_db +@pytest.mark.parametrize("release", (CURRENT_RELEASE,), ids=("current",)) +def test_a_database_with_tables_but_no_ledger_is_judged_by_the_schema_diff_instead_of_replaying_history( + scratch_database: str, tmp_path: Path +) -> None: + with psycopg.connect(scratch_database, autocommit=True) as conn: + conn.execute("DROP TABLE _prisma_migrations") + matching: Final = migration_dry_run.dry_run(conn, "public", PACKAGE / "migrations", baseline_check=lambda: None) + differing: Final = migration_dry_run.dry_run( + conn, + "public", + PACKAGE / "migrations", + baseline_check=lambda: ( + "the schema differs from this build. prisma migrate diff:\nALTER TABLE x ADD COLUMN y TEXT;" + ), + ) + custom: Final = migration_dry_run.dry_run( + conn, + "public", + PACKAGE / "migrations", + baseline_check=lambda: ProxyExtrasDBManager._baseline_refusal( + _extras_with_scratch_migration( + tmp_path, "20991231000000_custom", 'ALTER TABLE "LiteLLM_TeamTable" ADD COLUMN "c" TEXT;\n' + ) + ), + ) + + assert matching.ok is True and matching.applied == () + assert "nothing to dry-run" in migration_dry_run.report(matching) + assert differing.ok is False + assert "ALTER TABLE x ADD COLUMN y TEXT;" in migration_dry_run.report(differing) + assert custom.ok is False + assert "custom migration history" in migration_dry_run.report(custom) diff --git a/tests/integration/management/test_fallback_management_public_names.py b/tests/integration/management/test_fallback_management_public_names.py index fcbeee9dc60..6f97165fb3c 100644 --- a/tests/integration/management/test_fallback_management_public_names.py +++ b/tests/integration/management/test_fallback_management_public_names.py @@ -1,6 +1,7 @@ import uuid from collections.abc import Iterator from concurrent.futures import ThreadPoolExecutor +from contextlib import contextmanager from functools import partial from itertools import chain from pathlib import Path @@ -10,6 +11,7 @@ import httpx import psutil import pytest from pydantic import JsonValue +from redis import Redis from tests.integration._support.client import ( Gateway, @@ -27,7 +29,7 @@ from tests.integration._support.process import ( setup_only_proxy_run, worker_configuration, ) -from tests.integration._support.redis_process import owned_redis +from tests.integration._support.redis_process import OwnedRedis, owned_redis pytestmark: Final = [pytest.mark.timeout(300), pytest.mark.shares_worker_state] @@ -385,6 +387,39 @@ def test_fallback_write_survives_a_redis_outage(gateway: Gateway, tmp_path: Path cache.start() +@contextmanager +def _refusing_cache_deletes(cache: OwnedRedis) -> Iterator[None]: + with Redis(host=cache.host, port=cache.port) as client: + client.acl_setuser("default", enabled=True, commands=["-del", "-unlink"]) + try: + yield + finally: + client.acl_setuser("default", enabled=True, commands=["+del", "+unlink"]) + + +@pytest.mark.usefixtures("migrated_database") +def test_consecutive_creates_keep_every_rule_when_redis_refuses_the_cache_eviction( + gateway: Gateway, tmp_path: Path +) -> None: + with owned_redis(tmp_path) as cache: + overrides: Final = {"REDIS_HOST": cache.host, "REDIS_PORT": str(cache.port)} + with owned_proxy_process(gateway, tmp_path, overrides, database_setup=()) as owned: + with owned.gateway.scenario() as scenario: + target: Final = scenario.model(model=f"openai/x3t-{uuid.uuid4().hex}") + first: Final = scenario.model(model=f"openai/x3a-{uuid.uuid4().hex}") + second: Final = scenario.model(model=f"openai/x3b-{uuid.uuid4().hex}") + for model in (target, first, second): + _wait_until_served(owned.gateway, model) + with _refusing_cache_deletes(cache): + created: Final = tuple( + _create_fallback(owned.gateway, scenario, model, [target]) for model in (first, second) + ) + assert all(response.status_code == 200 for response in created), [r.text for r in created] + covered: Final = _covered_models(_stored_fallbacks()) + assert {first, second} <= covered, covered + assert EVICTION_WARNING in owned.log.read_text(), owned.log.read_text()[-3000:] + + @pytest.mark.usefixtures("migrated_database") def test_fallback_write_survives_a_worker_kill(gateway: Gateway, tmp_path: Path) -> None: with owned_proxy_process(gateway, tmp_path, {}, workers=2, database_setup=()) as owned: diff --git a/tests/integration/messages_endpoint/chat_bridge/test_anthropic_messages_pdf_document_wire.py b/tests/integration/messages_endpoint/chat_bridge/test_anthropic_messages_pdf_document_wire.py new file mode 100644 index 00000000000..130bab24f4c --- /dev/null +++ b/tests/integration/messages_endpoint/chat_bridge/test_anthropic_messages_pdf_document_wire.py @@ -0,0 +1,421 @@ +import json +import uuid +from collections.abc import Callable, Sequence +from dataclasses import dataclass +from pathlib import Path +from typing import Final + +import anthropic +import pytest +from integration._support.client import Gateway, Scenario, string_value +from integration._support.pdf_document import LETTER, Page, document, encoded, pdf_bytes +from integration._support.process import owned_proxy_process +from integration._support.wire import Reply, Request, Wire, wire_server +from pydantic import JsonValue, TypeAdapter + +_API_KEY: Final = "synthetic-pdf-document-key" +_ANSWER: Final = "The codeword is PELICAN-42." +_TITLE: Final = "Quarterly report" +_URL: Final = "https://example.com/brief.pdf" +_SAP_TOKEN_TARGET: Final = "/oauth/token" +_JSON_OBJECT: Final = TypeAdapter(dict[str, JsonValue]) +_PARTS: Final = TypeAdapter(list[dict[str, JsonValue]]) + + +def _pdf(text: str) -> str: + return encoded(pdf_bytes((Page(text=text), LETTER))) + + +def _chat_completion(identity: str, model: str) -> Reply: + return Reply( + body=json.dumps( + { + "id": identity, + "object": "chat.completion", + "created": 1, + "model": model, + "choices": [ + {"index": 0, "message": {"role": "assistant", "content": _ANSWER}, "finish_reason": "stop"} + ], + "usage": {"prompt_tokens": 914, "completion_tokens": 9, "total_tokens": 923}, + } + ).encode() + ) + + +def _only_body(wire: Wire) -> dict[str, JsonValue]: + received: Final = tuple( + request + for request in wire.drain() + if request.method == "POST" and not request.target.startswith(_SAP_TOKEN_TARGET) + ) + assert len(received) == 1, received + return _JSON_OBJECT.validate_json(received[0].body) + + +def _user_parts(body: dict[str, JsonValue], key: str) -> list[dict[str, JsonValue]]: + turns: Final = _PARTS.validate_python(body[key]) + assert len(turns) == 1, turns + return _PARTS.validate_python(turns[0]["content" if key == "messages" else "parts"]) + + +def test_anthropic_sdk_pdf_documents_reach_azure_as_file_parts_named_by_title(gateway: Gateway) -> None: + marker: Final = uuid.uuid4().hex + backend: Final = f"gpt-pdf-{marker[:8]}" + identity: Final = f"chatcmpl-{marker}" + titled: Final = _pdf(f"PELICAN-42 {marker}") + untitled: Final = _pdf(f"ORCHID-77 {marker}") + question: Final = f"What codeword does each document carry? {marker}" + with wire_server(lambda _: _chat_completion(identity, backend)) as wire, gateway.scenario() as scenario: + model: Final = scenario.model( + model=f"azure/{backend}", api_base=wire.url, api_key=_API_KEY, api_version="2024-10-21" + ) + client: Final = anthropic.Anthropic(base_url=str(gateway.client.base_url), api_key=gateway.key, max_retries=0) + message: Final = client.messages.create( + model=model, + max_tokens=64, + messages=[ + { + "role": "user", + "content": [ + { + "type": "document", + "title": _TITLE, + "source": {"type": "base64", "media_type": "application/pdf", "data": titled}, + }, + { + "type": "document", + "source": {"type": "base64", "media_type": "application/pdf", "data": untitled}, + }, + {"type": "document", "source": {"type": "url", "url": _URL}}, + {"type": "text", "text": question}, + ], + } + ], + ) + assert message.id == identity + assert [(block.type, getattr(block, "text", None)) for block in message.content] == [("text", _ANSWER)] + assert _user_parts(_only_body(wire), "messages") == [ + {"type": "file", "file": {"filename": _TITLE, "file_data": f"data:application/pdf;base64,{titled}"}}, + { + "type": "file", + "file": {"filename": "document.pdf", "file_data": f"data:application/pdf;base64,{untitled}"}, + }, + {"type": "image_url", "image_url": {"url": _URL}}, + {"type": "text", "text": question}, + ] + + +@dataclass(frozen=True, slots=True) +class _Reader: + deploy: Callable[[Scenario, Wire], str] + reply: Callable[[Request], Reply] + pdf_parts: Callable[[dict[str, JsonValue]], Sequence[JsonValue]] + + +def _gemini_reply(_: Request) -> Reply: + return Reply( + body=json.dumps( + { + "candidates": [ + {"content": {"parts": [{"text": _ANSWER}], "role": "model"}, "finishReason": "STOP", "index": 0} + ], + "usageMetadata": {"promptTokenCount": 300, "candidatesTokenCount": 9, "totalTokenCount": 309}, + "modelVersion": "gemini-2.5-flash", + } + ).encode() + ) + + +def _bedrock_reply(_: Request) -> Reply: + return Reply( + body=json.dumps( + { + "output": {"message": {"role": "assistant", "content": [{"text": _ANSWER}]}}, + "stopReason": "end_turn", + "usage": {"inputTokens": 300, "outputTokens": 9, "totalTokens": 309}, + "metrics": {"latencyMs": 1}, + } + ).encode() + ) + + +def _anthropic_reply(_: Request) -> Reply: + return Reply( + body=json.dumps( + { + "id": "msg_pdf", + "type": "message", + "role": "assistant", + "model": "claude-sonnet-4-5", + "content": [{"type": "text", "text": _ANSWER}], + "stop_reason": "end_turn", + "usage": {"input_tokens": 300, "output_tokens": 9}, + } + ).encode() + ) + + +def _sap_reply(request: Request) -> Reply: + if request.target.startswith(_SAP_TOKEN_TARGET): + return Reply(body=json.dumps({"access_token": "scripted", "expires_in": 3600}).encode()) + match request.target: + case "/v2/lm/deployments": + deployment: Final = { + "scenarioId": "orchestration", + "configurationId": "orchestration-config", + "deploymentUrl": f"http://{request.headers['host']}/sap-deployment", + "createdAt": "2026-01-01T00:00:00Z", + } + return Reply(body=json.dumps({"resources": [deployment]}).encode()) + case "/v2/lm/configurations/orchestration-config": + return Reply(body=json.dumps({"executableId": "orchestration"}).encode()) + case _: + completion: Final = _JSON_OBJECT.validate_json(_chat_completion("chatcmpl-sap-pdf", "claude").body) + return Reply(body=json.dumps({"final_result": completion}).encode()) + + +def _snowflake(scenario: Scenario, wire: Wire) -> str: + return scenario.model(model="snowflake/claude-sonnet-4-5", api_base=wire.url, api_key=_API_KEY) + + +def _sap(scenario: Scenario, wire: Wire) -> str: + service_key: Final = { + "clientid": "scripted", + "clientsecret": _API_KEY, + "url": wire.url, + "serviceurls": {"AI_API_URL": wire.url}, + } + return scenario.model(model="sap/anthropic--claude-4.5-sonnet", api_base=wire.url, api_key=json.dumps(service_key)) + + +def _snowflake_pdf_parts(body: dict[str, JsonValue]) -> Sequence[JsonValue]: + return tuple(part.get("source") for part in _user_parts(body, "messages") if part.get("type") == "document") + + +def _sap_pdf_parts(body: dict[str, JsonValue]) -> Sequence[JsonValue]: + config: Final = _JSON_OBJECT.validate_python(body["config"]) + templating: Final = _JSON_OBJECT.validate_python( + _JSON_OBJECT.validate_python(config["modules"])["prompt_templating"] + ) + prompt: Final = _JSON_OBJECT.validate_python(templating["prompt"]) + turns: Final = _PARTS.validate_python(prompt["template"]) + assert len(turns) == 1, turns + parts: Final = _PARTS.validate_python(turns[0]["content"]) + return tuple( + _JSON_OBJECT.validate_python(part["image_url"]).get("url") for part in parts if part.get("type") == "image_url" + ) + + +def _gemini(scenario: Scenario, wire: Wire) -> str: + return scenario.model(model="gemini/gemini-2.5-flash", api_base=wire.url, api_key=_API_KEY) + + +def _bedrock(scenario: Scenario, wire: Wire) -> str: + return scenario.model( + model="bedrock/converse/us.amazon.nova-2-lite-v1:0", + api_key=None, + aws_access_key_id="AKIDSYNTHETIC", + aws_secret_access_key="synthetic-secret", + aws_region_name="us-east-1", + aws_bedrock_runtime_endpoint=wire.url, + ) + + +def _gemini_pdf_parts(body: dict[str, JsonValue]) -> Sequence[JsonValue]: + return tuple(part.get("inline_data") for part in _user_parts(body, "contents") if "inline_data" in part) + + +def _bedrock_pdf_parts(body: dict[str, JsonValue]) -> Sequence[JsonValue]: + documents: Final = tuple( + _JSON_OBJECT.validate_python(part["document"]) for part in _user_parts(body, "messages") if "document" in part + ) + return tuple({"format": item.get("format"), "source": item.get("source")} for item in documents) + + +@pytest.mark.parametrize( + ("reader", "expected"), + [ + pytest.param( + _Reader(_gemini, _gemini_reply, _gemini_pdf_parts), + lambda data: [{"mime_type": "application/pdf", "data": data}], + id="gemini", + ), + pytest.param( + _Reader(_bedrock, _bedrock_reply, _bedrock_pdf_parts), + lambda data: [{"format": "pdf", "source": {"bytes": data}}], + id="bedrock-converse", + ), + pytest.param( + _Reader(_snowflake, _anthropic_reply, _snowflake_pdf_parts), + lambda data: [{"type": "base64", "media_type": "application/pdf", "data": data}], + id="snowflake-claude", + ), + pytest.param( + _Reader(_sap, _sap_reply, _sap_pdf_parts), + lambda data: [f"data:application/pdf;base64,{data}"], + id="sap-orchestration", + ), + ], +) +def test_messages_pdf_still_reaches_providers_that_read_pdfs_in_their_own_document_shape( + gateway: Gateway, reader: _Reader, expected: Callable[[str], Sequence[JsonValue]] +) -> None: + marker: Final = uuid.uuid4().hex + data: Final = _pdf(f"PELICAN-42 {marker}") + with wire_server(reader.reply) as wire, gateway.scenario() as scenario: + model: Final = reader.deploy(scenario, wire) + response: Final = gateway.request( + "POST", + "/v1/messages", + { + "model": model, + "max_tokens": 64, + "messages": [ + { + "role": "user", + "content": [ + document({"type": "base64", "media_type": "application/pdf", "data": data}, title=_TITLE), + {"type": "text", "text": f"What is the codeword? {marker}"}, + ], + } + ], + }, + headers={"anthropic-version": "2023-06-01"}, + ) + assert response.status_code == 200, response.text + assert _JSON_OBJECT.validate_json(response.content)["content"] == [{"type": "text", "text": _ANSWER}] + assert list(reader.pdf_parts(_only_body(wire))) == list(expected(data)) + + +def _prompt_security(job: str) -> Callable[[Request], Reply]: + def respond(request: Request) -> Reply: + match (request.method, request.target.partition("?")[0]): + case ("POST", "/api/sanitizeFile"): + return Reply(body=json.dumps({"jobId": job}).encode()) + case ("GET", "/api/sanitizeFile"): + return Reply( + body=json.dumps( + { + "status": "done", + "content": "", + "metadata": {"action": "block", "violations": ["Malicious File"]}, + } + ).encode() + ) + case _: + return Reply(body=json.dumps({"result": {"prompt": {"action": "allow"}}}).encode()) + + return respond + + +def _delete_guardrail(gateway: Gateway, identity: str) -> None: + response: Final = gateway.request("DELETE", f"/guardrails/{identity}") + assert response.status_code == 200, response.text + + +def test_prompt_security_still_sanitizes_a_messages_pdf_and_blocks_it_before_the_provider(gateway: Gateway) -> None: + marker: Final = uuid.uuid4().hex + pdf: Final = pdf_bytes((Page(text=f"PELICAN-42 {marker}"), LETTER)) + with ( + wire_server(lambda _: _chat_completion(f"chatcmpl-{marker}", "gpt-pdf")) as provider, + wire_server(_prompt_security(f"job-{marker}")) as sink, + gateway.scenario() as scenario, + ): + model: Final = scenario.model( + model=f"azure/gpt-pdf-{marker[:8]}", api_base=provider.url, api_key=_API_KEY, api_version="2024-10-21" + ) + name: Final = f"pdf-prompt-security-{marker}" + created: Final = gateway.post( + "/guardrails", + { + "guardrail": { + "guardrail_name": name, + "litellm_params": { + "guardrail": "prompt_security", + "mode": "pre_call", + "api_key": "synthetic-prompt-security-key", + "api_base": sink.url, + }, + } + }, + ) + scenario.cleanups.callback(_delete_guardrail, gateway, string_value(created["guardrail_id"])) + response: Final = gateway.request( + "POST", + "/v1/messages", + { + "model": model, + "max_tokens": 64, + "guardrails": [name], + "messages": [ + { + "role": "user", + "content": [ + document({"type": "base64", "media_type": "application/pdf", "data": encoded(pdf)}), + {"type": "text", "text": f"Summarize this PDF {marker}"}, + ], + } + ], + }, + headers={"anthropic-version": "2023-06-01"}, + ) + assert response.status_code == 400, response.text + assert "Malicious File" in response.text + assert provider.drain() == () + uploads: Final = tuple( + request for request in sink.drain() if (request.method, request.target) == ("POST", "/api/sanitizeFile") + ) + assert len(uploads) == 1, uploads + assert pdf in uploads[0].body + + +def _gigachat_reply(file_id: str) -> Callable[[Request], Reply]: + def respond(request: Request) -> Reply: + if request.target.endswith("/files"): + return Reply(body=json.dumps({"id": file_id, "object": "file", "purpose": "general"}).encode()) + return _chat_completion(f"chatcmpl-{file_id}", "GigaChat-Pro") + + return respond + + +def test_anthropic_sdk_pdf_document_reaches_gigachat_as_an_uploaded_attachment( + gateway: Gateway, tmp_path: Path +) -> None: + marker: Final = uuid.uuid4().hex + file_id: Final = f"file-{marker}" + pdf: Final = pdf_bytes((Page(text=f"PELICAN-42 {marker}"), LETTER)) + with ( + wire_server(_gigachat_reply(file_id)) as wire, + owned_proxy_process(gateway, tmp_path, {"GIGACHAT_ACCESS_TOKEN": _API_KEY}) as owned, + owned.gateway.scenario() as scenario, + ): + model: Final = scenario.model(model="gigachat/GigaChat-Pro", api_base=f"{wire.url}/v1", api_key=_API_KEY) + client: Final = anthropic.Anthropic( + base_url=str(owned.gateway.client.base_url), api_key=owned.gateway.key, max_retries=0 + ) + message: Final = client.messages.create( + model=model, + max_tokens=64, + messages=[ + { + "role": "user", + "content": [ + { + "type": "document", + "source": {"type": "base64", "media_type": "application/pdf", "data": encoded(pdf)}, + }, + {"type": "text", "text": f"What is the codeword? {marker}"}, + ], + } + ], + ) + assert [(block.type, getattr(block, "text", None)) for block in message.content] == [("text", _ANSWER)] + upload, chat = (request for request in wire.drain() if request.method == "POST") + assert upload.target == "/v1/files", upload.target + assert upload.headers["authorization"] == f"Bearer {_API_KEY}", sorted(upload.headers) + assert pdf in upload.body + assert b"application/pdf" in upload.body + assert chat.target == "/v1/chat/completions", chat.target + turns: Final = _PARTS.validate_python(_JSON_OBJECT.validate_json(chat.body)["messages"]) + assert turns[-1]["attachments"] == [file_id], chat.body diff --git a/tests/integration/messages_endpoint/test_beta_gated_fields_wire.py b/tests/integration/messages_endpoint/test_beta_gated_fields_wire.py new file mode 100644 index 00000000000..26ad9f20897 --- /dev/null +++ b/tests/integration/messages_endpoint/test_beta_gated_fields_wire.py @@ -0,0 +1,183 @@ +import json +import re +import uuid +from collections.abc import Iterator, Mapping +from dataclasses import dataclass +from pathlib import Path +from typing import Final + +import pytest +from integration._support.client import Gateway, gateway_from_environment, list_value, object_value +from integration._support.process import owned_proxy +from integration._support.wire import Reply, Request, Wire, wire_server +from pydantic import JsonValue, TypeAdapter + +pytestmark: Final = pytest.mark.shares_worker_state + +ANTHROPIC_MODEL: Final = "beta-gated-anthropic" +FOUNDRY_MODEL: Final = "beta-gated-foundry" +FOUNDRY_DROP_PARAMS_MODEL: Final = "beta-gated-foundry-drop-params" +CLEAR_AT_BETA: Final = "mid-conversation-system-clear-at-2026-08-21" +_JSON_OBJECT: Final = TypeAdapter(dict[str, JsonValue]) +_NONCE: Final = re.compile(rb"nonce-([0-9a-f]{32})") + + +def nonce() -> str: + return f"nonce-{uuid.uuid4().hex}" + + +def claude_reply(request: Request) -> Reply: + match: Final = _NONCE.search(request.body) + assert match is not None, request.body[:200] + identity: Final = match.group(1).decode() + return Reply( + body=json.dumps( + { + "id": f"msg_{identity}", + "type": "message", + "role": "assistant", + "model": "claude-opus-4-8", + "content": [{"type": "text", "text": f"answered {identity}"}], + "stop_reason": "end_turn", + "stop_sequence": None, + "usage": {"input_tokens": 12, "output_tokens": 3}, + } + ).encode() + ) + + +def respond(request: Request) -> Reply: + if request.method != "POST": + return Reply(body=b'{"object":"list","data":[]}') + return claude_reply(request) + + +def deployment(name: str, model: str, api_base: str, drop_params: bool = False) -> dict[str, JsonValue]: + return { + "model_name": name, + "litellm_params": { + "model": model, + "api_base": api_base, + "api_key": f"synthetic-{name}-key", + **({"drop_params": True} if drop_params else {}), + }, + } + + +def write_config(directory: Path, wire_url: str) -> Path: + path: Final = directory / f"beta-gated-{uuid.uuid4().hex}.json" + path.write_text( + json.dumps( + { + "model_list": [ + deployment(ANTHROPIC_MODEL, "anthropic/claude-opus-4-8", f"{wire_url}/anthropic"), + deployment(FOUNDRY_MODEL, "azure_ai/claude-opus-4-8", f"{wire_url}/foundry"), + deployment( + FOUNDRY_DROP_PARAMS_MODEL, "azure_ai/claude-opus-4-8", f"{wire_url}/foundry", drop_params=True + ), + ], + "general_settings": { + "master_key": "os.environ/LITELLM_MASTER_KEY", + "database_url": "os.environ/DATABASE_URL", + }, + } + ) + ) + return path + + +@dataclass(frozen=True, slots=True) +class Rig: + gateway: Gateway + wire: Wire + + def send(self, model: str, request_nonce: str) -> tuple[int, bytes]: + response: Final = self.gateway.client.post( + "/v1/messages", + json={ + "model": model, + "max_tokens": 64, + "messages": [ + {"role": "user", "content": f"hi {request_nonce}"}, + {"role": "assistant", "content": "Hello"}, + {"role": "user", "content": "how are you"}, + {"role": "system", "content": "Answer in French", "clear_at": "next_user_message"}, + ], + }, + headers={"Authorization": f"Bearer {self.gateway.key}", "anthropic-beta": CLEAR_AT_BETA}, + timeout=120, + ) + return response.status_code, response.content + + def received(self, request_nonce: str) -> Request: + requests: Final = tuple(request for request in self.wire.drain() if request_nonce.encode() in request.body) + assert len(requests) == 1, [request.target for request in requests] + return requests[0] + + +@pytest.fixture(scope="module") +def rig(tmp_path_factory: pytest.TempPathFactory) -> Iterator[Rig]: + directory: Final = tmp_path_factory.mktemp("beta-gated") + with ( + gateway_from_environment() as shared, + wire_server(respond) as wire, + owned_proxy( + shared, + directory, + {"LITELLM_LOCAL_ANTHROPIC_BETA_HEADERS": "true"}, + config=write_config(directory, wire.url), + workers=1, + ) as gateway, + ): + yield Rig(gateway, wire) + + +def reply_text(status: int, raw: bytes) -> str: + assert status == 200, raw[:500] + return "".join(str(object_value(block)["text"]) for block in list_value(_JSON_OBJECT.validate_json(raw)["content"])) + + +def beta_header(headers: Mapping[str, str]) -> str: + return ",".join(value for key, value in headers.items() if key.lower() == "anthropic-beta") + + +def system_message(body: bytes) -> dict[str, JsonValue]: + messages: Final = list_value(_JSON_OBJECT.validate_json(body)["messages"]) + system: Final = [object_value(m) for m in messages if object_value(m).get("role") == "system"] + assert len(system) == 1, messages + return system[0] + + +def test_a_drop_params_leg_whose_row_lacks_the_beta_sends_neither_the_beta_nor_clear_at(rig: Rig) -> None: + request_nonce: Final = nonce() + status, raw = rig.send(FOUNDRY_DROP_PARAMS_MODEL, request_nonce) + assert reply_text(status, raw) == f"answered {request_nonce.removeprefix('nonce-')}" + upstream: Final = rig.received(request_nonce) + assert CLEAR_AT_BETA not in beta_header(upstream.headers), upstream.headers + assert system_message(upstream.body) == {"role": "system", "content": "Answer in French"} + + +def test_a_leg_whose_row_lacks_the_beta_forwards_clear_at_as_sent_without_drop_params(rig: Rig) -> None: + request_nonce: Final = nonce() + status, raw = rig.send(FOUNDRY_MODEL, request_nonce) + assert reply_text(status, raw) == f"answered {request_nonce.removeprefix('nonce-')}" + upstream: Final = rig.received(request_nonce) + assert CLEAR_AT_BETA not in beta_header(upstream.headers), upstream.headers + assert system_message(upstream.body) == { + "role": "system", + "content": "Answer in French", + "clear_at": "next_user_message", + } + + +def test_a_leg_that_carries_the_beta_forwards_clear_at_untouched(rig: Rig) -> None: + request_nonce: Final = nonce() + status, raw = rig.send(ANTHROPIC_MODEL, request_nonce) + assert reply_text(status, raw) == f"answered {request_nonce.removeprefix('nonce-')}" + upstream: Final = rig.received(request_nonce) + assert CLEAR_AT_BETA in beta_header(upstream.headers), upstream.headers + assert system_message(upstream.body) == { + "role": "system", + "content": "Answer in French", + "clear_at": "next_user_message", + } diff --git a/tests/integration/messages_endpoint/test_guardrail_block_stream.py b/tests/integration/messages_endpoint/test_guardrail_block_stream.py new file mode 100644 index 00000000000..d487f81c757 --- /dev/null +++ b/tests/integration/messages_endpoint/test_guardrail_block_stream.py @@ -0,0 +1,129 @@ +import uuid +from collections.abc import Callable, Iterator +from dataclasses import dataclass +from pathlib import Path +from typing import Final + +import anthropic +import httpx +import pytest +import yaml +from anthropic.types import Message +from integration._support.client import JSON_OBJECT, Gateway, gateway_from_environment +from integration._support.process import graceful_stop_seconds, owned_proxy +from integration._support.wire import Reply, Request, wire_server + +pytestmark: Final = pytest.mark.timeout(2 * graceful_stop_seconds() + 120) + +_DENIAL: Final = "This request is not allowed by policy." +_CALL_ID_HEADER: Final = "x-litellm-call-id" + + +@dataclass(frozen=True, slots=True) +class _Rail: + gateway: Gateway + names: tuple[str, str] + + +@dataclass(frozen=True, slots=True) +class _Ask: + model: str + rails: tuple[str, str] + call_id: str + + +@pytest.fixture(scope="module") +def denying_rail(tmp_path_factory: pytest.TempPathFactory) -> Iterator[_Rail]: + directory: Final = tmp_path_factory.mktemp("messages-guardrail-block") + name: Final = f"messages-deny-{uuid.uuid4().hex}" + post_call_name: Final = f"messages-deny-replies-{uuid.uuid4().hex}" + stock: Final = JSON_OBJECT.validate_python(yaml.safe_load(Path("tests/integration/proxy_config.yaml").read_text())) + config: Final = directory / "messages-guardrail-block.yaml" + config.write_text( + yaml.safe_dump( + { + **stock, + "guardrails": [ + { + "guardrail_name": name, + "litellm_params": { + "guardrail": "custom_code", + "mode": "pre_call", + "default_on": False, + "custom_code": ( + f"def apply_guardrail(inputs, request_data, input_type):\n return block({_DENIAL!r})\n" + ), + }, + }, + { + "guardrail_name": post_call_name, + "litellm_params": { + "guardrail": "custom_code", + "mode": "post_call", + "default_on": False, + "custom_code": ( + "def apply_guardrail(inputs, request_data, input_type):\n" + " return block('A post-call rail scanned the block message.')\n" + ), + }, + }, + ], + } + ) + ) + with gateway_from_environment() as shared, owned_proxy(shared, directory, {}, config=config) as candidate: + yield _Rail(candidate, (name, post_call_name)) + + +def _provider_must_not_answer(request: Request) -> Reply: + return Reply(status=500, body=f"unexpected {request.method} {request.target}".encode()) + + +def _streamed(client: anthropic.Anthropic, ask: _Ask) -> tuple[Message, httpx.Headers]: + with client.messages.stream( + model=ask.model, + max_tokens=32, + messages=[{"role": "user", "content": "count from 1 to 100"}], + extra_body={"guardrails": list(ask.rails)}, + extra_headers={_CALL_ID_HEADER: ask.call_id}, + ) as stream: + return stream.get_final_message(), stream.response.headers + + +def _created(client: anthropic.Anthropic, ask: _Ask) -> tuple[Message, httpx.Headers]: + raw: Final = client.messages.with_raw_response.create( + model=ask.model, + max_tokens=32, + messages=[{"role": "user", "content": "count from 1 to 100"}], + extra_body={"guardrails": list(ask.rails)}, + extra_headers={_CALL_ID_HEADER: ask.call_id}, + ) + return raw.parse(), raw.headers + + +@pytest.mark.parametrize("send", (_streamed, _created), ids=("stream", "json")) +def test_anthropic_sdk_reads_a_pre_call_block_as_a_finished_message_past_post_call_rails( + denying_rail: _Rail, send: Callable[[anthropic.Anthropic, _Ask], tuple[Message, httpx.Headers]] +) -> None: + call_id: Final = f"messages-block-{uuid.uuid4().hex}" + with ( + wire_server(_provider_must_not_answer) as provider, + denying_rail.gateway.scenario() as scenario, + httpx.Client(timeout=30, trust_env=False) as transport, + anthropic.Anthropic( + api_key=denying_rail.gateway.key, + base_url=str(denying_rail.gateway.client.base_url).rstrip("/"), + max_retries=0, + http_client=transport, + ) as client, + ): + model: Final = scenario.model( + model="anthropic/claude-haiku-5-5", api_base=provider.url, api_key="synthetic-anthropic-key" + ) + message, headers = send(client, _Ask(model, denying_rail.names, call_id)) + sent: Final = provider.drain() + + assert [(block.type, getattr(block, "text", None)) for block in message.content] == [("text", _DENIAL)], message + assert message.stop_reason == "end_turn", message + assert headers.get(_CALL_ID_HEADER) == call_id, dict(headers) + assert sent == (), sent diff --git a/tests/integration/observability/test_guardrail_effects.py b/tests/integration/observability/test_guardrail_effects.py index 8f83287696c..537eb2bbf69 100644 --- a/tests/integration/observability/test_guardrail_effects.py +++ b/tests/integration/observability/test_guardrail_effects.py @@ -1902,11 +1902,20 @@ def test_messages_pre_call_denial_streams_message(gateway: Gateway, tmp_path: Pa ) assert response.status_code == 200, response.text assert response.headers["content-type"].startswith("text/event-stream"), response.text - lines: Final = tuple(line for line in response.text.split("\n") if line.startswith("data: ")) - assert len(lines) == 1, response.text - body: Final = json.loads(lines[0].removeprefix("data: ")) - assert body["type"] == "message" and body["role"] == "assistant", body - assert body["content"] == [{"type": "text", "text": _RESPONSES_DENIAL}], body + events: Final = tuple( + json.loads(line.removeprefix("data: ")) for line in response.text.split("\n") if line.startswith("data: ") + ) + assert [event["type"] for event in events] == [ + "message_start", + "content_block_start", + "content_block_delta", + "content_block_stop", + "message_delta", + "message_stop", + ], response.text + assert events[0]["message"]["role"] == "assistant", events[0] + assert events[2]["delta"] == {"type": "text_delta", "text": _RESPONSES_DENIAL}, events[2] + assert events[4]["delta"]["stop_reason"] == "end_turn", events[4] @pytest.mark.covers("other.observability.guardrails.responses_pre_call_denial_writes_zero_spend_row") diff --git a/tests/integration/providers/test_chat_provider_annotations_wire.py b/tests/integration/providers/test_chat_provider_annotations_wire.py new file mode 100644 index 00000000000..0b18d0837eb --- /dev/null +++ b/tests/integration/providers/test_chat_provider_annotations_wire.py @@ -0,0 +1,190 @@ +import json +import uuid +from collections.abc import Callable, Iterator +from itertools import chain +from typing import Final + +import httpx +from integration._support.client import Gateway +from integration._support.wire import Reply, Request, Wire, wire_server +from integration.caching.response_cache_case import await_entry, redis_store +from pydantic import JsonValue, TypeAdapter +from redis import Redis + +_BACKEND: Final = "z-ai/glm-5.2" +_API_KEY: Final = "synthetic-openrouter-key" +_ANSWER: Final = "The report covers revenue." +_URL_CITATION: Final[dict[str, JsonValue]] = { + "type": "url_citation", + "url_citation": { + "url": "https://example.com/report", + "title": "Report", + "start_index": 0, + "end_index": 10, + "content": "cited passage", + }, +} +_FILE_ANNOTATION: Final[dict[str, JsonValue]] = { + "type": "file", + "file": {"hash": "sha-report", "name": "report.pdf", "content": [{"type": "text", "text": "parsed page one"}]}, +} +_USAGE: Final[dict[str, JsonValue]] = {"prompt_tokens": 40, "completion_tokens": 6, "total_tokens": 46} +_JSON_OBJECT: Final = TypeAdapter(dict[str, JsonValue]) +_CHOICES: Final = TypeAdapter(list[dict[str, JsonValue]]) + + +def _completion(identity: str) -> bytes: + return json.dumps( + { + "id": identity, + "object": "chat.completion", + "created": 1, + "model": _BACKEND, + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "content": _ANSWER, + "annotations": [_URL_CITATION, _FILE_ANNOTATION], + }, + "finish_reason": "stop", + } + ], + "usage": _USAGE, + } + ).encode() + + +def _frame(frame: dict[str, JsonValue]) -> bytes: + return b"data: " + json.dumps(frame).encode() + b"\n\n" + + +def _stream(identity: str) -> Reply: + chunk: Final[dict[str, JsonValue]] = { + "id": identity, + "object": "chat.completion.chunk", + "created": 1, + "model": _BACKEND, + } + return Reply( + content_type="text/event-stream", + chunks=( + _frame({**chunk, "choices": [{"index": 0, "delta": {"role": "assistant", "content": _ANSWER}}]}), + _frame({**chunk, "choices": [{"index": 0, "delta": {"annotations": [_URL_CITATION, _FILE_ANNOTATION]}}]}), + _frame({**chunk, "choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}], "usage": _USAGE}), + b"data: [DONE]\n\n", + ), + ) + + +def _responder(identity: str) -> Callable[[Request], Reply]: + def respond(request: Request) -> Reply: + if _JSON_OBJECT.validate_json(request.body).get("stream") is True: + return _stream(identity) + return Reply(body=_completion(identity)) + + return respond + + +def _chat_body(model: str, marker: str, stream: bool) -> dict[str, JsonValue]: + return { + "model": model, + "messages": [{"role": "user", "content": f"Summarize the report {marker}"}], + "stream": stream, + } + + +def _posted(wire: Wire) -> int: + return sum(1 for request in wire.drain() if request.method == "POST") + + +def _message(response: httpx.Response) -> dict[str, JsonValue]: + assert response.status_code == 200, response.text + choices: Final = _CHOICES.validate_python(_JSON_OBJECT.validate_json(response.content)["choices"]) + return _JSON_OBJECT.validate_python(choices[0]["message"]) + + +def _assert_split(message: dict[str, JsonValue]) -> None: + assert message["content"] == _ANSWER, message + assert message["annotations"] == [_URL_CITATION], message + assert _JSON_OBJECT.validate_python(message["provider_specific_fields"])["annotations"] == [_FILE_ANNOTATION] + + +def test_file_annotation_reaches_the_caller_in_provider_specific_fields_and_survives_a_cache_hit( + gateway: Gateway, +) -> None: + marker: Final = uuid.uuid4().hex + identity: Final = f"gen-{marker}" + with wire_server(_responder(identity)) as wire, gateway.scenario() as scenario: + model: Final = scenario.model(model=f"openrouter/{_BACKEND}", api_base=wire.url, api_key=_API_KEY) + first: Final = gateway.request("POST", "/v1/chat/completions", _chat_body(model, marker, stream=False)) + _assert_split(_message(first)) + assert _posted(wire) == 1 + await_entry(redis_store(), identity) + cached: Final = gateway.request("POST", "/v1/chat/completions", _chat_body(model, marker, stream=False)) + assert cached.headers.get("x-litellm-cache-key"), dict(cached.headers) + _assert_split(_message(cached)) + assert _posted(wire) == 0 + + +def _deltas(response: httpx.Response) -> Iterator[dict[str, JsonValue]]: + for line in response.iter_lines(): + if not line.startswith("data: ") or line == "data: [DONE]": + continue + choices = _CHOICES.validate_python(_JSON_OBJECT.validate_json(line.removeprefix("data: "))["choices"]) + yield from (_JSON_OBJECT.validate_python(choice["delta"]) for choice in choices) + + +def _streamed(gateway: Gateway, body: dict[str, JsonValue]) -> tuple[dict[str, JsonValue], ...]: + with gateway.client.stream( + "POST", "/v1/chat/completions", json=body, headers={"Authorization": f"Bearer {gateway.key}"} + ) as response: + assert response.status_code == 200, response.read() + return tuple(_deltas(response)) + + +def _listed(value: JsonValue) -> list[JsonValue]: + return value if isinstance(value, list) else [] + + +def _provider_fields(delta: dict[str, JsonValue]) -> dict[str, JsonValue]: + return _JSON_OBJECT.validate_python(delta.get("provider_specific_fields") or {}) + + +def _annotations(deltas: tuple[dict[str, JsonValue], ...]) -> list[JsonValue]: + return list(chain.from_iterable(_listed(delta.get("annotations")) for delta in deltas)) + + +def _provider_annotations(deltas: tuple[dict[str, JsonValue], ...]) -> list[JsonValue]: + return list(chain.from_iterable(_listed(_provider_fields(delta).get("annotations")) for delta in deltas)) + + +def _text(deltas: tuple[dict[str, JsonValue], ...]) -> str: + return "".join(str(delta.get("content") or "") for delta in deltas) + + +def _cached_message(store: Redis, key: str) -> dict[str, JsonValue]: + raw: Final = store.get(key) + assert isinstance(raw, bytes), key + response: Final = _JSON_OBJECT.validate_json(raw)["response"] + stored: Final = _JSON_OBJECT.validate_json(response) if isinstance(response, str) else response + choices: Final = _CHOICES.validate_python(_JSON_OBJECT.validate_python(stored)["choices"]) + return _JSON_OBJECT.validate_python(choices[0]["message"]) + + +def test_streamed_annotations_reach_the_caller_as_sent_and_the_cached_stream_splits_them(gateway: Gateway) -> None: + marker: Final = uuid.uuid4().hex + identity: Final = f"gen-{marker}" + with wire_server(_responder(identity)) as wire, gateway.scenario() as scenario: + model: Final = scenario.model(model=f"openrouter/{_BACKEND}", api_base=wire.url, api_key=_API_KEY) + live: Final = _streamed(gateway, _chat_body(model, marker, stream=True)) + assert _text(live) == _ANSWER, live + assert _annotations(live) == [_URL_CITATION, _FILE_ANNOTATION], live + assert _posted(wire) == 1 + store: Final = redis_store() + _assert_split(_cached_message(store, await_entry(store, identity))) + replayed: Final = _streamed(gateway, _chat_body(model, marker, stream=True)) + assert _text(replayed) == _ANSWER, replayed + assert _provider_annotations(replayed) == [_FILE_ANNOTATION], replayed + assert _posted(wire) == 0 diff --git a/tests/integration/providers/test_pdf_input_support_wire.py b/tests/integration/providers/test_pdf_input_support_wire.py new file mode 100644 index 00000000000..b35815438f7 --- /dev/null +++ b/tests/integration/providers/test_pdf_input_support_wire.py @@ -0,0 +1,390 @@ +import json +import uuid +from collections.abc import Callable, Mapping, Sequence +from concurrent.futures import ThreadPoolExecutor +from dataclasses import dataclass +from pathlib import Path +from typing import Final + +import httpx +import pytest +from integration._support.client import Gateway, Scenario +from integration._support.openai_wire import chat_reply +from integration._support.pdf_document import ( + LETTER, + chat_body, + chat_file, + messages_body, + pdf_data_url, + pdf_document, + responses_body, + responses_input_file, +) +from integration._support.wire import Reply, Request, Wire, wire_server +from pydantic import JsonValue, TypeAdapter + +_API_KEY: Final = "synthetic-pdf-gate-key" +_ANSWER: Final = "The report covers revenue." +_FILENAME: Final = "report.pdf" +_PDF: Final = pdf_data_url((LETTER,)) +_JSON_OBJECT: Final = TypeAdapter(dict[str, JsonValue]) +_MESSAGES: Final = TypeAdapter(list[JsonValue]) +_COST_MAP_PATH: Final = Path(__file__).parents[3] / "litellm" / "model_prices_and_context_window_backup.json" + + +@dataclass(frozen=True, slots=True) +class _Entrypoint: + path: str + body: Callable[[str, str], Mapping[str, JsonValue]] + stream: bool = False + provider: str = "hosted_vllm" + + +_ENTRYPOINTS: Final = ( + pytest.param( + _Entrypoint("/v1/chat/completions", lambda model, text: chat_body(model, [chat_file(_PDF, _FILENAME)], text)), + id="chat-completions", + ), + pytest.param( + _Entrypoint( + "/v1/chat/completions", + lambda model, text: chat_body(model, [chat_file(_PDF, _FILENAME)], text, stream=True), + stream=True, + ), + id="chat-completions-stream", + ), + pytest.param( + _Entrypoint("/v1/messages", lambda model, text: messages_body(model, [pdf_document((LETTER,))], text)), + id="messages", + ), + pytest.param( + _Entrypoint( + "/v1/responses", + lambda model, text: responses_body(model, [responses_input_file(_PDF, _FILENAME)], text), + provider="lm_studio", + ), + id="responses-bridge", + ), +) + + +def _azure_ai_chat_model_pdf_input(entry: JsonValue) -> bool | None: + match entry: + case {"litellm_provider": "azure_ai", "mode": "chat", "supports_pdf_input": bool() as supports}: + return supports + case _: + return None + + +def _azure_ai_chat_model(reads_pdfs: bool) -> str: + entries: Final = _JSON_OBJECT.validate_json(_COST_MAP_PATH.read_bytes()) + matching: Final = sorted( + name for name, entry in entries.items() if _azure_ai_chat_model_pdf_input(entry) is reads_pdfs + ) + assert matching, f"the cost map marks no azure_ai chat model with supports_pdf_input {reads_pdfs}" + return matching[0] + + +def _ollama_reply(backend: str) -> Reply: + return Reply( + body=json.dumps( + { + "model": backend, + "created_at": "2026-01-01T00:00:00Z", + "message": {"role": "assistant", "content": _ANSWER}, + "done": True, + "done_reason": "stop", + "prompt_eval_count": 5, + "eval_count": 3, + } + ).encode() + ) + + +def _responder(backend: str) -> Callable[[Request], Reply]: + def respond(request: Request) -> Reply: + if request.target.endswith("/api/chat"): + return _ollama_reply(backend) + body: Final = _JSON_OBJECT.validate_json(request.body) + return chat_reply(f"chatcmpl-{uuid.uuid4().hex}", backend, _ANSWER, stream=body.get("stream") is True) + + return respond + + +def _post(gateway: Gateway, path: str, body: Mapping[str, JsonValue]) -> httpx.Response: + return gateway.request("POST", path, body, headers={"anthropic-version": "2023-06-01"}) + + +def _post_streaming(gateway: Gateway, path: str, body: Mapping[str, JsonValue]) -> httpx.Response: + with gateway.client.stream( + "POST", path, json=dict(body), headers={"Authorization": f"Bearer {gateway.key}"} + ) as response: + response.read() + return response + + +def _send(gateway: Gateway, entrypoint: _Entrypoint, model: str, text: str) -> httpx.Response: + body: Final = entrypoint.body(model, text) + if entrypoint.stream: + return _post_streaming(gateway, entrypoint.path, body) + return _post(gateway, entrypoint.path, body) + + +def _error_message(response: httpx.Response) -> str: + match _JSON_OBJECT.validate_json(response.content): + case {"error": {"message": str() as message}}: + return message + case _: + raise AssertionError(response.text) + + +def _assert_refused_before_the_provider(response: httpx.Response, wire: Wire, backend: str) -> None: + assert response.status_code == 400, response.text + message: Final = _error_message(response) + assert "PDF" in message and backend in message, message + assert wire.drain() == () + + +def _bodies(wire: Wire) -> tuple[dict[str, JsonValue], ...]: + return tuple(_JSON_OBJECT.validate_json(request.body) for request in wire.drain() if request.method == "POST") + + +def _only_body(wire: Wire) -> dict[str, JsonValue]: + bodies: Final = _bodies(wire) + assert len(bodies) == 1, bodies + return bodies[0] + + +def _assert_reaches_the_provider(gateway: Gateway, wire: Wire, model: str) -> None: + response: Final = _post(gateway, "/v1/chat/completions", chat_body(model, [], f"text only {uuid.uuid4().hex}")) + assert response.status_code == 200, response.text + assert len(_bodies(wire)) == 1 + + +def _openai_compatible( + scenario: Scenario, + wire: Wire, + backend: str, + model_info: Mapping[str, JsonValue], + provider: str = "hosted_vllm", + **parameters: JsonValue, +) -> str: + return scenario.model( + model=f"{provider}/{backend}", + api_base=f"{wire.url}/v1", + api_key=_API_KEY, + model_info=model_info, + **parameters, + ) + + +def _backend() -> str: + return f"pdf-gate-{uuid.uuid4().hex[:12]}" + + +@pytest.mark.parametrize("entrypoint", _ENTRYPOINTS) +def test_pdf_to_a_deployment_marked_unable_to_read_it_is_refused_before_the_provider( + gateway: Gateway, entrypoint: _Entrypoint +) -> None: + backend: Final = _backend() + with wire_server(_responder(backend)) as wire, gateway.scenario() as scenario: + model: Final = _openai_compatible(scenario, wire, backend, {"supports_pdf_input": False}, entrypoint.provider) + response: Final = _send(gateway, entrypoint, model, f"Summarize the report {uuid.uuid4().hex}") + _assert_refused_before_the_provider(response, wire, backend) + _assert_reaches_the_provider(gateway, wire, model) + + +def _base_model_deployment(scenario: Scenario, wire: Wire) -> tuple[str, str]: + backend: Final = f"pdf-gate-{uuid.uuid4().hex[:12]}" + model: Final = scenario.model( + model=f"azure_ai/{backend}", + api_base=wire.url, + api_key=_API_KEY, + model_info={"base_model": _azure_ai_chat_model(reads_pdfs=False)}, + ) + return model, backend + + +def _litellm_params_base_model_deployment(scenario: Scenario, wire: Wire) -> tuple[str, str]: + backend: Final = _backend() + model: Final = scenario.model( + model=f"azure_ai/{backend}", + api_base=wire.url, + api_key=_API_KEY, + base_model=_azure_ai_chat_model(reads_pdfs=False), + ) + return model, backend + + +def _cost_map_deployment(scenario: Scenario, wire: Wire) -> tuple[str, str]: + marked: Final = _azure_ai_chat_model(reads_pdfs=False) + return scenario.model(model=marked, api_base=wire.url, api_key=_API_KEY), marked.removeprefix("azure_ai/") + + +def _ollama_deployment(scenario: Scenario, wire: Wire) -> tuple[str, str]: + backend: Final = _backend() + return scenario.model(model=f"ollama_chat/{backend}", api_base=wire.url, api_key=None), backend + + +@pytest.mark.parametrize( + "deploy", + [ + pytest.param(_base_model_deployment, id="model-info-base-model"), + pytest.param(_litellm_params_base_model_deployment, id="litellm-params-base-model"), + pytest.param(_cost_map_deployment, id="cost-map"), + pytest.param(_ollama_deployment, id="ollama"), + ], +) +def test_pdf_to_a_model_its_declaration_marks_unable_is_refused_before_the_provider( + gateway: Gateway, deploy: Callable[[Scenario, Wire], tuple[str, str]] +) -> None: + with wire_server(_responder("scripted")) as wire, gateway.scenario() as scenario: + model, backend = deploy(scenario, wire) + response: Final = _post( + gateway, + "/v1/chat/completions", + chat_body(model, [chat_file(_PDF, _FILENAME)], f"Summarize the report {uuid.uuid4().hex}"), + ) + _assert_refused_before_the_provider(response, wire, backend) + _assert_reaches_the_provider(gateway, wire, model) + + +def _pdf_turns(question: str) -> list[JsonValue]: + return [ + {"role": "user", "content": [chat_file(_PDF, _FILENAME), {"type": "text", "text": question}]}, + {"role": "assistant", "content": "It is a quarterly report."}, + {"role": "user", "content": [chat_file(_PDF, "appendix.pdf")]}, + {"role": "user", "content": "Who wrote it?"}, + ] + + +def _answer(response: httpx.Response) -> JsonValue: + match _JSON_OBJECT.validate_json(response.content): + case {"choices": [{"message": {"content": content}}]}: + return content + case _: + raise AssertionError(response.text) + + +def _turns_without_pdfs(question: str) -> list[JsonValue]: + return [ + {"role": "user", "content": [{"type": "text", "text": question}]}, + {"role": "assistant", "content": "It is a quarterly report."}, + {"role": "user", "content": "Who wrote it?"}, + ] + + +@pytest.mark.parametrize( + ("deployment_params", "request_params"), + [ + pytest.param({"drop_params": True}, {}, id="deployment-drop-params"), + pytest.param({}, {"drop_params": True}, id="request-drop-params"), + ], +) +def test_drop_params_sends_the_conversation_without_the_pdfs_a_model_cannot_read( + gateway: Gateway, deployment_params: Mapping[str, JsonValue], request_params: Mapping[str, JsonValue] +) -> None: + backend: Final = _backend() + question: Final = f"Summarize the report {uuid.uuid4().hex}" + with wire_server(_responder(backend)) as wire, gateway.scenario() as scenario: + model: Final = _openai_compatible(scenario, wire, backend, {"supports_pdf_input": False}, **deployment_params) + response: Final = _post( + gateway, + "/v1/chat/completions", + {"model": model, "max_tokens": 64, "messages": _pdf_turns(question), **request_params}, + ) + assert response.status_code == 200, response.text + assert _answer(response) == _ANSWER + assert _only_body(wire)["messages"] == _turns_without_pdfs(question) + + +def test_drop_params_refuses_a_request_left_with_no_user_turn_once_the_pdf_is_dropped(gateway: Gateway) -> None: + backend: Final = _backend() + with wire_server(_responder(backend)) as wire, gateway.scenario() as scenario: + model: Final = _openai_compatible(scenario, wire, backend, {"supports_pdf_input": False}, drop_params=True) + response: Final = _post( + gateway, + "/v1/chat/completions", + { + "model": model, + "max_tokens": 64, + "messages": [ + {"role": "system", "content": f"Answer from the attachment {uuid.uuid4().hex}"}, + {"role": "user", "content": [chat_file(_PDF, _FILENAME)]}, + ], + }, + ) + _assert_refused_before_the_provider(response, wire, backend) + _assert_reaches_the_provider(gateway, wire, model) + + +def _file_and_question(question: str) -> list[JsonValue]: + return [{"role": "user", "content": [chat_file(_PDF, _FILENAME), {"type": "text", "text": question}]}] + + +@pytest.mark.parametrize( + ("model_info", "deployment_params", "keeps_pdf"), + [ + pytest.param({}, {}, True, id="undeclared"), + pytest.param({}, {"drop_params": True}, False, id="undeclared-drop-params"), + pytest.param({"supports_pdf_input": True}, {"drop_params": True}, True, id="declared-drop-params"), + pytest.param( + {}, + {"drop_params": True, "base_model": _azure_ai_chat_model(reads_pdfs=True)}, + True, + id="base-model-reads-drop-params", + ), + ], +) +def test_azure_ai_model_outside_the_cost_map_gets_the_pdf_unless_drop_params_finds_no_declaration( + gateway: Gateway, model_info: Mapping[str, JsonValue], deployment_params: Mapping[str, JsonValue], keeps_pdf: bool +) -> None: + backend: Final = _backend() + question: Final = f"Summarize the report {uuid.uuid4().hex}" + with wire_server(_responder(backend)) as wire, gateway.scenario() as scenario: + model: Final = scenario.model( + model=f"azure_ai/{backend}", + api_base=wire.url, + api_key=_API_KEY, + model_info=model_info, + **deployment_params, + ) + response: Final = _post( + gateway, "/v1/chat/completions", chat_body(model, [chat_file(_PDF, _FILENAME)], question) + ) + assert response.status_code == 200, response.text + expected: Final[list[JsonValue]] = ( + _file_and_question(question) if keeps_pdf else [{"role": "user", "content": question}] + ) + assert _only_body(wire)["messages"] == expected + + +def _sibling_pdf_request(gateway: Gateway, model: str) -> httpx.Response: + return _post( + gateway, + "/v1/chat/completions", + chat_body(model, [chat_file(_PDF, _FILENAME)], f"Summarize the report {uuid.uuid4().hex}"), + ) + + +def _forwarded_file_parts(bodies: Sequence[dict[str, JsonValue]]) -> tuple[JsonValue, ...]: + return tuple(_MESSAGES.validate_python(body["messages"])[0] for body in bodies) + + +def test_pdf_opt_out_on_one_deployment_leaves_its_sibling_on_the_same_model_reading_pdfs(gateway: Gateway) -> None: + backend: Final = _backend() + burst: Final = 6 + with wire_server(_responder(backend)) as wire, gateway.scenario() as scenario: + sibling: Final = _openai_compatible(scenario, wire, backend, {}) + marked: Final = _openai_compatible(scenario, wire, backend, {"supports_pdf_input": False}) + with ThreadPoolExecutor(max_workers=burst) as pool: + responses: Final = tuple(pool.map(lambda _: _sibling_pdf_request(gateway, sibling), range(burst))) + assert [response.status_code for response in responses] == [200] * burst, [r.text for r in responses] + forwarded: Final = _forwarded_file_parts(_bodies(wire)) + assert len(forwarded) == burst, forwarded + assert all( + _MESSAGES.validate_python(_JSON_OBJECT.validate_python(message)["content"])[0] == chat_file(_PDF, _FILENAME) + for message in forwarded + ), forwarded + refused: Final = _sibling_pdf_request(gateway, marked) + _assert_refused_before_the_provider(refused, wire, backend) diff --git a/tests/integration/routing/test_auto_router_management.py b/tests/integration/routing/test_auto_router_management.py index 5aed328ba7b..442dae4d689 100644 --- a/tests/integration/routing/test_auto_router_management.py +++ b/tests/integration/routing/test_auto_router_management.py @@ -9,7 +9,7 @@ import httpx import pytest import yaml from integration._support.client import JSON_OBJECT, Gateway, eventually, object_value, string_value, until_asserted -from integration._support.database import read_rows, scratch_database +from integration._support.database import read_rows, scratch_database, write_rows from integration._support.process import owned_proxy from integration._support.wire import Reply, Request, Wire, wire_server from pydantic import JsonValue, TypeAdapter @@ -639,3 +639,25 @@ def test_auto_router_session_tracks_key_scoped_spend_and_bounds_long_session_ids ), response.text assert tuple(request for request in complex_tier.drain() if request.method == "POST") == (), response.text assert tuple(request for request in baseline.drain() if request.method == "POST") == (), response.text + + +@pytest.mark.parametrize("api_key,expected", [("unattributed", 2.0), ("raw:unattributed", 4.0)]) +def test_benchmarks_preserve_literal_key_identity(gateway: Gateway, api_key: str, expected: float) -> None: + user_id: Final = f"benchmark-key-{uuid.uuid4().hex}" + for key, savings in (("", 3.0), ("unattributed", 2.0), ("raw:unattributed", 4.0)): + write_rows( + 'INSERT INTO "LiteLLM_DailyUserSpend" ' + "(id, user_id, date, api_key, model, autorouter_savings_spend, updated_at) " + "VALUES (%s, %s, '2026-09-09', %s, 'model', %s, now())", + (str(uuid.uuid4()), user_id, key, str(savings)), + ) + try: + response: Final = gateway.request( + "GET", + "/auto_router/benchmarks", + params={"start_date": "2026-09-09", "end_date": "2026-09-09", "user_id": user_id, "api_key": api_key}, + ) + assert response.status_code == 200, response.text + assert object_value(object_value(response.json())["totals"])["saved_spend"] == expected + finally: + write_rows('DELETE FROM "LiteLLM_DailyUserSpend" WHERE user_id = %s', (user_id,)) diff --git a/tests/integration/routing/test_non_chat_model_endpoint_gate.py b/tests/integration/routing/test_non_chat_model_endpoint_gate.py new file mode 100644 index 00000000000..465f5801692 --- /dev/null +++ b/tests/integration/routing/test_non_chat_model_endpoint_gate.py @@ -0,0 +1,1515 @@ +import asyncio +import contextlib +import json +import uuid +from collections.abc import Iterator, Mapping +from dataclasses import dataclass +from pathlib import Path +from types import MappingProxyType +from typing import Final + +import httpx +import openai +import pytest +import websockets +import yaml +from integration._support.client import ( + JSON_OBJECT, + Gateway, + Scenario, + eventually, + gateway_from_environment, + held, + list_value, + object_value, + string_value, +) +from integration._support import responses_ws_peer +from integration._support.database import read_rows +from integration._support.openai_wire import answering_model_discovery, chat_reply, responses_reply +from integration._support.process import OwnedState, graceful_stop_seconds, owned_proxy_process +from integration._support.tls import write_self_signed_cert +from integration._support.wire import Reply, Request, Wire, wire_server +from pydantic import JsonValue, TypeAdapter +from websockets.exceptions import ConnectionClosed + +_PROVIDER_KEY: Final = "integration-provider-key" +_EMBEDDING_MODEL: Final = "text-embedding-3-small" +_CHAT_MODEL: Final = "gpt-4o-mini" +_CHAT_BACKEND_PREFIX: Final = "gate-chat-backend-" +_SERVED_TEXT: Final = "served by the chat upstream" +_VECTOR: Final = (0.25, 0.5, 0.75) +_COUNTED_TOKENS: Final = 17 +_CLOSE: Final = MappingProxyType({"connection": "close"}) +_RELOAD_SECONDS: Final = 5 +_EMBED_ONLY: Final[Mapping[str, JsonValue]] = MappingProxyType( + {"mode": "embedding", "supported_endpoints": ["/v1/embeddings"], "enforce_supported_endpoints": True} +) + + +def _embedding_reply() -> Reply: + return Reply( + body=json.dumps( + { + "object": "list", + "data": [{"object": "embedding", "index": 0, "embedding": list(_VECTOR)}], + "model": _EMBEDDING_MODEL, + "usage": {"prompt_tokens": 3, "total_tokens": 3}, + } + ).encode() + ) + + +def _not_a_chat_model() -> Reply: + return Reply( + status=404, + body=json.dumps( + {"error": {"message": "scripted: not a chat model", "type": "invalid_request_error", "code": None}} + ).encode(), + ) + + +def _text_completion_reply() -> Reply: + return Reply( + body=json.dumps( + { + "id": f"cmpl-{uuid.uuid4().hex}", + "object": "text_completion", + "created": 1, + "model": _CHAT_MODEL, + "choices": [{"index": 0, "text": _SERVED_TEXT, "finish_reason": "stop", "logprobs": None}], + "usage": {"prompt_tokens": 3, "completion_tokens": 3, "total_tokens": 6}, + } + ).encode() + ) + + +def _serves_chat(model: JsonValue) -> bool: + return model == _CHAT_MODEL or (isinstance(model, str) and model.startswith(_CHAT_BACKEND_PREFIX)) + + +def _respond(request: Request) -> Reply: + path: Final = request.target.split("?")[0] + body: Final = JSON_OBJECT.validate_json(request.body or b"{}") + streamed: Final = body.get("stream") is True + if path == "/v1/embeddings": + return _embedding_reply() + if path == "/v1/responses/input_tokens": + return Reply(body=json.dumps({"object": "response.input_tokens", "input_tokens": _COUNTED_TOKENS}).encode()) + if not _serves_chat(body.get("model")): + return _not_a_chat_model() + if path == "/v1/chat/completions": + return chat_reply(f"chatcmpl-{uuid.uuid4().hex}", _CHAT_MODEL, _SERVED_TEXT, stream=streamed) + if path in ("/v1/responses", "/v1/responses/compact"): + return responses_reply(f"resp_{uuid.uuid4().hex}", _CHAT_MODEL, _SERVED_TEXT, stream=streamed) + if path == "/v1/completions": + return _text_completion_reply() + return _not_a_chat_model() + + +class _Upstream: + def __init__(self, wire: Wire) -> None: + self.wire = wire + self.seen: tuple[Request, ...] = () + + def calls(self, marker: str) -> tuple[Request, ...]: + self.seen = (*self.seen, *self.wire.drain()) + return tuple(request for request in self.seen if marker.encode() in request.body or marker in request.target) + + def targets(self, marker: str) -> tuple[str, ...]: + return tuple(request.target for request in self.calls(marker)) + + +@dataclass(frozen=True, slots=True) +class _Deployment: + litellm_model: str + declarations: tuple[Mapping[str, JsonValue], ...] + + +def _single(declaration: Mapping[str, JsonValue], litellm_model: str = f"openai/{_CHAT_MODEL}") -> _Deployment: + return _Deployment(litellm_model, (declaration,)) + + +_SHARED: Final[Mapping[str, _Deployment]] = MappingProxyType( + { + "embedding_only": _single(_EMBED_ONLY, f"openai/{_EMBEDDING_MODEL}"), + "embedding_only_second": _single(_EMBED_ONLY, f"openai/{_EMBEDDING_MODEL}"), + "image_generation": _single( + {"enforce_supported_endpoints": True, "supported_endpoints": ["/v1/images/generations"]} + ), + "chat_target": _single({"mode": "chat"}), + "chat_rate_limited": _single({"mode": "chat"}), + "mode_not_a_string": _single( + {"mode": 7, "supported_endpoints": ["/v1/embeddings"]}, "openai/gate-mode-not-a-string" + ), + } +) + + +@dataclass(frozen=True, slots=True) +class _Rig: + gateway: Gateway + upstream: _Upstream + names: Mapping[str, str] + api_base: str + + +def _create(gateway: Gateway, name: str, litellm_params: Mapping[str, JsonValue], info: Mapping[str, JsonValue]) -> str: + created: Final = gateway.post( + "/model/new", + { + "model_name": name, + "litellm_params": {"api_key": _PROVIDER_KEY, **litellm_params}, + "model_info": dict(info), + }, + ) + return string_value(object_value(created["model_info"])["id"]) + + +def _deploy(scenario: Scenario, api_base: str, label: str, deployment: _Deployment) -> tuple[str, tuple[str, ...]]: + name: Final = f"gate-{label}-{uuid.uuid4().hex[:12]}" + identities: Final = tuple( + _create(scenario.gateway, name, {"model": deployment.litellm_model, "api_base": api_base}, declaration) + for declaration in deployment.declarations + ) + tuple(scenario.cleanups.callback(scenario.delete_model, identity) for identity in identities) + return name, identities + + +def _listed_ids(gateway: Gateway) -> frozenset[str]: + listed: Final = gateway.request("GET", "/model/info", headers=_CLOSE) + assert listed.status_code == 200, listed.text + return frozenset( + string_value(object_value(object_value(entry)["model_info"])["id"]) + for entry in list_value(JSON_OBJECT.validate_json(listed.content)["data"]) + ) + + +def _served_by_every_worker(gateway: Gateway, identities: frozenset[str]) -> None: + held( + lambda: identities <= _listed_ids(gateway), + lambda served: served, + holding=_RELOAD_SECONDS * 2, + seconds=_RELOAD_SECONDS * 12, + ) + + +@pytest.fixture(scope="module") +def rig() -> Iterator[_Rig]: + with ( + wire_server(answering_model_discovery(_respond)) as wire, + gateway_from_environment() as gateway, + gateway.scenario() as scenario, + ): + api_base: Final = wire.url + "/v1" + deployed: Final = {label: _deploy(scenario, api_base, label, spec) for label, spec in _SHARED.items()} + _served_by_every_worker( + gateway, frozenset(identity for _name, identities in deployed.values() for identity in identities) + ) + yield _Rig( + gateway, + _Upstream(wire), + MappingProxyType({label: name for label, (name, _ids) in deployed.items()}), + api_base, + ) + + +def _marker() -> str: + return f"gate-{uuid.uuid4().hex}" + + +def _openai_error(endpoint: str, model: str, supported: str) -> dict[str, JsonValue]: + return { + "error": { + "message": f"{endpoint} is not supported by model={model}. Supported endpoints: {supported}", + "type": "invalid_request_error", + "param": "model", + "code": "400", + } + } + + +def _anthropic_error(model: str, supported: str) -> dict[str, JsonValue]: + return { + "type": "error", + "error": { + "type": "invalid_request_error", + "message": f"/v1/messages is not supported by model={model}. Supported endpoints: {supported}", + }, + } + + +def _chat_body(model: str, marker: str, *, stream: bool = False) -> dict[str, JsonValue]: + return {"model": model, "messages": [{"role": "user", "content": marker}], **({"stream": True} if stream else {})} + + +def _failure_row(call_id: str) -> dict[str, JsonValue]: + rows: Final = eventually( + lambda: read_rows('SELECT request_id, status FROM "LiteLLM_SpendLogs" WHERE request_id = %s', (call_id,)), + lambda found: len(found) >= 1, + seconds=70, + ) + assert len(rows) == 1, rows + return rows[0] + + +def _assert_error(response: httpx.Response, expected: dict[str, JsonValue]) -> None: + assert response.status_code == 400, response.text + body: Final = JSON_OBJECT.validate_json(response.content) + error: Final = object_value(body["error"]) + message: Final = string_value(error["message"]).split("\n\n", 1)[0] + assert {**body, "error": {**error, "message": message}} == expected, response.text + + +def _assert_rejected_before( + upstream: _Upstream, response: httpx.Response, marker: str, expected: dict[str, JsonValue] +) -> None: + _assert_error(response, expected) + assert upstream.calls(marker) == (), upstream.targets(marker) + assert _failure_row(response.headers["x-litellm-call-id"])["status"] == "failure" + + +def _assert_rejected(rig: _Rig, response: httpx.Response, marker: str, expected: dict[str, JsonValue]) -> None: + _assert_rejected_before(rig.upstream, response, marker, expected) + + +@dataclass(frozen=True, slots=True) +class _Route: + path: str + endpoint: str + anthropic_shape: bool + stream: bool + model_in_path: bool + shape: str + + +_ROUTES: Final[Mapping[str, _Route]] = MappingProxyType( + { + "interactions": _Route("/v1beta/interactions", "/v1beta/interactions", False, False, False, "interactions"), + "chat_v1": _Route("/v1/chat/completions", "/v1/chat/completions", False, False, False, "chat"), + "chat_v1_stream": _Route("/v1/chat/completions", "/v1/chat/completions", False, True, False, "chat"), + "chat_bare": _Route("/chat/completions", "/v1/chat/completions", False, False, False, "chat"), + "chat_engines": _Route("/engines/{model}/chat/completions", "/v1/chat/completions", False, False, True, "chat"), + "chat_azure_deployments": _Route( + "/openai/deployments/{model}/chat/completions", "/v1/chat/completions", False, False, True, "chat" + ), + "messages": _Route("/v1/messages", "/v1/messages", True, False, False, "messages"), + "messages_stream": _Route("/v1/messages", "/v1/messages", True, True, False, "messages"), + "responses_v1": _Route("/v1/responses", "/v1/responses", False, False, False, "responses"), + "responses_v1_stream": _Route("/v1/responses", "/v1/responses", False, True, False, "responses"), + "responses_bare": _Route("/responses", "/v1/responses", False, False, False, "responses"), + "cursor_responses_body": _Route("/cursor/chat/completions", "/v1/responses", False, False, False, "cursor"), + "cursor_chat_body": _Route("/cursor/chat/completions", "/v1/chat/completions", False, False, False, "chat"), + } +) + + +def _route_body(route: _Route, model: str, marker: str) -> dict[str, JsonValue]: + shaped: Final[Mapping[str, dict[str, JsonValue]]] = { + "chat": {"messages": [{"role": "user", "content": marker}]}, + "messages": {"max_tokens": 16, "messages": [{"role": "user", "content": marker}]}, + "responses": {"input": marker}, + "interactions": {"input": marker}, + "cursor": {"input": [{"role": "user", "content": marker}]}, + } + return { + **({} if route.model_in_path else {"model": model}), + **shaped[route.shape], + **({"stream": True} if route.stream else {}), + } + + +def _send(gateway: Gateway, route: _Route, model: str, marker: str) -> httpx.Response: + path: Final = route.path.format(model=model) + body: Final = _route_body(route, model, marker) + if not route.stream: + return gateway.request("POST", path, body) + with gateway.client.stream("POST", path, json=body, headers={"Authorization": f"Bearer {gateway.key}"}) as streamed: + streamed.read() + return streamed + + +@pytest.mark.parametrize("route", tuple(_ROUTES)) +def test_chat_shaped_route_rejects_an_embedding_only_model_before_the_upstream(rig: _Rig, route: str) -> None: + spec: Final = _ROUTES[route] + model: Final = rig.names["embedding_only"] + marker: Final = _marker() + response: Final = _send(rig.gateway, spec, model, marker) + expected: Final = ( + _anthropic_error(model, "/v1/embeddings") + if spec.anthropic_shape + else _openai_error(spec.endpoint, model, "/v1/embeddings") + ) + _assert_rejected(rig, response, marker, expected) + + +def _sdk_embedding(rig: _Rig, model: str, marker: str) -> tuple[tuple[float, ...], str]: + async def call() -> tuple[tuple[float, ...], str]: + async with openai.AsyncOpenAI( + base_url=f"{rig.gateway.client.base_url}/v1", api_key=rig.gateway.key, max_retries=0 + ) as sdk: + raw: Final = await sdk.embeddings.with_raw_response.create( + model=model, input=marker, encoding_format="float" + ) + parsed: Final = raw.parse() + return tuple(parsed.data[0].embedding), raw.headers["x-litellm-call-id"] + + return asyncio.run(call()) + + +def _httpx_embedding(rig: _Rig, model: str, marker: str) -> tuple[tuple[float, ...], str]: + response: Final = rig.gateway.request( + "POST", "/v1/embeddings", {"model": model, "input": marker, "encoding_format": "float"} + ) + assert response.status_code == 200, response.text + data: Final = list_value(JSON_OBJECT.validate_json(response.content)["data"]) + vector: Final = list_value(object_value(data[0])["embedding"]) + return tuple(float(str(value)) for value in vector), response.headers["x-litellm-call-id"] + + +@pytest.mark.parametrize("client", ("httpx_sync", "openai_async_sdk")) +def test_embeddings_still_reach_the_embedding_only_model(rig: _Rig, client: str) -> None: + model: Final = rig.names["embedding_only"] + marker: Final = _marker() + vector, call_id = ( + _httpx_embedding(rig, model, marker) if client == "httpx_sync" else _sdk_embedding(rig, model, marker) + ) + assert vector == _VECTOR + assert rig.upstream.targets(marker) == ("/v1/embeddings",) + row: Final = eventually( + lambda: read_rows( + 'SELECT status, call_type FROM "LiteLLM_SpendLogs" WHERE request_id = %s OR litellm_call_id = %s', + (call_id, call_id), + ), + lambda found: len(found) >= 1, + seconds=70, + ) + assert [(entry["status"], entry["call_type"]) for entry in row] == [("success", "aembedding")], row + + +def _assert_served(rig: _Rig, response: httpx.Response, marker: str, target: str) -> None: + assert response.status_code == 200, response.text + assert _SERVED_TEXT in response.text, response.text + assert rig.upstream.targets(marker) == (target,) + + +def test_key_alias_to_an_embedding_only_model_is_rejected_under_the_resolved_name(rig: _Rig) -> None: + target: Final = rig.names["embedding_only"] + alias: Final = f"gate-alias-{uuid.uuid4().hex[:12]}" + marker: Final = _marker() + with rig.gateway.scenario() as scenario: + key: Final = scenario.key(aliases={alias: target}) + response: Final = rig.gateway.request("POST", "/v1/chat/completions", _chat_body(alias, marker), key=key) + _assert_rejected(rig, response, marker, _openai_error("/v1/chat/completions", target, "/v1/embeddings")) + + +async def _websocket_first_frame_and_close( + gateway: Gateway, key: str, model: str, marker: str +) -> tuple[dict[str, JsonValue], int | None, str | None]: + url: Final = str(gateway.client.base_url).rstrip("/").replace("http://", "ws://", 1) + "/v1/responses" + async with websockets.connect(url, additional_headers={"Authorization": f"Bearer {key}"}, open_timeout=10) as ws: + await ws.send(json.dumps({"type": "response.create", "model": model, "input": marker})) + first: Final = JSON_OBJECT.validate_json(await asyncio.wait_for(ws.recv(), 30)) + with contextlib.suppress(ConnectionClosed): + await asyncio.wait_for(ws.recv(), 30) + return first, ws.close_code, ws.close_reason + + +def _websocket_rejection(model: str) -> tuple[dict[str, JsonValue], int | None, str | None]: + return ( + { + "type": "error", + "status": 400, + "error": { + "type": "invalid_request_error", + "message": f"/v1/responses is not supported by model={model}. Supported endpoints: /v1/embeddings", + }, + }, + 1008, + "Pre-call error", + ) + + +@pytest.mark.parametrize("aliased", [False, True]) +def test_responses_websocket_closes_on_an_embedding_only_model_before_the_upstream(rig: _Rig, aliased: bool) -> None: + target: Final = rig.names["embedding_only"] + alias: Final = f"gate-ws-alias-{uuid.uuid4().hex[:12]}" + marker: Final = _marker() + with rig.gateway.scenario() as scenario: + key: Final = scenario.key(aliases={alias: target}) + closed: Final = asyncio.run( + _websocket_first_frame_and_close(rig.gateway, key, alias if aliased else target, marker) + ) + assert closed == _websocket_rejection(target) + assert rig.upstream.calls(marker) == () + + +_RESERVING_KEYS: Final[Mapping[str, Mapping[str, JsonValue]]] = MappingProxyType( + { + "max_parallel_requests": {"max_parallel_requests": 1}, + "tpm_limit": {"tpm_limit": 100, "default_estimated_output_tokens": 80}, + } +) + + +@pytest.mark.parametrize("limit", tuple(_RESERVING_KEYS)) +def test_responses_websocket_rejections_leave_the_key_reservation_free(rig: _Rig, limit: str) -> None: + target: Final = rig.names["embedding_only"] + markers: Final = tuple(_marker() for _ in range(3)) + served_marker: Final = _marker() + with rig.gateway.scenario() as scenario: + key: Final = scenario.key(**_RESERVING_KEYS[limit]) + closed: Final = tuple( + asyncio.run(_websocket_first_frame_and_close(rig.gateway, key, target, marker)) for marker in markers + ) + assert closed == (_websocket_rejection(target),) * 3 + served: Final = rig.gateway.request( + "POST", "/v1/chat/completions", _chat_body(rig.names["chat_target"], served_marker), key=key + ) + _assert_served(rig, served, served_marker, "/v1/chat/completions") + assert [rig.upstream.calls(marker) for marker in markers] == [()] * 3 + + +def test_team_scoped_embedding_only_model_is_rejected_under_its_public_name(rig: _Rig) -> None: + public: Final = f"gate-team-{uuid.uuid4().hex[:12]}" + marker: Final = _marker() + with rig.gateway.scenario() as scenario: + team: Final = scenario.team() + identity: Final = _create( + rig.gateway, + public, + {"model": f"openai/{_EMBEDDING_MODEL}", "api_base": rig.api_base}, + {**_EMBED_ONLY, "team_id": team}, + ) + scenario.cleanups.callback(scenario.delete_model, identity) + key: Final = scenario.key(team_id=team) + _served_by_every_worker(rig.gateway, frozenset({identity})) + embedded: Final = rig.gateway.request( + "POST", "/v1/embeddings", {"model": public, "input": marker, "encoding_format": "float"}, key=key + ) + assert embedded.status_code == 200, embedded.text + assert rig.upstream.targets(marker) == ("/v1/embeddings",) + rejected_marker: Final = _marker() + response: Final = rig.gateway.request( + "POST", "/v1/chat/completions", _chat_body(public, rejected_marker), key=key + ) + _assert_rejected( + rig, response, rejected_marker, _openai_error("/v1/chat/completions", public, "/v1/embeddings") + ) + + +def test_wildcard_embedding_only_deployment_rejects_chat_for_any_matched_model(rig: _Rig) -> None: + prefix: Final = f"gate-wild-{uuid.uuid4().hex[:12]}" + requested: Final = f"{prefix}/{_EMBEDDING_MODEL}" + marker: Final = _marker() + with rig.gateway.scenario() as scenario: + identity: Final = _create( + rig.gateway, f"{prefix}/*", {"model": "openai/*", "api_base": rig.api_base}, _EMBED_ONLY + ) + scenario.cleanups.callback(scenario.delete_model, identity) + _served_by_every_worker(rig.gateway, frozenset({identity})) + response: Final = rig.gateway.request("POST", "/v1/chat/completions", _chat_body(requested, marker)) + _assert_rejected(rig, response, marker, _openai_error("/v1/chat/completions", requested, "/v1/embeddings")) + + +def test_key_without_access_gets_the_access_error_and_learns_nothing_about_endpoints(rig: _Rig) -> None: + marker: Final = _marker() + with rig.gateway.scenario() as scenario: + key: Final = scenario.key(models=[rig.names["chat_target"]]) + response: Final = rig.gateway.request( + "POST", "/v1/chat/completions", _chat_body(rig.names["embedding_only"], marker), key=key + ) + assert response.status_code == 403, response.text + assert object_value(JSON_OBJECT.validate_json(response.content)["error"])["type"] == "key_model_access_denied", ( + response.text + ) + assert "Supported endpoints" not in response.text, response.text + assert rig.upstream.calls(marker) == () + + +def test_unknown_model_keeps_the_invalid_model_error(rig: _Rig) -> None: + marker: Final = _marker() + response: Final = rig.gateway.request( + "POST", "/v1/chat/completions", _chat_body(f"gate-missing-{uuid.uuid4().hex}", marker) + ) + assert response.status_code == 400, response.text + assert "Invalid model name" in response.text and "Supported endpoints" not in response.text, response.text + assert rig.upstream.calls(marker) == () + + +def test_request_fallbacks_answer_for_an_embedding_only_primary(rig: _Rig) -> None: + model: Final = rig.names["embedding_only"] + marker: Final = _marker() + response: Final = rig.gateway.request( + "POST", "/v1/chat/completions", {**_chat_body(model, marker), "fallbacks": [rig.names["chat_target"]]} + ) + assert response.status_code == 200, response.text + assert _SERVED_TEXT in response.text, response.text + assert rig.upstream.targets(marker) == ("/v1/chat/completions",) + + +def test_an_empty_request_fallback_list_keeps_the_rejection(rig: _Rig) -> None: + model: Final = rig.names["embedding_only"] + marker: Final = _marker() + response: Final = rig.gateway.request( + "POST", "/v1/chat/completions", {**_chat_body(model, marker), "fallbacks": []} + ) + _assert_rejected(rig, response, marker, _openai_error("/v1/chat/completions", model, "/v1/embeddings")) + + +def test_fallbacks_that_cannot_answer_chat_keep_the_rejection(rig: _Rig) -> None: + model: Final = rig.names["embedding_only"] + marker: Final = _marker() + fallbacks: Final = [rig.names["image_generation"], model] + response: Final = rig.gateway.request( + "POST", "/v1/chat/completions", {**_chat_body(model, marker), "fallbacks": fallbacks} + ) + _assert_rejected(rig, response, marker, _openai_error("/v1/chat/completions", model, "/v1/embeddings")) + + +def test_request_fallbacks_do_not_rescue_an_unknown_primary(rig: _Rig) -> None: + marker: Final = _marker() + response: Final = rig.gateway.request( + "POST", + "/v1/chat/completions", + {**_chat_body(f"gate-missing-{uuid.uuid4().hex}", marker), "fallbacks": [rig.names["chat_target"]]}, + ) + assert response.status_code == 400, response.text + assert "Invalid model name" in response.text, response.text + assert rig.upstream.calls(marker) == () + + +def _chat(rig: _Rig, model: str, marker: str, *, key: str | None = None, **extra: JsonValue) -> httpx.Response: + return rig.gateway.request("POST", "/v1/chat/completions", {**_chat_body(model, marker), **extra}, key=key) + + +def _assert_rejected_chat(rig: _Rig, response: httpx.Response, marker: str, model: str) -> None: + _assert_rejected(rig, response, marker, _openai_error("/v1/chat/completions", model, "/v1/embeddings")) + + +def test_a_fallback_chain_that_reaches_chat_two_hops_away_answers(rig: _Rig) -> None: + first, second, chat = rig.names["embedding_only"], rig.names["embedding_only_second"], rig.names["chat_target"] + marker: Final = _marker() + response: Final = _chat(rig, first, marker, fallbacks=[{first: [second]}, {second: [chat]}]) + assert response.status_code == 200, response.text + assert _SERVED_TEXT in response.text, response.text + assert rig.upstream.targets(marker) == ("/v1/chat/completions",) + + +def test_a_fallback_chain_that_loops_between_embedding_only_groups_keeps_the_rejection(rig: _Rig) -> None: + first, second = rig.names["embedding_only"], rig.names["embedding_only_second"] + marker: Final = _marker() + response: Final = _chat(rig, first, marker, fallbacks=[{first: [second]}, {second: [first]}]) + _assert_rejected_chat(rig, response, marker, first) + + +def test_a_specific_chain_that_loops_beats_a_catch_all_that_could_answer(rig: _Rig) -> None: + first, second = rig.names["embedding_only"], rig.names["embedding_only_second"] + marker: Final = _marker() + response: Final = _chat( + rig, first, marker, fallbacks=[{"*": [rig.names["chat_target"]]}, {first: [second]}, {second: [first]}] + ) + _assert_rejected_chat(rig, response, marker, first) + + +def test_disable_fallbacks_keeps_the_rejection_over_request_fallbacks(rig: _Rig) -> None: + model: Final = rig.names["embedding_only"] + marker: Final = _marker() + response: Final = _chat(rig, model, marker, fallbacks=[rig.names["chat_target"]], disable_fallbacks=True) + _assert_rejected_chat(rig, response, marker, model) + + +def _assert_fallback_answered( + upstream: _Upstream, response: httpx.Response, marker: str, target: str = "/v1/chat/completions" +) -> None: + assert response.status_code == 200, response.text + assert _SERVED_TEXT in response.text, response.text + assert upstream.targets(marker) == (target,), response.text + + +def _through_a_group_with_no_deployments(rig: _Rig) -> Mapping[str, JsonValue]: + ghost: Final = f"gate-ghost-{uuid.uuid4().hex}" + return {"fallbacks": [{rig.names["embedding_only"]: [ghost]}, {ghost: [rig.names["chat_target"]]}]} + + +def test_request_fallbacks_naming_a_chat_backend_name_answer_for_an_embedding_only_primary(rig: _Rig) -> None: + model: Final = rig.names["embedding_only"] + backend: Final = f"openai/{_CHAT_BACKEND_PREFIX}{uuid.uuid4().hex[:12]}" + marker: Final = _marker() + with rig.gateway.scenario() as scenario: + identity: Final = _create( + rig.gateway, f"gate-backend-{uuid.uuid4().hex[:12]}", {"model": backend, "api_base": rig.api_base}, {} + ) + scenario.cleanups.callback(scenario.delete_model, identity) + _served_by_every_worker(rig.gateway, frozenset({identity})) + response: Final = _chat(rig, model, marker, fallbacks=[{model: [backend]}]) + _assert_fallback_answered(rig.upstream, response, marker) + + +@pytest.mark.parametrize("level", ("key", "team")) +def test_router_settings_fallbacks_through_a_group_with_no_deployments_answer(rig: _Rig, level: str) -> None: + marker: Final = _marker() + with rig.gateway.scenario() as scenario: + settings: Final = {"router_settings": dict(_through_a_group_with_no_deployments(rig))} + key: Final = scenario.key(**settings) if level == "key" else scenario.key(team_id=scenario.team(**settings)) + response: Final = _chat(rig, rig.names["embedding_only"], marker, key=key) + _assert_fallback_answered(rig.upstream, response, marker) + + +def test_team_router_settings_fallbacks_answer_for_an_embedding_only_primary(rig: _Rig) -> None: + model, chat = rig.names["embedding_only"], rig.names["chat_target"] + marker: Final = _marker() + with rig.gateway.scenario() as scenario: + team: Final = scenario.team(router_settings={"fallbacks": [{model: [chat]}]}) + response: Final = _chat(rig, model, marker, key=scenario.key(team_id=team)) + assert response.status_code == 200, response.text + assert _SERVED_TEXT in response.text, response.text + assert rig.upstream.targets(marker) == ("/v1/chat/completions",) + + +def test_key_router_settings_fallbacks_answer_for_an_embedding_only_primary(rig: _Rig) -> None: + model, chat = rig.names["embedding_only"], rig.names["chat_target"] + marker: Final = _marker() + with rig.gateway.scenario() as scenario: + key: Final = scenario.key(router_settings={"fallbacks": [{model: [chat]}]}) + response: Final = _chat(rig, model, marker, key=key) + assert response.status_code == 200, response.text + assert _SERVED_TEXT in response.text, response.text + assert rig.upstream.targets(marker) == ("/v1/chat/completions",) + + +def test_key_router_settings_that_cannot_answer_override_team_fallbacks_that_could(rig: _Rig) -> None: + model: Final = rig.names["embedding_only"] + marker: Final = _marker() + with rig.gateway.scenario() as scenario: + team: Final = scenario.team(router_settings={"fallbacks": [{model: [rig.names["chat_target"]]}]}) + key: Final = scenario.key( + team_id=team, router_settings={"fallbacks": [{model: [rig.names["embedding_only_second"]]}]} + ) + response: Final = _chat(rig, model, marker, key=key) + _assert_rejected_chat(rig, response, marker, model) + + +def test_an_empty_request_fallback_list_overrides_key_router_settings_fallbacks(rig: _Rig) -> None: + model: Final = rig.names["embedding_only"] + marker: Final = _marker() + with rig.gateway.scenario() as scenario: + key: Final = scenario.key(router_settings={"fallbacks": [{model: [rig.names["chat_target"]]}]}) + response: Final = _chat(rig, model, marker, key=key, fallbacks=[]) + _assert_rejected_chat(rig, response, marker, model) + + +def test_local_rate_limit_fallback_skips_an_embedding_only_hop(rig: _Rig) -> None: + limited, chat, model = rig.names["chat_rate_limited"], rig.names["chat_target"], rig.names["embedding_only"] + first, second = _marker(), _marker() + with rig.gateway.scenario() as scenario: + key: Final = scenario.key( + model_rpm_limit={limited: 1}, router_settings={"fallbacks": [{limited: [model, chat]}]} + ) + served_first: Final = _chat(rig, limited, first, key=key) + served_second: Final = _chat(rig, limited, second, key=key) + assert (served_first.status_code, served_first.headers.get("x-litellm-model-group")) == (200, limited), ( + served_first.text + ) + assert (served_second.status_code, served_second.headers.get("x-litellm-model-group")) == (200, chat), ( + served_second.text + ) + assert (rig.upstream.targets(first), rig.upstream.targets(second)) == ( + ("/v1/chat/completions",), + ("/v1/chat/completions",), + ) + + +_COMPACT_PATHS: Final = ("/v1/responses/compact", "/responses/compact", "/openai/v1/responses/compact") + + +def _compact_body(model: str, marker: str) -> dict[str, JsonValue]: + return {"model": model, "input": [{"role": "user", "content": marker}]} + + +@pytest.mark.parametrize("path", _COMPACT_PATHS) +def test_responses_compact_rejects_an_embedding_only_model_before_the_upstream(rig: _Rig, path: str) -> None: + model: Final = rig.names["embedding_only"] + marker: Final = _marker() + response: Final = rig.gateway.request("POST", path, _compact_body(model, marker)) + _assert_rejected(rig, response, marker, _openai_error("/v1/responses/compact", model, "/v1/embeddings")) + + +def test_responses_compact_keeps_reaching_the_upstream_for_a_chat_model(rig: _Rig) -> None: + marker: Final = _marker() + response: Final = rig.gateway.request( + "POST", "/v1/responses/compact", _compact_body(rig.names["chat_target"], marker) + ) + _assert_served(rig, response, marker, "/v1/responses/compact") + + +def test_count_tokens_keeps_counting_for_an_embedding_only_model(rig: _Rig) -> None: + marker: Final = _marker() + response: Final = rig.gateway.request( + "POST", + "/v1/messages/count_tokens", + {"model": rig.names["embedding_only"], "messages": [{"role": "user", "content": marker}]}, + ) + assert response.status_code == 200, response.text + assert JSON_OBJECT.validate_json(response.content)["input_tokens"] == _COUNTED_TOKENS, response.text + assert rig.upstream.targets(marker) == ("/v1/responses/input_tokens",) + + +def test_text_completions_reject_an_embedding_only_model_before_the_upstream(rig: _Rig) -> None: + model: Final = rig.names["embedding_only"] + marker: Final = _marker() + response: Final = rig.gateway.request("POST", "/v1/completions", {"model": model, "prompt": marker}) + _assert_rejected(rig, response, marker, _openai_error("/v1/completions", model, "/v1/embeddings")) + + +@pytest.mark.parametrize("method", ("generateContent", "streamGenerateContent")) +def test_gemini_routes_reject_an_embedding_only_model_before_the_upstream(rig: _Rig, method: str) -> None: + model: Final = rig.names["embedding_only"] + marker: Final = _marker() + response: Final = rig.gateway.request( + "POST", f"/v1beta/models/{model}:{method}", {"contents": [{"role": "user", "parts": [{"text": marker}]}]} + ) + assert response.status_code == 400, response.text + assert f"{method} is not supported by model={model}. Supported endpoints: /v1/embeddings" in response.text + assert rig.upstream.calls(marker) == (), rig.upstream.targets(marker) + + +def test_repeated_rejections_log_one_failure_row_per_call(rig: _Rig) -> None: + model: Final = rig.names["embedding_only"] + marker: Final = _marker() + responses: Final = tuple( + rig.gateway.request("POST", "/v1/chat/completions", _chat_body(model, marker)) for _ in range(3) + ) + expected: Final = _openai_error("/v1/chat/completions", model, "/v1/embeddings") + for response in responses: + _assert_error(response, expected) + call_ids: Final = tuple(item.headers["x-litellm-call-id"] for item in responses) + assert len(set(call_ids)) == 3, call_ids + assert [_failure_row(call_id)["status"] for call_id in call_ids] == ["failure"] * 3 + assert rig.upstream.calls(marker) == () + + +def _closed_chat(rig: _Rig, model: str, marker: str) -> httpx.Response: + return rig.gateway.request("POST", "/v1/chat/completions", _chat_body(model, marker), headers=_CLOSE) + + +def test_live_declaration_update_turns_the_gate_on_and_off_on_every_worker(rig: _Rig) -> None: + chat_capable: Final[dict[str, JsonValue]] = { + "mode": "chat", + "supported_endpoints": ["/v1/chat/completions", "/v1/embeddings"], + } + marker: Final = _marker() + with rig.gateway.scenario() as scenario: + name, identities = _deploy(scenario, rig.api_base, "flip", _single(chat_capable)) + _served_by_every_worker(rig.gateway, frozenset(identities)) + served: Final = _closed_chat(rig, name, marker) + _assert_served(rig, served, marker, "/v1/chat/completions") + patched: Final = rig.gateway.request( + "PATCH", f"/model/{identities[0]}/update", {"model_info": dict(_EMBED_ONLY)} + ) + assert patched.status_code == 200, patched.text + rejected: Final = held( + lambda: _closed_chat(rig, name, _marker()), + lambda response: response.status_code == 400, + holding=_RELOAD_SECONDS * 2, + seconds=_RELOAD_SECONDS * 12, + ) + _assert_error(rejected, _openai_error("/v1/chat/completions", name, "/v1/embeddings")) + restored: Final = rig.gateway.request("PATCH", f"/model/{identities[0]}/update", {"model_info": chat_capable}) + assert restored.status_code == 200, restored.text + held( + lambda: _closed_chat(rig, name, _marker()), + lambda response: response.status_code == 200, + holding=_RELOAD_SECONDS * 2, + seconds=_RELOAD_SECONDS * 12, + ) + + +_TESTS_ROOT: Final = Path(__file__).resolve().parents[2] +_CONFIG: Final = TypeAdapter(dict[str, JsonValue]) + + +def _owned_embedding(owned: Gateway, model: str, marker: str) -> httpx.Response: + return owned.request("POST", "/v1/embeddings", {"model": model, "input": marker, "encoding_format": "float"}) + + +@dataclass(frozen=True, slots=True) +class _Routed: + gateway: Gateway + upstream: _Upstream + names: Mapping[str, str] + + +def _routed_entry(name: str, api_base: str, litellm_model: str, info: Mapping[str, JsonValue]) -> dict[str, JsonValue]: + return { + "model_name": name, + "litellm_params": {"model": litellm_model, "api_base": api_base, "api_key": _PROVIDER_KEY}, + "model_info": dict(info), + } + + +def _routed_config(directory: Path, api_base: str, names: Mapping[str, str]) -> Path: + shared: Final = _CONFIG.validate_python(yaml.safe_load((_TESTS_ROOT / "integration/proxy_config.yaml").read_text())) + config: Final = { + **shared, + "model_list": [ + _routed_entry(names["embed"], api_base, f"openai/{_EMBEDDING_MODEL}", _EMBED_ONLY), + _routed_entry(names["chat"], api_base, f"openai/{_CHAT_MODEL}", {"mode": "chat"}), + ], + "router_settings": { + **object_value(shared["router_settings"]), + "num_retries": 0, + "default_fallbacks": [names["chat"]], + "model_group_alias": {names["alias_embed"]: names["embed"], names["alias_chat"]: names["chat"]}, + }, + } + path: Final = directory / "gate-routed-config.yaml" + path.write_text(yaml.safe_dump(config, sort_keys=False)) + return path + + +@pytest.fixture(scope="module") +def routed(tmp_path_factory: pytest.TempPathFactory) -> Iterator[_Routed]: + directory: Final = tmp_path_factory.mktemp("gate-routed") + suffix: Final = uuid.uuid4().hex[:12] + names: Final = MappingProxyType( + {label: f"gate-routed-{label}-{suffix}" for label in ("embed", "chat", "alias_embed", "alias_chat")} + ) + with ( + wire_server(answering_model_discovery(_respond)) as wire, + gateway_from_environment() as rig_gateway, + owned_proxy_process( + rig_gateway, + directory, + {}, + config=_routed_config(directory, wire.url + "/v1", names), + workers=2, + state=OwnedState.SHARED_WITH_WORKER, + ) as owned, + ): + eventually( + lambda: owned.gateway.request("POST", "/v1/chat/completions", _chat_body(names["chat"], "probe")), + lambda response: response.status_code == 200, + seconds=30, + ) + yield _Routed(owned.gateway, _Upstream(wire), names) + + +_ROUTED_TIMEOUT: Final = pytest.mark.timeout(2 * graceful_stop_seconds() + 120) +_DEFAULT_FALLBACK_ROUTES: Final[Mapping[str, str]] = MappingProxyType( + {"chat_v1": "/v1/chat/completions", "messages": "/v1/responses", "responses_v1": "/v1/responses"} +) + + +@_ROUTED_TIMEOUT +@pytest.mark.parametrize("route", tuple(_DEFAULT_FALLBACK_ROUTES)) +def test_router_default_fallbacks_answer_for_an_embedding_only_group(routed: _Routed, route: str) -> None: + marker: Final = _marker() + response: Final = _send(routed.gateway, _ROUTES[route], routed.names["embed"], marker) + assert response.status_code == 200, response.text + assert _SERVED_TEXT in response.text, response.text + assert routed.upstream.targets(marker) == (_DEFAULT_FALLBACK_ROUTES[route],) + + +_OPT_OUTS: Final[Mapping[str, Mapping[str, JsonValue]]] = MappingProxyType( + { + "empty_fallbacks": {"fallbacks": []}, + "null_fallbacks": {"fallbacks": None}, + "disable_fallbacks": {"disable_fallbacks": True}, + } +) + + +@_ROUTED_TIMEOUT +@pytest.mark.parametrize("opt_out", tuple(_OPT_OUTS)) +def test_request_opt_outs_keep_the_rejection_over_router_default_fallbacks(routed: _Routed, opt_out: str) -> None: + model: Final = routed.names["embed"] + marker: Final = _marker() + response: Final = routed.gateway.request( + "POST", "/v1/chat/completions", {**_chat_body(model, marker), **_OPT_OUTS[opt_out]} + ) + _assert_rejected_before( + routed.upstream, response, marker, _openai_error("/v1/chat/completions", model, "/v1/embeddings") + ) + + +@_ROUTED_TIMEOUT +def test_model_group_alias_to_an_embedding_only_group_falls_back_to_the_default(routed: _Routed) -> None: + marker: Final = _marker() + response: Final = routed.gateway.request( + "POST", "/v1/chat/completions", _chat_body(routed.names["alias_embed"], marker) + ) + assert response.status_code == 200, response.text + assert _SERVED_TEXT in response.text, response.text + assert routed.upstream.targets(marker) == ("/v1/chat/completions",) + + +@_ROUTED_TIMEOUT +def test_model_group_alias_to_an_embedding_only_group_is_rejected_without_fallbacks(routed: _Routed) -> None: + alias: Final = routed.names["alias_embed"] + marker: Final = _marker() + response: Final = routed.gateway.request( + "POST", "/v1/chat/completions", {**_chat_body(alias, marker), "disable_fallbacks": True} + ) + _assert_rejected_before( + routed.upstream, response, marker, _openai_error("/v1/chat/completions", alias, "/v1/embeddings") + ) + + +@_ROUTED_TIMEOUT +def test_model_group_alias_to_a_chat_group_keeps_reaching_the_upstream(routed: _Routed) -> None: + marker: Final = _marker() + response: Final = routed.gateway.request( + "POST", "/v1/chat/completions", _chat_body(routed.names["alias_chat"], marker) + ) + assert response.status_code == 200, response.text + assert _SERVED_TEXT in response.text, response.text + assert routed.upstream.targets(marker) == ("/v1/chat/completions",) + + +@_ROUTED_TIMEOUT +def test_model_group_alias_to_an_embedding_only_group_keeps_serving_embeddings(routed: _Routed) -> None: + marker: Final = _marker() + response: Final = _owned_embedding(routed.gateway, routed.names["alias_embed"], marker) + assert response.status_code == 200, response.text + assert list_value( + object_value(list_value(JSON_OBJECT.validate_json(response.content)["data"])[0])["embedding"] + ) == list(_VECTOR), response.text + assert routed.upstream.targets(marker) == ("/v1/embeddings",) + + +_REWRITER: Final = """from litellm.integrations.custom_logger import CustomLogger + + +class WebSocketModelRewriter(CustomLogger): + async def async_pre_call_hook(self, user_api_key_dict, cache, data, call_type): + if call_type == "_aresponses_websocket" and data.get("model") == "__SOURCE__": + data["model"] = "__TARGET__" + return data + + +rewriter = WebSocketModelRewriter() +""" + + +@dataclass(frozen=True, slots=True) +class _Rewritten: + gateway: Gateway + upstream: _Upstream + source: str + target: str + + +def _rewritten_config(directory: Path, api_base: str, source: str, target: str, module: str) -> Path: + shared: Final = _CONFIG.validate_python(yaml.safe_load((_TESTS_ROOT / "integration/proxy_config.yaml").read_text())) + config: Final = { + **shared, + "model_list": [ + _routed_entry(source, api_base, f"openai/{_CHAT_MODEL}", {"mode": "chat"}), + _routed_entry(target, api_base, f"openai/{_EMBEDDING_MODEL}", _EMBED_ONLY), + ], + "router_settings": {**object_value(shared["router_settings"]), "num_retries": 0}, + "litellm_settings": {**object_value(shared["litellm_settings"]), "callbacks": [f"{module}.rewriter"]}, + } + path: Final = directory / "gate-rewritten-config.yaml" + path.write_text(yaml.safe_dump(config, sort_keys=False)) + return path + + +@pytest.fixture(scope="module") +def rewritten(tmp_path_factory: pytest.TempPathFactory) -> Iterator[_Rewritten]: + directory: Final = tmp_path_factory.mktemp("gate-rewritten") + suffix: Final = uuid.uuid4().hex[:12] + source: Final = f"gate-rewrite-chat-{suffix}" + target: Final = f"gate-rewrite-embed-{suffix}" + module: Final = f"gate_ws_rewriter_{suffix}" + (directory / f"{module}.py").write_text(_REWRITER.replace("__SOURCE__", source).replace("__TARGET__", target)) + with ( + wire_server(answering_model_discovery(_respond)) as wire, + gateway_from_environment() as rig_gateway, + owned_proxy_process( + rig_gateway, + directory, + {}, + config=_rewritten_config(directory, wire.url + "/v1", source, target, module), + workers=2, + state=OwnedState.SHARED_WITH_WORKER, + ) as owned, + ): + eventually( + lambda: owned.gateway.request("POST", "/v1/chat/completions", _chat_body(source, "probe")), + lambda response: response.status_code == 200, + seconds=30, + ) + yield _Rewritten(owned.gateway, _Upstream(wire), source, target) + + +@_ROUTED_TIMEOUT +@pytest.mark.parametrize("limit", tuple(_RESERVING_KEYS)) +def test_a_websocket_model_a_hook_rewrites_to_an_embedding_only_group_releases_the_key_reservation( + rewritten: _Rewritten, limit: str +) -> None: + markers: Final = tuple(_marker() for _ in range(3)) + served_marker: Final = _marker() + with rewritten.gateway.scenario() as scenario: + key: Final = scenario.key(**_RESERVING_KEYS[limit]) + closed: Final = tuple( + asyncio.run(_websocket_first_frame_and_close(rewritten.gateway, key, rewritten.source, marker)) + for marker in markers + ) + assert closed == (_websocket_rejection(rewritten.target),) * 3 + served: Final = rewritten.gateway.request( + "POST", "/v1/chat/completions", _chat_body(rewritten.source, served_marker), key=key + ) + assert served.status_code == 200, served.text + assert _SERVED_TEXT in served.text, served.text + assert [rewritten.upstream.calls(marker) for marker in markers] == [()] * 3 + assert rewritten.upstream.targets(served_marker) == ("/v1/chat/completions",) + + +@dataclass(frozen=True, slots=True) +class _Addressed: + gateway: Gateway + rig: Gateway + upstream: _Upstream + names: Mapping[str, str] + addresses: Mapping[str, str] + api_base: str + + +def _wildcard_backend_entry(name: str, api_base: str, backend: str) -> dict[str, JsonValue]: + entry: Final = _routed_entry(name, api_base, backend, _EMBED_ONLY) + return {**entry, "litellm_params": {**object_value(entry["litellm_params"]), "custom_llm_provider": "openai"}} + + +def _addressed_config( + directory: Path, api_base: str, names: Mapping[str, str], suffix: str, with_wildcards: bool +) -> Path: + shared: Final = _CONFIG.validate_python(yaml.safe_load((_TESTS_ROOT / "integration/proxy_config.yaml").read_text())) + wildcard: Final = f"gate-addr-wild-{suffix}" + wildcard_entries: Final = ( + ( + _routed_entry(f"{wildcard}/*", api_base, "openai/*", {}), + _wildcard_backend_entry(names["wildcard_backend"], api_base, f"{wildcard}/{_CHAT_MODEL}"), + ) + if with_wildcards + else () + ) + aliases: Final = {names["wildcard_alias"]: f"{wildcard}/{_CHAT_BACKEND_PREFIX}{suffix}"} if with_wildcards else {} + config: Final = { + **shared, + "model_list": [ + _routed_entry( + names["embed"], api_base, f"openai/gate-addr-backend-{suffix}", {**_EMBED_ONLY, "id": names["embed_id"]} + ), + _routed_entry(names["chat"], api_base, f"openai/{_CHAT_MODEL}", {"mode": "chat", "id": names["chat_id"]}), + _routed_entry(names["shared_embed"], api_base, f"openai/{_CHAT_MODEL}", _EMBED_ONLY), + *wildcard_entries, + _routed_entry(names["ghost_primary"], api_base, f"openai/{_EMBEDDING_MODEL}", _EMBED_ONLY), + ], + "router_settings": { + **object_value(shared["router_settings"]), + "num_retries": 0, + "model_group_alias": aliases, + "fallbacks": [{names["ghost_primary"]: [names["ghost"]]}, {names["ghost"]: [names["chat"]]}], + }, + "general_settings": { + **object_value(shared["general_settings"]), + "proxy_config_reload_interval_seconds": 3600, + }, + } + path: Final = directory / "gate-addressed-config.yaml" + path.write_text(yaml.safe_dump(config, sort_keys=False)) + return path + + +@contextlib.contextmanager +def _addressed_proxy(tmp_path_factory: pytest.TempPathFactory, with_wildcards: bool) -> Iterator[_Addressed]: + directory: Final = tmp_path_factory.mktemp("gate-addressed") + suffix: Final = uuid.uuid4().hex[:12] + names: Final = MappingProxyType( + { + label: f"gate-addr-{label}-{suffix}" + for label in ( + "embed", + "embed_id", + "chat", + "chat_id", + "shared_embed", + "wildcard_backend", + "wildcard_alias", + "ghost_primary", + "ghost", + ) + } + ) + with ( + wire_server(answering_model_discovery(_respond)) as wire, + gateway_from_environment() as rig_gateway, + owned_proxy_process( + rig_gateway, + directory, + {}, + config=_addressed_config(directory, wire.url + "/v1", names, suffix, with_wildcards), + workers=2, + state=OwnedState.SHARED_WITH_WORKER, + ) as owned, + ): + eventually( + lambda: owned.gateway.request("POST", "/v1/chat/completions", _chat_body(names["chat"], "probe")), + lambda response: response.status_code == 200, + seconds=30, + ) + yield _Addressed( + owned.gateway, + rig_gateway, + _Upstream(wire), + names, + MappingProxyType( + { + "deployment_id": names["embed_id"], + "backend_name": f"openai/gate-addr-backend-{suffix}", + "wildcard_backend_name": f"gate-addr-wild-{suffix}/{_CHAT_MODEL}", + } + ), + wire.url + "/v1", + ) + + +@pytest.fixture(scope="module") +def addressed(tmp_path_factory: pytest.TempPathFactory) -> Iterator[_Addressed]: + with _addressed_proxy(tmp_path_factory, with_wildcards=True) as proxy: + yield proxy + + +@pytest.fixture(scope="module") +def addressed_without_wildcards(tmp_path_factory: pytest.TempPathFactory) -> Iterator[_Addressed]: + with _addressed_proxy(tmp_path_factory, with_wildcards=False) as proxy: + yield proxy + + +_ADDRESSED_ROUTES: Final = ("chat_v1", "messages", "responses_v1") + + +@_ROUTED_TIMEOUT +@pytest.mark.parametrize("route", _ADDRESSED_ROUTES) +@pytest.mark.parametrize("address", ("deployment_id", "backend_name")) +def test_chat_sent_to_an_embedding_only_deployment_id_or_backend_name_is_rejected_before_the_upstream( + addressed: _Addressed, address: str, route: str +) -> None: + spec: Final = _ROUTES[route] + model: Final = addressed.addresses[address] + marker: Final = _marker() + response: Final = _send(addressed.gateway, spec, model, marker) + expected: Final = ( + _anthropic_error(model, "/v1/embeddings") + if spec.anthropic_shape + else _openai_error(spec.endpoint, model, "/v1/embeddings") + ) + _assert_rejected_before(addressed.upstream, response, marker, expected) + + +@_ROUTED_TIMEOUT +@pytest.mark.parametrize("address", ("deployment_id", "backend_name")) +def test_embeddings_sent_to_an_embedding_only_deployment_id_or_backend_name_keep_working( + addressed: _Addressed, address: str +) -> None: + marker: Final = _marker() + response: Final = _owned_embedding(addressed.gateway, addressed.addresses[address], marker) + assert response.status_code == 200, response.text + assert list_value( + object_value(list_value(JSON_OBJECT.validate_json(response.content)["data"])[0])["embedding"] + ) == list(_VECTOR), response.text + assert addressed.upstream.targets(marker) == ("/v1/embeddings",) + + +@_ROUTED_TIMEOUT +@pytest.mark.parametrize("address", ("chat_deployment_id", "backend_name_a_chat_deployment_shares")) +def test_chat_sent_to_a_chat_capable_deployment_id_or_backend_name_keeps_reaching_the_upstream( + addressed: _Addressed, address: str +) -> None: + model: Final = addressed.names["chat_id"] if address == "chat_deployment_id" else f"openai/{_CHAT_MODEL}" + marker: Final = _marker() + response: Final = addressed.gateway.request("POST", "/v1/chat/completions", _chat_body(model, marker)) + assert response.status_code == 200, response.text + assert _SERVED_TEXT in response.text, response.text + assert addressed.upstream.targets(marker) == ("/v1/chat/completions",) + + +@_ROUTED_TIMEOUT +def test_request_fallbacks_naming_a_chat_deployment_id_answer_for_an_embedding_only_primary( + addressed: _Addressed, +) -> None: + marker: Final = _marker() + response: Final = addressed.gateway.request( + "POST", + "/v1/chat/completions", + {**_chat_body(addressed.names["embed"], marker), "fallbacks": [addressed.names["chat_id"]]}, + ) + assert response.status_code == 200, response.text + assert _SERVED_TEXT in response.text, response.text + assert addressed.upstream.targets(marker) == ("/v1/chat/completions",) + + +@_ROUTED_TIMEOUT +def test_chat_sent_to_an_embedding_only_backend_name_a_wildcard_also_matches_is_served_by_the_wildcard( + addressed: _Addressed, +) -> None: + marker: Final = _marker() + response: Final = addressed.gateway.request( + "POST", "/v1/chat/completions", _chat_body(addressed.addresses["wildcard_backend_name"], marker) + ) + assert response.status_code == 200, response.text + assert _SERVED_TEXT in response.text, response.text + assert [JSON_OBJECT.validate_json(call.body)["model"] for call in addressed.upstream.calls(marker)] == [_CHAT_MODEL] + + +@_ROUTED_TIMEOUT +def test_request_fallbacks_naming_an_alias_only_a_wildcard_serves_answer_for_an_embedding_only_primary( + addressed: _Addressed, +) -> None: + marker: Final = _marker() + response: Final = addressed.gateway.request( + "POST", + "/v1/chat/completions", + {**_chat_body(addressed.names["embed"], marker), "fallbacks": [addressed.names["wildcard_alias"]]}, + ) + _assert_fallback_answered(addressed.upstream, response, marker) + + +@_ROUTED_TIMEOUT +def test_yaml_fallbacks_through_a_group_with_no_deployments_answer_for_an_embedding_only_group( + addressed: _Addressed, +) -> None: + marker: Final = _marker() + response: Final = addressed.gateway.request( + "POST", "/v1/chat/completions", _chat_body(addressed.names["ghost_primary"], marker) + ) + _assert_fallback_answered(addressed.upstream, response, marker) + + +@_ROUTED_TIMEOUT +def test_an_embedding_only_model_the_read_through_loads_from_the_database_is_rejected_on_every_call( + addressed_without_wildcards: _Addressed, +) -> None: + addressed: Final = addressed_without_wildcards + name: Final = f"gate-addr-loaded-{uuid.uuid4().hex[:12]}" + markers: Final = tuple(_marker() for _ in range(6)) + with addressed.rig.scenario() as scenario: + identity: Final = _create( + addressed.rig, name, {"model": f"openai/{_EMBEDDING_MODEL}", "api_base": addressed.api_base}, _EMBED_ONLY + ) + scenario.cleanups.callback(scenario.delete_model, identity) + responses: Final = tuple( + addressed.gateway.request("POST", "/v1/chat/completions", _chat_body(name, marker), headers=_CLOSE) + for marker in markers + ) + expected: Final = _openai_error("/v1/chat/completions", name, "/v1/embeddings") + for response in responses: + _assert_error(response, expected) + assert [addressed.upstream.calls(marker) for marker in markers] == [()] * len(markers) + + +@pytest.mark.parametrize("metadata", ({"model_group": 7}, {"model_group": ["wrong-group"]})) +def test_rejection_with_invalid_metadata_is_logged_without_blocking_embeddings( + rig: _Rig, metadata: dict[str, JsonValue] +) -> None: + marker: Final = _marker() + model: Final = rig.names["embedding_only"] + rejected: Final = _chat(rig, model, marker, metadata=metadata) + _assert_rejected_chat(rig, rejected, marker, model) + embedded_marker: Final = _marker() + embedded: Final = rig.gateway.request( + "POST", "/v1/embeddings", {"model": model, "input": embedded_marker, "encoding_format": "float"} + ) + assert embedded.status_code == 200, embedded.text + assert rig.upstream.targets(embedded_marker) == ("/v1/embeddings",) + + +def test_existing_non_chat_metadata_on_a_chat_backend_remains_advisory(rig: _Rig) -> None: + marker: Final = _marker() + with rig.gateway.scenario() as scenario: + name, identities = _deploy( + scenario, + rig.api_base, + "legacy-audio", + _single({"mode": "audio_speech", "supported_endpoints": ["/v1/audio/speech"]}), + ) + _served_by_every_worker(rig.gateway, frozenset(identities)) + response: Final = _chat(rig, name, marker) + _assert_served(rig, response, marker, "/v1/chat/completions") + + +def test_a_mixed_group_skips_its_restricted_embedding_deployment(rig: _Rig) -> None: + marker: Final = _marker() + with rig.gateway.scenario() as scenario: + name: Final = f"gate-mixed-{uuid.uuid4().hex[:12]}" + embed: Final = _create( + scenario.gateway, + name, + {"model": f"openai/{_EMBEDDING_MODEL}", "api_base": rig.api_base, "order": 0}, + _EMBED_ONLY, + ) + scenario.cleanups.callback(scenario.delete_model, embed) + chat: Final = _create( + scenario.gateway, name, {"model": f"openai/{_CHAT_MODEL}", "api_base": rig.api_base, "order": 1}, {} + ) + scenario.cleanups.callback(scenario.delete_model, chat) + _served_by_every_worker(rig.gateway, frozenset({embed, chat})) + response: Final = _chat(rig, name, marker) + _assert_served(rig, response, marker, "/v1/chat/completions") + calls: Final = rig.upstream.calls(marker) + assert [JSON_OBJECT.validate_json(call.body)["model"] for call in calls] == [_CHAT_MODEL] + + +@pytest.mark.parametrize( + "fallbacks, expected_status", + [(("embedding_only",), 429), (("embedding_only", "chat_target"), 200), (("missing",), 400)], +) +def test_local_rate_limit_skips_restricted_fallbacks_and_still_tries_unloaded_ones( + rig: _Rig, fallbacks: tuple[str, ...], expected_status: int +) -> None: + limited: Final = rig.names["chat_rate_limited"] + targets: Final = tuple(rig.names[name] if name != "missing" else _marker() for name in fallbacks) + with rig.gateway.scenario() as scenario: + key: Final = scenario.key( + model_rpm_limit={limited: 1}, router_settings={"fallbacks": [{limited: list(targets)}]} + ) + first: Final = _chat(rig, limited, _marker(), key=key) + assert first.status_code == 200, first.text + marker: Final = _marker() + second: Final = _chat(rig, limited, marker, key=key) + assert second.status_code == expected_status, second.text + if expected_status == 200: + _assert_served(rig, second, marker, "/v1/chat/completions") + return + assert "Supported endpoints" not in second.text, second.text + assert rig.upstream.calls(marker) == () + + +def _websocket_fallback_config(directory: Path, primary: tuple[str, str], fallback: tuple[str, str]) -> Path: + shared: Final = _CONFIG.validate_python(yaml.safe_load((_TESTS_ROOT / "integration/proxy_config.yaml").read_text())) + config: Final = { + **shared, + "model_list": [ + _routed_entry(primary[0], primary[1], f"openai/{_EMBEDDING_MODEL}", _EMBED_ONLY), + _routed_entry(fallback[0], fallback[1], f"openai/{_CHAT_MODEL}", {"mode": "chat"}), + ], + "router_settings": { + **object_value(shared["router_settings"]), + "num_retries": 0, + "fallbacks": [{primary[0]: [fallback[0]]}], + }, + } + path: Final = directory / "gate-websocket-fallback.yaml" + path.write_text(yaml.safe_dump(config, sort_keys=False)) + return path + + +def test_responses_websocket_uses_a_configured_chat_fallback_without_calling_the_embedding_provider( + rig: _Rig, tmp_path: Path +) -> None: + marker: Final = _marker() + suffix: Final = uuid.uuid4().hex[:12] + primary: Final = f"gate-ws-embed-{suffix}" + fallback: Final = f"gate-ws-chat-{suffix}" + certificate: Final = write_self_signed_cert(tmp_path) + with ( + responses_ws_peer.responses_peer( + certificate, lambda identity: responses_ws_peer.events(identity, _SERVED_TEXT) + ) as peer, + owned_proxy_process( + rig.gateway, + tmp_path, + {"SSL_CERT_FILE": str(certificate[0])}, + config=_websocket_fallback_config(tmp_path, (primary, f"{rig.api_base}/{marker}"), (fallback, peer.url)), + workers=2, + state=OwnedState.SHARED_WITH_WORKER, + ) as owned, + owned.gateway.scenario() as scenario, + ): + key: Final = scenario.key() + (received,) = asyncio.run( + responses_ws_peer.session(str(owned.gateway.client.base_url), key, primary, (marker,)) + ) + assert received[-1]["type"] == "response.completed", received + assert responses_ws_peer.completed(received)["status"] == "completed" + assert any(event.get("delta") == _SERVED_TEXT for event in received), received + (forwarded,) = responses_ws_peer.drain(peer.frames) + assert marker in json.dumps(forwarded), forwarded + assert rig.upstream.calls(marker) == () + + +def test_client_routing_fields_cannot_disable_an_explicit_restriction(rig: _Rig) -> None: + marker: Final = _marker() + with rig.gateway.scenario() as scenario: + model, identities = _deploy(scenario, rig.api_base, "restricted-chat", _single(_EMBED_ONLY)) + _served_by_every_worker(rig.gateway, frozenset(identities)) + response: Final = _chat( + rig, + model, + marker, + original_generic_function="caller-value", + additional_drop_params=["original_generic_function"], + ) + _assert_rejected_chat(rig, response, marker, model) + + +def test_a_compaction_endpoint_declaration_keeps_the_model_available_for_compaction(rig: _Rig) -> None: + marker: Final = _marker() + with rig.gateway.scenario() as scenario: + model, identities = _deploy( + scenario, + rig.api_base, + "compact-declaration", + _single({"supported_endpoints": ["/responses/compact"], "enforce_supported_endpoints": True}), + ) + _served_by_every_worker(rig.gateway, frozenset(identities)) + response: Final = rig.gateway.request("POST", "/responses/compact", {"model": model, "input": marker}) + _assert_served(rig, response, marker, "/v1/responses/compact") + + +@pytest.mark.parametrize("alias_source", ["key_alias", "key_router", "team_router"]) +def test_local_rate_limit_fallbacks_resolve_saved_aliases(rig: _Rig, tmp_path: Path, alias_source: str) -> None: + suffix: Final = uuid.uuid4().hex[:12] + names: Final = {label: f"gate-fallback-alias-{label}-{suffix}" for label in ("primary", "target", "alias")} + shared: Final = _CONFIG.validate_python(yaml.safe_load((_TESTS_ROOT / "integration/proxy_config.yaml").read_text())) + config: Final = { + **shared, + "model_list": [ + _routed_entry(names["primary"], rig.api_base, f"openai/{_CHAT_MODEL}", {"mode": "chat"}), + _routed_entry(names["target"], rig.api_base, f"openai/{_CHAT_MODEL}", {"mode": "chat"}), + ], + "router_settings": { + **object_value(shared["router_settings"]), + "num_retries": 0, + "fallbacks": [{names["primary"]: [names["alias"]]}], + }, + } + path: Final = tmp_path / "fallback-alias.yaml" + path.write_text(yaml.safe_dump(config, sort_keys=False)) + aliases: Final = {names["alias"]: names["target"]} + with ( + owned_proxy_process( + rig.gateway, tmp_path, {}, config=path, workers=2, state=OwnedState.SHARED_WITH_WORKER + ) as owned, + owned.gateway.scenario() as scenario, + ): + team: Final = ( + scenario.team(router_settings={"model_group_alias": aliases}) if alias_source == "team_router" else None + ) + key: Final = scenario.key( + team_id=team, + aliases=aliases if alias_source == "key_alias" else {}, + router_settings={"model_group_alias": aliases} if alias_source == "key_router" else None, + model_rpm_limit={names["primary"]: 1}, + ) + first: Final = owned.gateway.request( + "POST", "/v1/chat/completions", _chat_body(names["primary"], _marker()), key=key + ) + assert first.status_code == 200, first.text + marker: Final = _marker() + second: Final = owned.gateway.request( + "POST", "/v1/chat/completions", _chat_body(names["primary"], marker), key=key + ) + _assert_fallback_answered(rig.upstream, second, marker) diff --git a/tests/integration/sdk/test_router_silent_model_shadow_sdk.py b/tests/integration/sdk/test_router_silent_model_shadow_sdk.py index 821e4ddd0b7..be5998e205e 100644 --- a/tests/integration/sdk/test_router_silent_model_shadow_sdk.py +++ b/tests/integration/sdk/test_router_silent_model_shadow_sdk.py @@ -82,9 +82,9 @@ class _ShadowRecorder(CustomLogger): async def async_log_success_event( self, kwargs: Mapping[str, object], response_obj: object, start_time: object, end_time: object ) -> None: - metadata: Final = _OBJECT.validate_python(_OBJECT.validate_python(kwargs["litellm_params"])["metadata"] or {}) - if metadata.get("is_silent_experiment") is True: - self.shadow_groups.put(str(metadata.get("model_group"))) + payload: Final = _OBJECT.validate_python(kwargs["standard_logging_object"]) + if _OBJECT.validate_python(payload["metadata"]).get("is_silent_experiment") is True: + self.shadow_groups.put(str(payload["model_group"])) def drained(self) -> tuple[str, ...]: return tuple(self.shadow_groups.get_nowait() for _ in range(self.shadow_groups.qsize())) diff --git a/tests/integration/security/test_api_key_secret_reference.py b/tests/integration/security/test_api_key_secret_reference.py index 0b1187d2e0e..86a7d58e0d3 100644 --- a/tests/integration/security/test_api_key_secret_reference.py +++ b/tests/integration/security/test_api_key_secret_reference.py @@ -1,6 +1,9 @@ from __future__ import annotations +import base64 import json +import threading +import time import uuid from collections.abc import Callable, Iterator, Mapping, Sequence from dataclasses import dataclass @@ -46,7 +49,18 @@ _SEARCH_PATH: Final = f"/v1/vector_stores/{_VECTOR_STORE}/search" _BATCH_DEPLOYMENT: Final = "wif-vllm-batch" _BEDROCK_MODEL: Final = "bedrock/converse/us.anthropic.claude-sonnet-5-5" _PROVIDER_INPUT_TOKENS: Final = 7919 -_UNSUPPORTED_REFERENCE: Final = "oidc/google/https://example.invalid" +_UNSUPPORTED_REFERENCE: Final = "oidc/github/https://example.invalid" +_GOOGLE_MODEL: Final = "hosted_vllm/wif-google" +_GOOGLE_AUDIENCES: Final = MappingProxyType( + { + "config_inline": "https://vllm-inline.example.test", + "config_credential": "https://vllm-credential.example.test", + "db_credential": "https://vllm-db-credential.example.test", + } +) +_UNAVAILABLE_AUDIENCE: Final = "https://vllm-unavailable.example.test" +_HUNG_AUDIENCE: Final = "https://vllm-hung.example.test" +_IDENTITY_PATH: Final = "/computeMetadata/v1/instance/service-accounts/default/identity" _RUN_SECRETS_TOKEN: Final = "/run/secrets/wif-absent-token" _SOURCES: Final = ("config_credential", "config_inline", "db_credential", "db_inline") _UNREADABLE: Final = MappingProxyType( @@ -108,6 +122,45 @@ def _reference(path: Path) -> str: return f"oidc/file/{path}" +def _google_reference(audience: str) -> str: + return f"oidc/google/{audience}" + + +def _jwt_segment(part: Mapping[str, JsonValue]) -> str: + return base64.urlsafe_b64encode(json.dumps(part).encode()).decode().rstrip("=") + + +@dataclass(frozen=True, slots=True) +class _Google: + expires: int + + def token(self, audience: str) -> str: + claims: Final = _jwt_segment({"iss": "https://accounts.google.com", "aud": audience, "exp": self.expires}) + return f"{_jwt_segment({'alg': 'RS256', 'typ': 'JWT'})}.{claims}.signature" + + def bearers(self) -> frozenset[str]: + return frozenset(f"Bearer {self.token(audience)}" for audience in _GOOGLE_AUDIENCES.values()) + + +def _metadata_server(google: _Google) -> Callable[[Request], Reply]: + def respond(request: Request) -> Reply: + target: Final = httpx.URL(request.target) + audience: Final = target.params.get("audience") + if audience is None or (request.method, target.path, request.headers.get("metadata-flavor")) != ( + "GET", + _IDENTITY_PATH, + "Google", + ): + return Reply(status=404, body=b"not found", content_type="text/plain") + if audience == _UNAVAILABLE_AUDIENCE: + return Reply(status=503, body=b"metadata server unavailable", content_type="text/plain") + if audience == _HUNG_AUDIENCE: + return Reply(chunks=(b"never sent",), gate_before_first=threading.Event(), content_type="text/plain") + return Reply(body=google.token(audience).encode(), content_type="text/plain") + + return respond + + def _secrets(directory: Path) -> _Secrets: allowed: Final = directory / "secrets" outside_dir: Final = directory / "outside" @@ -169,9 +222,9 @@ def _served(request: Request) -> Reply: return openai_error(404) -def _provider(secrets: _Secrets) -> Callable[[Request], Reply]: +def _provider(secrets: _Secrets, google: _Google) -> Callable[[Request], Reply]: def respond(request: Request) -> Reply: - if request.headers.get("authorization") != f"Bearer {secrets.current()}": + if request.headers.get("authorization") not in {f"Bearer {secrets.current()}", *google.bearers()}: return openai_error(401) return _served(request) @@ -224,6 +277,27 @@ def _config(directory: Path, secrets: _Secrets, provider: str) -> Path: "aws_bedrock_runtime_endpoint": provider.removesuffix("/v1"), }, ), + _deployment( + "wif-google-inline", + { + "model": _GOOGLE_MODEL, + "api_key": _google_reference(_GOOGLE_AUDIENCES["config_inline"]), + "api_base": provider, + }, + ), + _deployment("wif-google-credential", _named(_GOOGLE_MODEL, "wif-google")), + _deployment( + "wif-google-unavailable", + {"model": _GOOGLE_MODEL, "api_key": _google_reference(_UNAVAILABLE_AUDIENCE), "api_base": provider}, + ), + _deployment( + "wif-google-unavailable-count", + {"model": _CHAT_MODEL, "api_key": _google_reference(_UNAVAILABLE_AUDIENCE), "api_base": provider}, + ), + _deployment( + "wif-google-hung", + {"model": _GOOGLE_MODEL, "api_key": _google_reference(_HUNG_AUDIENCE), "api_base": provider}, + ), _deployment( "wif-mistral-passthrough", { @@ -239,6 +313,7 @@ def _config(directory: Path, secrets: _Secrets, provider: str) -> Path: _credential("wif-outside", _reference(secrets.outside), provider), _credential("wif-unsupported", _UNSUPPORTED_REFERENCE, provider), _credential("wif-run-secrets", f"oidc/file/{_RUN_SECRETS_TOKEN}", provider), + _credential("wif-google", _google_reference(_GOOGLE_AUDIENCES["config_credential"]), provider), ], "vector_store_registry": [ { @@ -312,6 +387,8 @@ class _Rig: owned: OwnedProxy provider: Recorder intruder: Recorder + metadata: Recorder + google: _Google provider_base: str intruder_base: str secrets: _Secrets @@ -339,17 +416,24 @@ def _is_discovery(request: Request) -> bool: def rig(tmp_path_factory: pytest.TempPathFactory) -> Iterator[_Rig]: directory: Final = tmp_path_factory.mktemp("api_key_secret_reference").resolve() secrets: Final = _secrets(directory) + google: Final = _Google(expires=int(time.time()) + 3600) with ( gateway_from_environment() as gateway, - wire_server(_provider(secrets)) as provider_wire, + wire_server(_provider(secrets, google)) as provider_wire, wire_server(_intruder) as intruder_wire, + wire_server(_metadata_server(google)) as metadata_wire, ): provider: Final = Recorder(provider_wire) provider_base: Final = f"{provider_wire.url}/v1" with owned_proxy_process( gateway, directory, - {**secrets.overrides(), "MISTRAL_API_BASE": provider_wire.url}, + { + **secrets.overrides(), + "MISTRAL_API_BASE": provider_wire.url, + "GCE_METADATA_HOST": httpx.URL(metadata_wire.url).netloc.decode(), + "GCE_METADATA_TIMEOUT": "1", + }, config=_config(directory, secrets, provider_base), workers=2, healthcheck=WorkerHealthcheck.LENIENT, @@ -361,6 +445,8 @@ def rig(tmp_path_factory: pytest.TempPathFactory) -> Iterator[_Rig]: owned=owned, provider=provider, intruder=Recorder(intruder_wire), + metadata=Recorder(metadata_wire), + google=google, provider_base=provider_base, intruder_base=f"{intruder_wire.url}/v1", secrets=secrets, @@ -708,7 +794,92 @@ def test_team_row_naming_the_credential_sends_the_token_only_to_its_host(rig: _R def test_startup_discovery_sends_the_boot_token(rig: _Rig) -> None: sent: Final = frozenset(_bearers(tuple(request for request in rig.boot if _is_discovery(request)))) - assert sent == {f"Bearer {rig.boot_token}"}, sent + assert sent - rig.google.bearers() == {f"Bearer {rig.boot_token}"}, sent + + +def _discovery_bearers(rig: _Rig) -> frozenset[str | None]: + return frozenset(_bearers(tuple(request for request in rig.provider.requests() if _is_discovery(request)))) + + +def test_startup_discovery_sends_the_google_token(rig: _Rig) -> None: + expected: Final = frozenset( + f"Bearer {rig.google.token(_GOOGLE_AUDIENCES[source])}" for source in ("config_inline", "config_credential") + ) + sent: Final = eventually( + partial(_discovery_bearers, rig), lambda seen: expected <= seen, seconds=30, return_last_on_timeout=True + ) + assert expected <= sent, sent + + +def _google_model(rig: _Rig, scenario: Scenario, source: str) -> str: + match source: + case "config_inline": + return "wif-google-inline" + case "config_credential": + return "wif-google-credential" + case "db_credential": + credential: Final = f"db-google-{uuid.uuid4().hex}" + rig.owned.gateway.post( + "/credentials", + _credential(credential, _google_reference(_GOOGLE_AUDIENCES[source]), rig.provider_base), + ) + scenario.cleanups.callback(_delete_credential, rig.owned.gateway, credential) + name, identity = _created_model( + rig.owned.gateway, + scenario, + _deployment(f"db-google-{uuid.uuid4().hex}", _named(_GOOGLE_MODEL, credential)), + ) + eventually(partial(rig.owned.serves_only_on_every_worker, name, identity), bool, seconds=120) + return name + case _: + pytest.fail(f"unknown source {source}") + + +def _asked_audiences(rig: _Rig) -> frozenset[str | None]: + return frozenset(httpx.URL(request.target).params.get("audience") for request in rig.metadata.requests()) + + +@pytest.mark.parametrize("source", tuple(_GOOGLE_AUDIENCES)) +def test_google_reference_sends_the_metadata_server_token_for_its_audience(rig: _Rig, source: str) -> None: + audience: Final = _GOOGLE_AUDIENCES[source] + with rig.owned.gateway.scenario() as scenario: + marker: Final = _marker() + response: Final = _chat(rig.owned.gateway, _google_model(rig, scenario, source), marker) + sent: Final = _bearers(_carrying(rig.provider.requests(), marker)) + assert (response.status_code, sent) == (200, (f"Bearer {rig.google.token(audience)}",)), response.text + assert audience in _asked_audiences(rig) + + +def test_google_reference_fails_closed_when_the_metadata_server_fails(rig: _Rig) -> None: + marker: Final = _marker() + response: Final = _chat(rig.owned.gateway, "wif-google-unavailable", marker) + reached: Final = _carrying(rig.provider.requests(), marker) + assert (response.status_code, reached) == (503, ()), (response.text, _bearers(reached)) + assert "oidc/google secret reference could not be resolved" in response.text, response.text + assert _UNAVAILABLE_AUDIENCE.removeprefix("https://") not in response.text, response.text + assert _UNAVAILABLE_AUDIENCE in _asked_audiences(rig) + + +def test_google_reference_answers_503_when_the_metadata_server_never_finishes_its_reply(rig: _Rig) -> None: + marker: Final = _marker() + response: Final = _chat(rig.owned.gateway, "wif-google-hung", marker) + reached: Final = _carrying(rig.provider.requests(), marker) + assert (response.status_code, reached) == (503, ()), (response.text, _bearers(reached)) + assert "the metadata server did not return an ID token" in response.text, response.text + assert _HUNG_AUDIENCE in _asked_audiences(rig) + + +def test_input_token_count_answers_503_when_the_metadata_server_fails(rig: _Rig) -> None: + marker: Final = _marker() + response: Final = rig.owned.gateway.request( + "POST", + "/v1/responses/input_tokens", + {"model": "wif-google-unavailable-count", "input": f"say {marker}"}, + headers=_CLOSE, + ) + reached: Final = _carrying(rig.provider.requests(), marker) + assert (response.status_code, reached) == (503, ()), (response.text, _bearers(reached)) + assert "oidc/google secret reference could not be resolved" in response.text, response.text def _side_call(rig: _Rig, seam: str, marker: str) -> tuple[httpx.Response, Callable[[Request], bool]]: diff --git a/tests/integration/spend/test_daily_activity_aggregated_breakdowns.py b/tests/integration/spend/test_daily_activity_aggregated_breakdowns.py index 1c315a6e694..ff60cb9d174 100644 --- a/tests/integration/spend/test_daily_activity_aggregated_breakdowns.py +++ b/tests/integration/spend/test_daily_activity_aggregated_breakdowns.py @@ -1,3 +1,5 @@ +import csv +import io import uuid from collections.abc import Sequence from datetime import datetime, timedelta @@ -12,6 +14,7 @@ from tests.integration._support.database import write_rows _URL: Final = "/user/daily/activity/aggregated" _RESULTS: Final = TypeAdapter(list[dict[str, JsonValue]]) +_NUMBER: Final = TypeAdapter(float) def _unique_day() -> str: @@ -43,6 +46,181 @@ def _row_id() -> str: return f"agg-{uuid.uuid4().hex}" +@pytest.mark.parametrize("suffix", ["/aggregated", ""]) +def test_missing_attribution_reconciles_and_filters_without_key_collisions(gateway: Gateway, suffix: str) -> None: + day: Final = _unique_day() + rows: Final = ( + ("", None, None, 3.0), + ("", "", "", 0.0), + ("known-key", "unknown-model", None, -1.0), + ("unattributed", "known-model", "", 2.0), + ("raw:unattributed", "known-model", "named-group", 4.0), + ("known-key", "", "unknown-model-group", 5.0), + ) + _seed( + day, + [ + (_row_id(), "bucket-user", day, key, model, group, "", "tool", "/mcp", 10, spend, 1, 1) + for key, model, group, spend in rows + ], + ) + params: Final = {"start_date": day, "end_date": day} + url: Final = f"/user/daily/activity{suffix}" + try: + response: Final = gateway.request("GET", url, params=params) + assert response.status_code == 200, response.text + body: Final = object_value(response.json()) + results: Final = _RESULTS.validate_python(body["results"]) + assert len(results) == 1 + totals: Final = object_value(results[0]["metrics"]) + assert totals["spend"] == 13.0 + assert totals["api_requests"] == 6 + breakdown: Final = object_value(results[0]["breakdown"]) + for dimension in ("models", "model_groups", "api_keys", "providers", "mcp_servers", "endpoints"): + buckets: Final = _RESULTS.validate_python(list(object_value(breakdown[dimension]).values())) + for metric in ("spend", "api_requests", "prompt_tokens"): + assert ( + sum(_NUMBER.validate_python(object_value(bucket["metrics"])[metric]) for bucket in buckets) + == totals[metric] + ) + models: Final = object_value(breakdown["models"]) + assert set(models) == {"known-model", "unknown-model"} + assert object_value(object_value(models["unknown-model"])["metrics"])["api_requests"] == 4 + groups: Final = object_value(breakdown["model_groups"]) + assert set(groups) == {"unknown-model-group", "unknown-model", "known-model", "named-group"} + assert object_value(object_value(groups["unknown-model-group"])["metrics"])["spend"] == 8.0 + keys: Final = object_value(breakdown["api_keys"]) + assert set(keys) == {"unattributed", "raw:unattributed", "raw:raw:unattributed", "known-key"} + unknown_key: Final = object_value(keys["unattributed"]) + assert object_value(unknown_key["metadata"]) == { + "key_alias": "Unattributed", + "key_exists": False, + "team_id": None, + "user_id": None, + "user_email": None, + } + for dimension in ("models", "model_groups", "providers", "mcp_servers", "endpoints"): + for bucket in _RESULTS.validate_python(list(object_value(breakdown[dimension]).values())): + nested: Final = _RESULTS.validate_python(list(object_value(bucket["api_key_breakdown"]).values())) + assert ( + sum(_NUMBER.validate_python(object_value(key["metrics"])["spend"]) for key in nested) + == object_value(bucket["metrics"])["spend"] + ) + for key, spend, count in ( + ("unattributed", 3.0, 2), + ("raw:unattributed", 2.0, 1), + ("raw:raw:unattributed", 4.0, 1), + ): + filtered: Final = gateway.request("GET", url, params={**params, "api_key": key}) + assert filtered.status_code == 200, filtered.text + metadata: Final = object_value(object_value(filtered.json())["metadata"]) + assert metadata["total_spend"] == spend + assert metadata["total_api_requests"] == count + model_response: Final = gateway.request("GET", url, params={**params, "model": "unknown-model"}) + assert model_response.status_code == 200, model_response.text + model_metadata: Final = object_value(object_value(model_response.json())["metadata"]) + assert model_metadata["total_spend"] == 7.0 + assert model_metadata["total_api_requests"] == 4 + finally: + _clean(day) + + +@pytest.mark.parametrize( + ("route", "extra", "expected"), + [ + ("keys", {}, {"unattributed": 3.0, "raw:unattributed": 2.0, "known-key": 7.0}), + ("search", {"search": "Unattributed"}, {"unattributed": 3.0, "raw:unattributed": 2.0}), + ("model_top_keys", {"model_group": "unknown-model-group"}, {"unattributed": 3.0}), + ( + "model_top_keys", + {"model_group": "unknown-model", "by_model_group": "false"}, + {"unattributed": 3.0, "raw:unattributed": 2.0}, + ), + ], +) +def test_missing_attribution_key_drilldowns( + gateway: Gateway, + route: str, + extra: dict[str, str], + expected: dict[str, float], +) -> None: + day: Final = _unique_day() + _seed( + day, + [ + (_row_id(), "bucket-user", day, "", None, None, "", None, None, 0, 3.0, 1, 1), + (_row_id(), "bucket-user", day, "unattributed", "unknown-model", "", "", None, None, 0, 2.0, 1, 1), + (_row_id(), "bucket-user", day, "known-key", "known-model", "", "", None, None, 0, 7.0, 1, 1), + ], + ) + try: + response: Final = gateway.request( + "GET", f"{_URL}/{route}", params={"start_date": day, "end_date": day, **extra} + ) + assert response.status_code == 200, response.text + rows: Final = _RESULTS.validate_python(object_value(response.json())["api_keys"]) + assert {str(row["api_key"]): object_value(row["metrics"])["spend"] for row in rows} == expected + unattributed: Final = next(row for row in rows if row["api_key"] == "unattributed") + assert object_value(unattributed["metadata"])["key_alias"] == "Unattributed" + assert object_value(unattributed["metadata"])["key_exists"] is False + finally: + _clean(day) + + +@pytest.mark.parametrize("format", ["json", "csv"]) +@pytest.mark.parametrize( + ("export_type", "field", "expected"), + [ + ("daily_with_keys", "api_key", {"unattributed": 3.0, "raw:unattributed": 2.0, "raw:raw:unattributed": 4.0}), + ("daily_with_models", "model", {"unknown-model": 5.0, "known-model": 4.0}), + ], +) +def test_export_bucket_ids_round_trip_across_streaming_batches( + gateway: Gateway, format: str, export_type: str, field: str, expected: dict[str, float] +) -> None: + day: Final = _unique_day() + _seed( + day, + [ + (_row_id(), "export-user", day, "", None, None, "", None, None, 0, 3.0, 1, 1), + (_row_id(), "export-user", day, "", "", "", "", None, None, 0, 0.0, 1, 1), + (_row_id(), "export-user", day, "unattributed", "unknown-model", "", "", None, None, 0, 2.0, 1, 1), + (_row_id(), "export-user", day, "raw:unattributed", "known-model", "", "", None, None, 0, 4.0, 1, 1), + ], + ) + write_rows( + 'INSERT INTO "LiteLLM_DailyUserSpend" (id, user_id, date, api_key, model, spend, api_requests, updated_at) ' + "SELECT %s || i, 'export-user', %s, 'raw:a-key-' || i, 'a-model-' || i, 1, 1, now() " + "FROM generate_series(1, 1001) i", + (str(uuid.uuid4()), day), + ) + params: Final = {"start_date": day, "end_date": day} + try: + response: Final = gateway.request( + "GET", "/user/daily/activity/export", params={**params, "export_type": export_type, "format": format} + ) + assert response.status_code == 200, response.text + rows: Final = ( + _RESULTS.validate_python(response.json()) + if format == "json" + else _RESULTS.validate_python(list(csv.DictReader(io.StringIO(response.text)))) + ) + buckets: Final = {str(row[field]): _NUMBER.validate_python(row["spend"]) for row in rows} + filler: Final = "raw:raw:a-key-" if field == "api_key" else "a-model-" + assert buckets == {**{f"{filler}{i}": 1.0 for i in range(1, 1002)}, **expected} + assert len(rows) == len(buckets) + assert sum(buckets.values()) == 1010.0 + for bucket, spend in expected.items(): + filtered: Final = _activity(gateway, day, **{field: bucket}) + assert object_value(filtered["metadata"])["total_spend"] == spend + if field == "api_key": + unattributed: Final = next(row for row in rows if row["api_key"] == "unattributed") + assert unattributed["key_alias"] == "Unattributed" + assert unattributed["user_id"] in (None, "") + finally: + _clean(day) + + def _ranked_key_rows(day: str, count: int) -> list[tuple[object, ...]]: return [ ( diff --git a/tests/integration/spend/test_daily_activity_routes.py b/tests/integration/spend/test_daily_activity_routes.py index 520c8ccc6d8..9bfe4bb50ce 100644 --- a/tests/integration/spend/test_daily_activity_routes.py +++ b/tests/integration/spend/test_daily_activity_routes.py @@ -451,9 +451,23 @@ def test_tag_daily_activity_scopes_internal_user_keys_and_filters( seeded_row(TAG_SPEND, "tag", "beta", digest_one_b, DAY), seeded_row(TAG_SPEND, "tag", "gamma", digest_one_a, DAY), seeded_row(TAG_SPEND, "tag", "alpha", digest_two, DAY), + seeded_row(TAG_SPEND, "tag", "alpha", "", DAY), replace(standard_model_row, model="gpt-4o"), ) with daily_rows(rows): + for suffix in ("", "/aggregated"): + bucket_params: Final = {"start_date": DAY, "end_date": DAY, "tags": "alpha", "api_key": "unattributed"} + bucket_url: Final = f"/tag/daily/activity{suffix}" + denied_bucket: Final = gateway.request("GET", bucket_url, params=bucket_params, key=key_one_a) + admin_bucket: Final = gateway.request("GET", bucket_url, params=bucket_params) + assert denied_bucket.status_code == 200, denied_bucket.text + assert admin_bucket.status_code == 200, admin_bucket.text + denied_body: Final = SpendAnalyticsPaginatedResponse.model_validate_json(denied_bucket.content) + allowed_body: Final = SpendAnalyticsPaginatedResponse.model_validate_json(admin_bucket.content) + assert denied_body.results == [] + assert denied_body.metadata.total_api_requests == 0 + assert allowed_body.metadata.total_api_requests == 1 + assert set(allowed_body.results[0].breakdown.api_keys) == {"unattributed"} scoped: Final = _tag_activity( gateway, key=key_one_a, diff --git a/tests/integration/spend/test_shutdown_flush.py b/tests/integration/spend/test_shutdown_flush.py index e96511511d9..5c2e9cf98da 100644 --- a/tests/integration/spend/test_shutdown_flush.py +++ b/tests/integration/spend/test_shutdown_flush.py @@ -1,11 +1,14 @@ +import hashlib import json import os +import re import threading +import time import uuid from collections.abc import Callable, Iterator from concurrent.futures import ThreadPoolExecutor from contextlib import contextmanager -from dataclasses import dataclass +from dataclasses import dataclass, replace from pathlib import Path from typing import Final @@ -26,6 +29,225 @@ MODEL_INSERT_ARRIVED_LOG_LINE: Final = "path=/model/new" COMMIT_DELAY_SECONDS: Final = 15 BURST_REQUESTS: Final = 30 +_GATED_LOGGER: Final = """ +import os +import re +import httpx +from litellm.integrations.custom_logger import CustomLogger + +class ShutdownLogger(CustomLogger): + async def async_log_success_event(self, kwargs, response_obj, start_time, end_time): + async with httpx.AsyncClient(timeout=25, trust_env=False) as client: + await client.post(os.environ["SHUTDOWN_CALLBACK_URL"]) + +logger = ShutdownLogger() +""" + + +def _tag_shutdown_config(directory: Path, upstream: str, redis_buffer: bool, *, gated: bool = False) -> Path: + path: Final = directory / "tag-shutdown.yaml" + if gated: + (directory / "shutdown_callbacks.py").write_text(_GATED_LOGGER) + path.write_text( + yaml.safe_dump( + { + "model_list": [ + { + "model_name": "shutdown-model", + "litellm_params": { + "model": "openai/gpt-4o-mini", + "api_base": upstream + "/v1", + "api_key": "synthetic-key", + "input_cost_per_token": 0.01, + "output_cost_per_token": 0.02, + }, + } + ], + "general_settings": { + "master_key": "os.environ/LITELLM_MASTER_KEY", + "database_url": "os.environ/DATABASE_URL", + "proxy_batch_write_at": 3600, + "use_redis_transaction_buffer": redis_buffer, + "disable_model_info_refresh": True, + }, + "litellm_settings": { + "cache": True, + "cache_params": {"type": "redis", "host": "os.environ/REDIS_HOST", "port": "os.environ/REDIS_PORT"}, + "callbacks": ["shutdown_callbacks.logger"] if gated else [], + }, + } + ) + ) + return path + + +def _tagged_chat(gateway: Gateway, key: str, tags: tuple[str, str]) -> None: + response: Final = gateway.request( + "POST", + "/v1/chat/completions", + { + "model": "shutdown-model", + "messages": [{"role": "user", "content": uuid.uuid4().hex}], + "metadata": {"tags": list(tags)}, + }, + key=key, + ) + assert response.status_code == 200, response.text + + +def _assert_shutdown_accounting(key: str, tags: tuple[str, str], requests: int) -> None: + hashed: Final = hashlib.sha256(key.encode()).hexdigest() + for table, count in (("LiteLLM_SpendLogs", "count(*)"), ("LiteLLM_DailyUserSpend", "sum(api_requests)")): + rows: Final = read_rows( + f'SELECT ({count})::int AS requests, sum(spend) AS spend FROM "{table}" WHERE api_key=%s', (hashed,) + ) + assert rows[0]["requests"] == requests, (table, rows) + assert rows[0]["spend"] == pytest.approx(requests * 0.05, abs=1e-9), (table, rows) + key_rows: Final = read_rows('SELECT spend FROM "LiteLLM_VerificationToken" WHERE token=%s', (hashed,)) + assert key_rows[0]["spend"] == pytest.approx(requests * 0.05, abs=1e-9) + for tag in tags: + tag_rows: Final = read_rows( + 'SELECT sum(api_requests)::int AS requests, sum(spend) AS spend FROM "LiteLLM_DailyTagSpend" WHERE api_key=%s AND tag=%s', + (hashed, tag), + ) + assert tag_rows[0]["requests"] == requests, (tag, tag_rows) + assert tag_rows[0]["spend"] == pytest.approx(requests * 0.05, abs=1e-9), (tag, tag_rows) + + +@pytest.mark.parametrize("redis_buffer", [False, True]) +def test_tagged_request_survives_shutdown_before_the_tag_timer( + gateway: Gateway, tmp_path: Path, redis_buffer: bool +) -> None: + tags: Final = (f"shutdown-a-{uuid.uuid4().hex}", f"shutdown-b-{uuid.uuid4().hex}") + overrides: Final = {"DATABASE_URL": os.environ["DATABASE_URL"], "LITELLM_LOG": "DEBUG"} + with wire_server(_provider) as wire: + config: Final = _tag_shutdown_config(tmp_path, wire.url, redis_buffer) + with owned_proxy_process(gateway, tmp_path, overrides, config=config) as owned: + key: Final = string_value( + owned.gateway.post("/key/generate", {"user_id": f"shutdown-{uuid.uuid4().hex}"})["key"] + ) + _tagged_chat(owned.gateway, key, tags) + owned.begin_shutdown() + owned.process.wait(timeout=90) + with owned_proxy_process(gateway, tmp_path, overrides, config=config) as restarted: + _assert_shutdown_accounting(key, tags, 1) + restarted.begin_shutdown() + restarted.process.wait(timeout=90) + _assert_shutdown_accounting(key, tags, 1) + + +@pytest.mark.parametrize("redis_buffer", [False, True]) +@pytest.mark.parametrize("overflow", [False, True]) +def test_callback_backlog_is_accounted_before_shutdown_disconnects( + gateway: Gateway, tmp_path: Path, redis_buffer: bool, overflow: bool +) -> None: + release: Final = threading.Event() + requests: Final = 6 if overflow else 3 + tags: Final = (f"backlog-a-{uuid.uuid4().hex}", f"backlog-b-{uuid.uuid4().hex}") + with ( + wire_server(_provider) as wire, + wire_server(lambda _: Reply(chunks=(b"{}",), gate_before_first=release)) as logger, + ): + config: Final = _tag_shutdown_config(tmp_path, wire.url, redis_buffer, gated=True) + with owned_proxy_process( + gateway, + tmp_path, + { + "DATABASE_URL": os.environ["DATABASE_URL"], + "LOGGING_WORKER_CONCURRENCY": "2", + "LOGGING_WORKER_MAX_QUEUE_SIZE": "2" if overflow else "50000", + "SHUTDOWN_CALLBACK_URL": logger.url, + "GRACEFUL_SHUTDOWN_TIMEOUT": "15", + "LITELLM_LOG": "DEBUG", + }, + config=config, + ) as owned: + key: Final = string_value( + owned.gateway.post("/key/generate", {"user_id": f"backlog-{uuid.uuid4().hex}"})["key"] + ) + try: + for _ in range(requests): + _tagged_chat(owned.gateway, key, tags) + eventually(logger.received.qsize, lambda count: count >= 2, seconds=10) + owned.begin_shutdown() + eventually( + lambda: ( + owned.process.poll() is not None + or "Draining success callbacks before the final spend flush" in owned.log.read_text() + ), + lambda ready: ready, + seconds=30, + ) + release.set() + owned.process.wait(timeout=90) + _assert_shutdown_accounting(key, tags, requests) + finally: + release.set() + + +@pytest.mark.parametrize("gunicorn", [False, True]) +def test_sigterm_http_drain_consumes_callback_budget(gateway: Gateway, tmp_path: Path, gunicorn: bool) -> None: + release_request: Final = threading.Event() + release_callback: Final = threading.Event() + tags: Final = (f"deadline-a-{uuid.uuid4().hex}", f"deadline-b-{uuid.uuid4().hex}") + + def held_provider(request: Request) -> Reply: + response: Final = _provider(request) + return replace(response, chunks=(response.body,), gate_before_first=release_request) + + with ( + wire_server(held_provider) as wire, + wire_server(lambda _: Reply(chunks=(b"{}",), gate_before_first=release_callback)) as logger, + ): + config: Final = _tag_shutdown_config(tmp_path, wire.url, False, gated=True) + with ( + owned_proxy_process( + gateway, + tmp_path, + { + "DATABASE_URL": os.environ["DATABASE_URL"], + "SHUTDOWN_CALLBACK_URL": logger.url, + "GRACEFUL_SHUTDOWN_TIMEOUT": "30", + "LITELLM_LOG": "DEBUG", + }, + config=config, + extra_arguments=("--run_gunicorn",) if gunicorn else (), + ) as owned, + ThreadPoolExecutor(max_workers=1) as executor, + ): + key: Final = string_value( + owned.gateway.post("/key/generate", {"user_id": f"deadline-{uuid.uuid4().hex}"})["key"] + ) + request: Final = executor.submit(_tagged_chat, owned.gateway, key, tags) + try: + eventually(wire.received.qsize, lambda count: count == 1, seconds=10) + owned.begin_shutdown() + eventually( + lambda: "graceful_shutdown_started" in owned.log.read_text(), lambda stamped: stamped, seconds=5 + ) + assert not request.done(), "The shutdown clock must start before the HTTP request drains" + stamped: Final = time.monotonic() + eventually(time.monotonic, lambda now: now - stamped >= 2, seconds=5) + held_seconds: Final = time.monotonic() - stamped + release_request.set() + request.result(timeout=10) + budget: Final = eventually( + lambda: re.search(r"remaining_s=([\d.]+) callback_timeout_s=([\d.]+)", owned.log.read_text()), + lambda match: match is not None, + seconds=15, + ) + assert budget is not None + remaining: Final = float(budget[1]) + allowance: Final = float(budget[2]) + assert remaining <= 30 - held_seconds + 0.05 + assert allowance == pytest.approx(max(0, remaining - 10), abs=0.002) + release_callback.set() + owned.process.wait(timeout=45) + _assert_shutdown_accounting(key, tags, 1) + finally: + release_request.set() + release_callback.set() + def _api_requests(table: str, column: str, identity: str) -> int: rows: Final = read_rows( diff --git a/tests/integration/streaming/test_text_completion_stream_guardrail_rebuild.py b/tests/integration/streaming/test_text_completion_stream_guardrail_rebuild.py index 229a9be6d20..ea0a820a705 100644 --- a/tests/integration/streaming/test_text_completion_stream_guardrail_rebuild.py +++ b/tests/integration/streaming/test_text_completion_stream_guardrail_rebuild.py @@ -27,7 +27,12 @@ _PROXY_CONFIG: Final = Path(__file__).resolve().parents[1] / "proxy_config.yaml" _PROVIDER_KEY: Final = "synthetic-provider-key" _GUARDRAIL: Final = "audit-text-rebuild-tool-permission" _TEXT_MODEL: Final = f"audit-text-instruct-{uuid.uuid4().hex}" +_GOVERNED_MODEL: Final = f"audit-text-governed-{uuid.uuid4().hex}" +_KEYWORD_FILTER: Final = "audit-text-keyword-filter" +_POLICY: Final = "audit-text-post-call-governance" _PIECES: Final = ("Hello", " streamed", " world") +_MASKED_PIECES: Final = ("A pers", "im", "mon tart") +_BLOCKED_PIECES: Final = ("A kum", "quat tart") _PROMPT_TOKENS: Final = 11 _COMPLETION_TOKENS: Final = 3 _USAGE: Final[dict[str, JsonValue]] = { @@ -97,6 +102,14 @@ def _text_choice(text: str, finish_reason: str | None) -> dict[str, JsonValue]: return {"text": text, "index": 0, "logprobs": None, "finish_reason": finish_reason} +def _pieces_for(identity: str) -> tuple[str, ...]: + if identity.startswith("cmpl-mask-"): + return _MASKED_PIECES + if identity.startswith("cmpl-block-"): + return _BLOCKED_PIECES + return _PIECES + + def _respond(request: Request) -> Reply: body: Final = JSON_OBJECT.validate_json(request.body) assert body.get("stream") is True, body @@ -106,7 +119,7 @@ def _respond(request: Request) -> Reply: return Reply( content_type="text/event-stream", chunks=( - *(_text_frame(identity, [_text_choice(piece, None)]) for piece in _PIECES), + *(_text_frame(identity, [_text_choice(piece, None)]) for piece in _pieces_for(identity)), _text_frame(identity, [_text_choice("", "length")]), _text_frame(identity, [], usage=_USAGE), b"data: [DONE]\n\n", @@ -114,20 +127,25 @@ def _respond(request: Request) -> Reply: ) +def _text_deployment(model_name: str, wire: Wire) -> dict[str, JsonValue]: + return { + "model_name": model_name, + "litellm_params": { + "model": "text-completion-openai/gpt-3.5-turbo-instruct", + "api_base": f"{wire.url}/v1", + "api_key": _PROVIDER_KEY, + }, + } + + def _config(directory: Path, wire: Wire) -> Path: base: Final = object_value(yaml.safe_load(_PROXY_CONFIG.read_text())) config: Final = { **base, "model_list": [ *list_value(base["model_list"]), - { - "model_name": _TEXT_MODEL, - "litellm_params": { - "model": "text-completion-openai/gpt-3.5-turbo-instruct", - "api_base": f"{wire.url}/v1", - "api_key": _PROVIDER_KEY, - }, - }, + _text_deployment(_TEXT_MODEL, wire), + _text_deployment(_GOVERNED_MODEL, wire), ], "guardrails": [ { @@ -139,8 +157,30 @@ def _config(directory: Path, wire: Wire) -> Path: "default_action": "allow", "rules": [], }, - } + }, + { + "guardrail_name": _KEYWORD_FILTER, + "litellm_params": { + "guardrail": "litellm_content_filter", + "mode": "post_call", + "default_on": False, + "blocked_words": [ + {"keyword": "persimmon", "action": "MASK"}, + {"keyword": "kumquat", "action": "BLOCK"}, + ], + }, + }, ], + "policies": { + _POLICY: { + "guardrails": {"add": [_KEYWORD_FILTER]}, + "pipeline": { + "mode": "post_call", + "steps": [{"guardrail": _KEYWORD_FILTER, "on_pass": "allow", "on_fail": "block"}], + }, + } + }, + "policy_attachments": [{"policy": _POLICY, "models": [_GOVERNED_MODEL]}], } path: Final = directory / f"text-rebuild-{uuid.uuid4().hex}.yaml" path.write_text(yaml.safe_dump(config)) @@ -163,14 +203,14 @@ def rig(tmp_path_factory: pytest.TempPathFactory) -> Iterator[_Rig]: yield _Rig(wire, owned) -def _stream(rig: _Rig, identity: str) -> _Stream: +def _stream(rig: _Rig, identity: str, model: str = _TEXT_MODEL) -> _Stream: gateway: Final = rig.proxy.gateway with gateway.client.stream( "POST", "/v1/completions", headers={"Authorization": f"Bearer {gateway.key}"}, json={ - "model": _TEXT_MODEL, + "model": model, "prompt": identity, "max_tokens": 16, "stream": True, @@ -212,6 +252,37 @@ def test_guarded_text_completion_stream_reaches_the_caller_whole_with_its_usage( assert _GUARDRAIL in list_value(object_value(row["metadata"])["applied_guardrails"]), row +def test_post_call_pipeline_masks_a_streamed_text_completion(rig: _Rig) -> None: + identity: Final = f"cmpl-mask-{uuid.uuid4().hex}" + + stream: Final = _stream(rig, identity, _GOVERNED_MODEL) + + assert stream.status == 200, stream.body + assert stream.body.rstrip().endswith(b"data: [DONE]"), stream.body[-400:] + assert stream.text == "A [KEYWORD_REDACTED] tart", stream.frames + assert b"persimmon" not in stream.body, stream.body + assert stream.finish_reasons == ("length",), stream.frames + assert [(usage["prompt_tokens"], usage["completion_tokens"]) for usage in stream.usages] == [ + (_PROMPT_TOKENS, _COMPLETION_TOKENS) + ], stream.frames + (row,) = eventually( + lambda: rig.proxy.gateway.rows('SELECT metadata FROM "LiteLLM_SpendLogs" WHERE request_id = %s', (identity,)), + lambda rows: len(rows) == 1, + seconds=70, + ) + assert _KEYWORD_FILTER in list_value(object_value(row["metadata"])["applied_guardrails"]), row + + +def test_post_call_pipeline_blocks_a_streamed_text_completion(rig: _Rig) -> None: + identity: Final = f"cmpl-block-{uuid.uuid4().hex}" + + stream: Final = _stream(rig, identity, _GOVERNED_MODEL) + + assert stream.status == 400, stream.body + assert b"Content blocked: keyword 'kumquat' detected" in stream.body, stream.body + assert b"quat tart" not in stream.body, stream.body + + def _together_chunk(choices: list[JsonValue], **extra: JsonValue) -> bytes: return _frame( { diff --git a/tests/unit/AGENTS.md b/tests/unit/AGENTS.md index ee6648c0a82..75c9cc94981 100644 --- a/tests/unit/AGENTS.md +++ b/tests/unit/AGENTS.md @@ -42,6 +42,9 @@ Green if `send_batched` drops every row. pydantic doubles in 12 of 203 files, fa `tests/unit/` mirrors `litellm/`, so a changed file selects its tests by path, not a mapping file. New unit tests go here +A new directory under `tests/unit` runs in CI only once a shard in `.github/workflows/test-unit.yml` names it. +`pytest tests/unit/test_assert_ci_coverage.py` checks that, so run it before you push + ## Writing it so a human can read it A class only when tests share an arrange diff --git a/tests/unit/caching/test_unit_test_caching.py b/tests/unit/caching/test_unit_test_caching.py index 83132b5aa1d..f8a367798db 100644 --- a/tests/unit/caching/test_unit_test_caching.py +++ b/tests/unit/caching/test_unit_test_caching.py @@ -8,6 +8,7 @@ load_dotenv() import asyncio import hashlib import random +from typing import Final import pytest @@ -16,7 +17,7 @@ from litellm import aembedding, completion, embedding from litellm.caching.caching import Cache from unittest.mock import AsyncMock, patch, MagicMock -from litellm.caching.caching_handler import LLMCachingHandler, CachingHandlerResponse +from litellm.caching.caching_handler import LLMCachingHandler, CachingHandlerResponse, _PENDING_CACHE_WRITES from litellm.caching.caching import LiteLLMCacheType from litellm.types.utils import CallTypes from litellm.types.rerank import RerankResponse @@ -278,6 +279,107 @@ def test_get_model_param_value_reads_model_group_from_litellm_metadata(): ) +def test_get_model_param_value_prefers_the_router_group_over_a_caller_model_group(): + cache: Final = Cache() + deployment: Final = {"id": "deployment-1"} + request: Final = {"model": "openai/gpt-5.6", "input": "search this text", "model_info": deployment} + router_metadata: Final = {"model_group": "caller", "model_info": deployment} + + assert ( + cache._get_model_param_value( + {**request, "metadata": {"model_group": "target"}, "litellm_metadata": router_metadata} + ) + == "caller" + ) + assert ( + cache._get_model_param_value( + { + **request, + "litellm_params": {"metadata": {"model_group": "target"}, "litellm_metadata": router_metadata}, + } + ) + == "caller" + ) + assert ( + cache._get_model_param_value( + { + **request, + "metadata": {"model_group": "target", "model_info": deployment}, + "litellm_metadata": router_metadata, + } + ) + == "caller" + ) + assert ( + cache._get_model_param_value( + { + **request, + "metadata": router_metadata, + "litellm_metadata": {"model_group": "parent", "model_info": {"id": "parent-deployment"}}, + } + ) + == "caller" + ) + + +def test_get_model_param_value_keeps_metadata_first_outside_the_router(): + cache: Final = Cache() + + assert ( + cache._get_model_param_value( + { + "model": "openai/gpt-5.6", + "input": "search this text", + "metadata": {"model_group": "group-a"}, + "litellm_metadata": {"model_group": "group-b"}, + } + ) + == "group-a" + ) + + +@pytest.mark.asyncio +async def test_router_call_keeps_its_own_group_when_forwarded_litellm_metadata_names_another( + monkeypatch: pytest.MonkeyPatch, +): + monkeypatch.setattr(litellm, "cache", Cache(type=LiteLLMCacheType.LOCAL)) + router: Final = litellm.Router( + model_list=[ + { + "model_name": group, + "litellm_params": {"model": "openai/gpt-5.6", "api_key": "sk-test", "mock_response": f"{group} answer"}, + } + for group in ("group-a", "group-b") + ] + ) + messages: Final = [{"role": "user", "content": "which group answers this"}] + + await router.acompletion( + model="group-a", + messages=messages, + caching=True, + litellm_metadata={"model_group": "group-b", "model_info": {"id": "parent-deployment"}}, + ) + await asyncio.gather(*_PENDING_CACHE_WRITES) + reply: Final = await router.acompletion(model="group-b", messages=messages, caching=True) + + assert reply.choices[0].message.content == "group-b answer" + + +@pytest.mark.parametrize("planted", [["sales-premium"], [("sales", "sales-premium")]], ids=["string", "tuple"]) +def test_get_model_param_value_reads_caching_groups_only_beside_the_router_group(planted: list[str | tuple[str, str]]): + cache: Final = Cache() + request: Final = { + "model": "openai/gpt-5.6", + "input": "search this text", + "model_info": {"id": "sales-deployment"}, + "litellm_metadata": {"model_group": "sales", "caching_groups": None, "model_info": {"id": "sales-deployment"}}, + "metadata": {"caching_groups": planted}, + } + + assert cache._get_model_param_value(request) == "sales" + + def test_preset_cache_key(): """ Test that the preset cache key is used if it is set in kwargs["litellm_params"] diff --git a/tests/unit/integrations/datadog/test_datadog_cost_management.py b/tests/unit/integrations/datadog/test_datadog_cost_management.py index 1a50a6991da..3601db12909 100644 --- a/tests/unit/integrations/datadog/test_datadog_cost_management.py +++ b/tests/unit/integrations/datadog/test_datadog_cost_management.py @@ -1,4 +1,5 @@ import time +from typing import Final from unittest.mock import AsyncMock import pytest @@ -314,6 +315,43 @@ async def test_extract_tags_nested_metadata_allowlisted(clean_env): assert "ignored" not in tags +@pytest.mark.parametrize( + ("metadata", "request_tags", "expected_tag"), + [ + ( + {"is_silent_experiment": True, "requester_metadata": {"is_silent_experiment": False}}, + ["is_silent_experiment:false"], + "true", + ), + ( + { + "requester_metadata": {"is_silent_experiment": True}, + "spend_logs_metadata": {"is_silent_experiment": "true"}, + }, + [], + None, + ), + ({}, ["is_silent_experiment:true"], None), + ], + ids=["mirrored call", "direct call with a client flag", "direct call with a client request tag"], +) +@pytest.mark.asyncio +async def test_extract_tags_takes_the_silent_experiment_flag_only_from_the_router( + clean_env: None, metadata: dict[str, object], request_tags: list[str], expected_tag: str | None +): + logger: Final = DatadogCostManagementLogger(cost_tag_keys=["is_silent_experiment"]) + log: Final = StandardLoggingPayload( + custom_llm_provider="openai", + model="gpt-4", + response_cost=0.01, + startTime=time.time(), + metadata=metadata, + request_tags=request_tags, + ) + + assert logger._extract_tags(log).get("is_silent_experiment") == expected_tag + + @pytest.mark.asyncio async def test_extract_tags_allowlist_cannot_override_reserved_dimensions(clean_env): """ diff --git a/tests/unit/integrations/test_langfuse.py b/tests/unit/integrations/test_langfuse.py index 00d85b64566..2d1e49e32df 100644 --- a/tests/unit/integrations/test_langfuse.py +++ b/tests/unit/integrations/test_langfuse.py @@ -830,6 +830,31 @@ class TestLangfuseUsageDetails(unittest.TestCase): assert generation_metadata["litellm_response_cost"] == 0.25 assert generation_metadata["api_base"] == "https://real-api-base" + def test_caller_sent_is_silent_experiment_cannot_hide_the_mirrored_call_flag(self): + payload = self._build_standard_logging_payload(trace_id="canary-trace-id") + payload["metadata"]["is_silent_experiment"] = True + payload["metadata"]["requester_metadata"] = {"is_silent_experiment": False} + kwargs = self._build_langfuse_kwargs(payload) + metadata = self._canary_request_metadata() + self.use_real_langfuse_client() + + self.logger._log_langfuse_v2( + user_id="user-1", + metadata=metadata, + litellm_params={"metadata": metadata}, + output=None, + start_time=datetime.datetime(2024, 1, 1, 12, 0, 0), + end_time=datetime.datetime(2024, 1, 1, 12, 0, 1), + kwargs=kwargs, + optional_params={}, + input=None, + response_obj=None, + level="INFO", + litellm_call_id="canary-call-id", + ) + + assert self.exported_generation_metadata()["is_silent_experiment"] is True + def test_generation_metadata_carries_the_call_id_and_response_id(self): """ v2's generation id was ``time-_``, so a generation could diff --git a/tests/unit/integrations/test_prometheus_spend_logs_metadata.py b/tests/unit/integrations/test_prometheus_spend_logs_metadata.py index e2af6fd2daf..608195a9df8 100644 --- a/tests/unit/integrations/test_prometheus_spend_logs_metadata.py +++ b/tests/unit/integrations/test_prometheus_spend_logs_metadata.py @@ -5,6 +5,10 @@ Verifies that metadata from x-litellm-spend-logs-metadata header is available in Prometheus custom labels via combined_metadata. """ +from typing import Final + +import pytest + from litellm.integrations.prometheus import ( _get_combined_custom_metadata_from_standard_logging_payload, get_custom_labels_from_metadata, @@ -205,3 +209,34 @@ def test_project_alias_accessible_without_prefix(monkeypatch): result = get_custom_labels_from_metadata(combined) assert result == {"user_api_key_project_alias": "hotel-recommendations"} + + +@pytest.mark.parametrize( + ("standard_logging_metadata", "expected_labels"), + [ + ( + {"is_silent_experiment": True, "requester_metadata": {"is_silent_experiment": False}}, + {"metadata_is_silent_experiment": "true"}, + ), + ( + { + "requester_metadata": {"is_silent_experiment": True}, + "spend_logs_metadata": {"is_silent_experiment": "true"}, + }, + {}, + ), + ], + ids=["mirrored call", "direct call with a client flag"], +) +def test_silent_experiment_label_comes_only_from_the_router_flag( + monkeypatch: pytest.MonkeyPatch, + standard_logging_metadata: dict[str, object], + expected_labels: dict[str, str], +): + monkeypatch.setattr("litellm.custom_prometheus_metadata_labels", ["metadata.is_silent_experiment"]) + + combined: Final = _get_combined_custom_metadata_from_standard_logging_payload( + {"metadata": standard_logging_metadata} + ) + + assert get_custom_labels_from_metadata(combined) == expected_labels diff --git a/tests/unit/integrations/websearch_interception/conftest.py b/tests/unit/integrations/websearch_interception/conftest.py new file mode 100644 index 00000000000..a9f79a60bfe --- /dev/null +++ b/tests/unit/integrations/websearch_interception/conftest.py @@ -0,0 +1,18 @@ +from typing import Final + +import pytest +import respx + +import litellm + +PERPLEXITY_SEARCH_URL: Final = "https://api.perplexity.ai/search" + + +@pytest.fixture +def perplexity_search(monkeypatch: pytest.MonkeyPatch, respx_mock: respx.MockRouter) -> respx.Route: + monkeypatch.setenv("PERPLEXITYAI_API_KEY", "test-perplexity-key") + monkeypatch.delenv("PERPLEXITY_API_BASE", raising=False) + monkeypatch.setattr(litellm, "disable_aiohttp_transport", True) + return respx_mock.post(PERPLEXITY_SEARCH_URL).respond( + json={"results": [{"title": "AI news", "url": "https://example.com/ai-news", "snippet": "AI news."}]} + ) diff --git a/tests/unit/integrations/websearch_interception/test_websearch_chat_completion.py b/tests/unit/integrations/websearch_interception/test_websearch_chat_completion.py index 21e50561f57..fc415374096 100644 --- a/tests/unit/integrations/websearch_interception/test_websearch_chat_completion.py +++ b/tests/unit/integrations/websearch_interception/test_websearch_chat_completion.py @@ -5,15 +5,19 @@ Tests the end-to-end flow of websearch_interception callback with litellm.acompletion() for transparent server-side web search execution. """ -from unittest.mock import MagicMock +import datetime +from typing import Final +from unittest.mock import AsyncMock, MagicMock, patch import pytest +import respx import litellm from litellm.integrations.websearch_interception.handler import ( WebSearchInterceptionLogger, ) -from litellm.types.utils import LlmProviders, ModelResponse +from litellm.litellm_core_utils.litellm_logging import Logging +from litellm.types.utils import CallTypes, LlmProviders, ModelResponse @pytest.fixture @@ -378,7 +382,6 @@ async def test_execute_chat_completion_agentic_loop_strips_tool_choice(): optional_params into the follow-up params without explicit removal causes the model to call the search tool again instead of synthesizing an answer. """ - from unittest.mock import patch websearch_logger = WebSearchInterceptionLogger(enabled_providers=[LlmProviders.OPENAI]) @@ -422,3 +425,54 @@ async def test_execute_chat_completion_agentic_loop_strips_tool_choice(): if __name__ == "__main__": # Run with: pytest test_websearch_chat_completion.py -v -s pytest.main([__file__, "-v", "-s"]) + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "requested_model,handler_model,custom_llm_provider", + [ + ("openrouter/openrouter/free", "openrouter/free", "openrouter"), + ("bedrock/converse_like/my-endpoint-model", "my-endpoint-model", "bedrock"), + ], +) +async def test_chat_followup_reuses_the_model_the_caller_requested( + requested_model: str, handler_model: str, custom_llm_provider: str, perplexity_search: respx.Route +) -> None: + """The provider handler sees the model after its routing prefixes are stripped, which + re-qualifying with the provider alone cannot restore.""" + logging_obj: Final = Logging( + model=requested_model, + messages=[], + stream=False, + call_type=CallTypes.completion.value, + start_time=datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc), + litellm_call_id="call-chat-followup", + function_id="fn-chat-followup", + ) + litellm.completion( + model=requested_model, + messages=[{"role": "user", "content": "What's the latest AI news?"}], + mock_response="searching", + litellm_logging_obj=logging_obj, + ) + logger: Final = WebSearchInterceptionLogger() + + plan: Final = await logger.async_build_chat_completion_agentic_loop_plan( + tools={ + "tool_calls": [{"id": "call_abc", "name": "litellm_web_search", "input": {"query": "latest ai news"}}], + "tool_type": "websearch", + "provider": custom_llm_provider, + "response_format": "openai", + }, + model=handler_model, + messages=[{"role": "user", "content": "What's the latest AI news?"}], + response=MagicMock(), + optional_params={}, + logging_obj=logging_obj, + stream=False, + kwargs={"custom_llm_provider": custom_llm_provider}, + ) + + assert plan.request_patch is not None + assert plan.request_patch.model == requested_model + assert perplexity_search.call_count == 1 diff --git a/tests/unit/integrations/websearch_interception/test_websearch_responses.py b/tests/unit/integrations/websearch_interception/test_websearch_responses.py index 00b7299cc31..6ca812a5e40 100644 --- a/tests/unit/integrations/websearch_interception/test_websearch_responses.py +++ b/tests/unit/integrations/websearch_interception/test_websearch_responses.py @@ -6,17 +6,22 @@ tool calls returned by /v1/responses, executes the search server-side, and builds a Responses-format follow-up request. """ +import datetime from types import SimpleNamespace +from typing import Final from unittest.mock import AsyncMock, MagicMock, patch import pytest +import litellm + from litellm.integrations.websearch_interception.handler import ( WebSearchInterceptionLogger, ) from litellm.types.integrations.custom_logger import ( RESPONSES_AGENTIC_SURFACE, ) +from litellm.litellm_core_utils.litellm_logging import Logging from litellm.types.utils import CallTypes, LlmProviders @@ -252,6 +257,112 @@ async def test_build_responses_plan_produces_responses_input(): assert patch_obj.model == "openai/gpt-4o" +_WEB_SEARCH_FUNCTION_CALL: Final = { + "tool_calls": [ + { + "id": "fc_1", + "call_id": "fc_1", + "type": "function_call", + "name": "litellm_web_search", + "arguments": '{"query": "latest ai news"}', + "input": {"query": "latest ai news"}, + } + ], + "tool_type": "websearch", + "response_format": "responses", +} + + +async def _responses_followup_model(model: str, custom_llm_provider: str, logging_obj: Logging | None) -> str | None: + logger: Final = WebSearchInterceptionLogger() + plan: Final = await logger.async_build_responses_agentic_loop_plan( + tools={**_WEB_SEARCH_FUNCTION_CALL, "provider": custom_llm_provider}, + model=model, + messages=[{"role": "user", "content": "What's the latest AI news?"}], + response=_responses_output_with_web_search(), + optional_params={"tools": [{"type": "function", "name": "litellm_web_search"}]}, + logging_obj=logging_obj, + stream=False, + kwargs={ + "custom_llm_provider": custom_llm_provider, + "_agentic_loop_api_surface": RESPONSES_AGENTIC_SURFACE, + }, + ) + assert plan.request_patch is not None + return plan.request_patch.model + + +@pytest.mark.asyncio +@pytest.mark.usefixtures("perplexity_search") +async def test_build_responses_plan_keeps_the_provider_on_a_sub_path_model() -> None: + """#38829 on the Responses surface: a provider-stripped model whose remainder still holds a + slash was left unqualified, so the follow-up could not resolve a provider.""" + followup_model: Final = await _responses_followup_model("mantle/anthropic.claude-sonnet-5", "bedrock", None) + + assert followup_model == "bedrock/mantle/anthropic.claude-sonnet-5" + + +@pytest.mark.asyncio +@pytest.mark.usefixtures("perplexity_search") +async def test_responses_followup_reuses_the_model_the_caller_requested() -> None: + """An OpenRouter model whose own id starts with "openrouter/" reaches the handler as + "openrouter/free", which re-qualifying alone cannot tell apart from a request for model "free".""" + requested_model: Final = "openrouter/openrouter/free" + logging_obj: Final = Logging( + model=requested_model, + messages=[], + stream=False, + call_type=CallTypes.responses.value, + start_time=datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc), + litellm_call_id="call-responses-followup", + function_id="fn-responses-followup", + ) + litellm.responses( + model=requested_model, + input="What's the latest AI news?", + mock_response="searching", + litellm_logging_obj=logging_obj, + ) + + followup_model: Final = await _responses_followup_model("openrouter/free", "openrouter", logging_obj) + + assert followup_model == requested_model + + +@pytest.mark.asyncio +@pytest.mark.usefixtures("perplexity_search") +async def test_responses_followup_keeps_the_caller_model_across_the_chat_completions_bridge() -> None: + """A Responses request bridged to chat completions re-enters completion() with the stripped model.""" + requested_model: Final = "openrouter/openrouter/free" + logging_obj: Final = Logging( + model=requested_model, + messages=[], + stream=False, + call_type=CallTypes.responses.value, + start_time=datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc), + litellm_call_id="call-bridged-followup", + function_id="fn-bridged-followup", + ) + litellm.responses( + model=requested_model, + input="What's the latest AI news?", + mock_response="searching", + litellm_logging_obj=logging_obj, + ) + litellm.completion( + model="openrouter/free", + custom_llm_provider="openrouter", + messages=[{"role": "user", "content": "What's the latest AI news?"}], + mock_response="searching", + litellm_logging_obj=logging_obj, + _skip_responses_api_bridge=True, + ) + + followup_model: Final = await _responses_followup_model("openrouter/free", "openrouter", logging_obj) + + assert followup_model == requested_model + + @pytest.mark.asyncio async def test_deployment_hook_converts_native_responses_web_search_tool(): """async_pre_call_deployment_hook converts a native Responses web_search tool diff --git a/tests/unit/integrations/websearch_interception/test_websearch_rich_query_shape.py b/tests/unit/integrations/websearch_interception/test_websearch_rich_query_shape.py index 72149e8a435..1020848ec89 100644 --- a/tests/unit/integrations/websearch_interception/test_websearch_rich_query_shape.py +++ b/tests/unit/integrations/websearch_interception/test_websearch_rich_query_shape.py @@ -239,6 +239,7 @@ class TestCallSiteWiring: tool_calls=tool_calls, optional_params={}, kwargs={}, + logging_obj=None, ) call_kwargs = mock_asearch.await_args.kwargs diff --git a/tests/unit/integrations/websearch_interception/test_websearch_short_circuit.py b/tests/unit/integrations/websearch_interception/test_websearch_short_circuit.py index 414bcc11602..ef050d96dc5 100644 --- a/tests/unit/integrations/websearch_interception/test_websearch_short_circuit.py +++ b/tests/unit/integrations/websearch_interception/test_websearch_short_circuit.py @@ -32,6 +32,7 @@ class TestTryShortCircuitSearch: ("github_copilot", "github_copilot/claude-sonnet-5.5"), ("databricks", "databricks/databricks-claude-sonnet-5-5"), ("databricks", "databricks/system.ai.claude-sonnet-5-5"), + ("dashscope", "dashscope/qwen3.7-plus"), ], ) async def test_short_circuits_single_web_search_tool(self, provider, model): diff --git a/tests/unit/litellm_core_utils/llm_response_utils/test_convert_dict_to_response.py b/tests/unit/litellm_core_utils/llm_response_utils/test_convert_dict_to_response.py index 0c601a6b2c8..faecb8d8ef6 100644 --- a/tests/unit/litellm_core_utils/llm_response_utils/test_convert_dict_to_response.py +++ b/tests/unit/litellm_core_utils/llm_response_utils/test_convert_dict_to_response.py @@ -2287,17 +2287,19 @@ class TestConvertToStreamingResponse: } chunks = list(convert_to_streaming_response(response_object=response_object)) - assert len(chunks) == 1 - chunk = chunks[0] - assert chunk.id == "chatcmpl-123" - assert chunk.model == "gpt-4" - assert chunk.created == 1700000000 - assert chunk.system_fingerprint == "fp_abc" - assert chunk.choices[0].delta.content == "Hello!" - assert chunk.choices[0].delta.role == "assistant" - assert chunk.choices[0].finish_reason == "stop" - assert chunk.usage.prompt_tokens == 5 - assert chunk.usage.completion_tokens == 2 + assert len(chunks) == 2 + assert all(chunk.id == "chatcmpl-123" for chunk in chunks) + assert all(chunk.model == "gpt-4" for chunk in chunks) + assert all(chunk.created == 1700000000 for chunk in chunks) + assert all(chunk.system_fingerprint == "fp_abc" for chunk in chunks) + content, finish = chunks + assert content.choices[0].delta.content == "Hello!" + assert content.choices[0].delta.role == "assistant" + assert content.choices[0].finish_reason is None + assert finish.choices[0].delta.content is None + assert finish.choices[0].finish_reason == "stop" + assert finish.usage.prompt_tokens == 5 + assert finish.usage.completion_tokens == 2 def test_finish_details_fallback(self): from litellm.litellm_core_utils.llm_response_utils.convert_dict_to_response import ( @@ -2315,7 +2317,7 @@ class TestConvertToStreamingResponse: } chunks = list(convert_to_streaming_response(response_object=response_object)) - assert chunks[0].choices[0].finish_reason == "length" + assert chunks[-1].choices[0].finish_reason == "length" def test_tool_calls_in_streaming(self): from litellm.litellm_core_utils.llm_response_utils.convert_dict_to_response import ( @@ -2347,17 +2349,20 @@ class TestConvertToStreamingResponse: } async def run(): - chunks = [] - async for chunk in convert_to_streaming_response_async( - response_object=response_object - ): - chunks.append(chunk) - return chunks + return [ + chunk + async for chunk in convert_to_streaming_response_async( + response_object=response_object + ) + ] chunks = asyncio.run(run()) - assert len(chunks) == 1 + assert len(chunks) == 2 assert chunks[0].choices[0].delta.tool_calls[0].id == "call_1" assert chunks[0].choices[0].delta.tool_calls[0].function.name == "get_weather" + assert chunks[0].choices[0].finish_reason is None + assert chunks[1].choices[0].delta.tool_calls is None + assert chunks[1].choices[0].finish_reason == "tool_calls" class TestConvertToStreamingResponseAsync: def test_none_raises(self): @@ -2395,17 +2400,17 @@ class TestConvertToStreamingResponseAsync: } async def run(): - chunks = [] - async for chunk in convert_to_streaming_response_async( - response_object=response_object - ): - chunks.append(chunk) - return chunks + return [ + chunk + async for chunk in convert_to_streaming_response_async( + response_object=response_object + ) + ] chunks = asyncio.run(run()) # Cached replay is sliced into word-shaped chunks to preserve # streaming cadence; joining the slices reconstructs the content. - assert len(chunks) == 2 + assert len(chunks) == 3 assert all(c.id == "msg_async_1" for c in chunks) assert all(c.model == "claude-3" for c in chunks) assert "".join(c.choices[0].delta.content or "" for c in chunks) == "Hi there" @@ -2413,6 +2418,81 @@ class TestConvertToStreamingResponseAsync: assert chunks[-1].choices[0].finish_reason == "stop" assert chunks[-1].usage.prompt_tokens == 3 + def test_preserves_reasoning_fields(self): + import asyncio + + from litellm.litellm_core_utils.llm_response_utils.convert_dict_to_response import ( + convert_to_streaming_response_async, + ) + + thinking_blocks = [ + { + "type": "thinking", + "thinking": "cached reasoning", + "signature": "sig-cache", + } + ] + annotations = [ + { + "type": "url_citation", + "url_citation": { + "start_index": 0, + "end_index": 5, + "title": "Cached source", + "url": "https://example.com/cached-source", + }, + } + ] + response_object = { + "id": "msg_async_reasoning_cache", + "model": "claude-3", + "created": 1700000000, + "choices": [ + { + "finish_reason": "stop", + "index": 0, + "message": { + "content": "Final answer", + "role": "assistant", + "reasoning_content": "cached reasoning", + "thinking_blocks": thinking_blocks, + "annotations": annotations, + }, + } + ], + } + + async def run(): + return [ + chunk + async for chunk in convert_to_streaming_response_async( + response_object=response_object + ) + ] + + chunks = asyncio.run(run()) + + assert ( + "".join(c.choices[0].delta.content or "" for c in chunks) == "Final answer" + ) + assert chunks[0].choices[0].delta.content is None + assert chunks[0].choices[0].delta.reasoning_content == "cached reasoning" + assert chunks[0].choices[0].delta.thinking_blocks == thinking_blocks + last_content = max( + i for i, c in enumerate(chunks) if c.choices[0].delta.content + ) + assert chunks[last_content].choices[0].delta.annotations == annotations + assert [ + i for i, c in enumerate(chunks) if c.choices[0].delta.get("annotations") + ] == [last_content] + assert not any( + hasattr(c.choices[0].delta, "reasoning_content") for c in chunks[1:] + ) + assert not any( + hasattr(c.choices[0].delta, "thinking_blocks") for c in chunks[1:] + ) + + class TestHandleInvalidParallelToolCalls: def test_none_input(self): from litellm.litellm_core_utils.llm_response_utils.convert_dict_to_response import ( @@ -2968,3 +3048,54 @@ def test_convert_to_model_response_object_function_output(): assert result.usage.completion_tokens_details == CompletionTokensDetailsWrapper(reasoning_tokens=0) assert result._hidden_params is not None + + +_URL_CITATION: Final = { + "type": "url_citation", + "url_citation": { + "url": "https://example.com", + "title": "Example", + "content": "cited snippet", + "start_index": 0, + "end_index": 4, + }, +} +_FILE_ANNOTATION: Final = { + "type": "file", + "file": {"hash": "abc123", "name": "report.pdf", "content": [{"type": "text", "text": "parsed page"}]}, +} + + +def _message_with_annotations(annotations: list[dict[str, object]]) -> Message: + response: Final = { + "id": "gen-1", + "object": "chat.completion", + "created": 1712697600, + "model": "z-ai/glm-5.3", + "choices": [ + { + "index": 0, + "finish_reason": "stop", + "message": {"role": "assistant", "content": "done", "annotations": annotations}, + } + ], + "usage": {"completion_tokens": 1, "prompt_tokens": 1, "total_tokens": 2}, + } + return convert_to_model_response_object( + response_object=response, model_response_object=ModelResponse(), response_type="completion" + ).choices[0].message + + +def test_annotations_typed_other_than_url_citation_move_to_provider_specific_fields() -> None: + untyped_annotation: Final = {"start_index": 0, "end_index": 4} + message: Final = _message_with_annotations([_URL_CITATION, untyped_annotation, _FILE_ANNOTATION]) + + assert message.annotations == [_URL_CITATION, untyped_annotation] + assert message.provider_specific_fields["annotations"] == [_FILE_ANNOTATION] + + +def test_url_citation_annotations_keep_provider_keys() -> None: + message: Final = _message_with_annotations([_URL_CITATION]) + + assert message.annotations == [_URL_CITATION] + assert "annotations" not in (message.provider_specific_fields or {}) diff --git a/tests/unit/litellm_core_utils/llm_response_utils/test_convert_to_streaming_response.py b/tests/unit/litellm_core_utils/llm_response_utils/test_convert_to_streaming_response.py index 348d0105a6c..3713869d7ca 100644 --- a/tests/unit/litellm_core_utils/llm_response_utils/test_convert_to_streaming_response.py +++ b/tests/unit/litellm_core_utils/llm_response_utils/test_convert_to_streaming_response.py @@ -9,9 +9,11 @@ replay yields multiple word-shaped chunks instead of a single one-shot content frame, restoring per-token cadence on cache hits. """ -from typing import Final +from datetime import datetime +from typing import Final, Literal import pytest +from pydantic import JsonValue import litellm from litellm.litellm_core_utils.llm_response_utils.convert_dict_to_response import ( @@ -19,6 +21,7 @@ from litellm.litellm_core_utils.llm_response_utils.convert_dict_to_response impo convert_to_streaming_response, convert_to_streaming_response_async, ) +from litellm.litellm_core_utils.streaming_handler import CustomStreamWrapper from litellm.types.utils import ModelResponseStream @@ -267,23 +270,20 @@ async def test_async_usage_attached_to_last_chunk_only(): @pytest.mark.asyncio -async def test_async_empty_content_yields_single_finish_frame(): - # tool-calls-only-style response: short-circuit to the single-yield path. +async def test_async_empty_content_yields_the_role_then_a_separate_finish_frame(): payload = _async_payload(content=None) payload["usage"] = None chunks = await _collect_async(payload) - assert len(chunks) == 1 - assert chunks[0].choices[0].delta.content is None - assert chunks[0].choices[0].finish_reason == "stop" - assert chunks[0].choices[0].delta.role == "assistant" - assert getattr(chunks[0], "usage", None) is None + assert [(c.choices[0].delta.role, c.choices[0].finish_reason) for c in chunks] == [ + ("assistant", None), + (None, "stop"), + ] + assert all(c.choices[0].delta.content is None for c in chunks) + assert all(getattr(c, "usage", None) is None for c in chunks) @pytest.mark.asyncio -async def test_async_tool_calls_and_function_call_only_on_first_chunk(): - # A cached response combining multi-word content with tool_calls must not - # repeat the tool_calls on every slice — downstream handlers accumulate - # tool-call deltas and would collect them N times. +async def test_async_tool_calls_arrive_once_after_the_last_content_slice(): payload = _async_payload(content="I'll look that up for you") payload["choices"][0]["message"]["tool_calls"] = [ { @@ -294,13 +294,13 @@ async def test_async_tool_calls_and_function_call_only_on_first_chunk(): } ] chunks = await _collect_async(payload) - assert len(chunks) > 1 - first_tool_calls = chunks[0].choices[0].delta.tool_calls - assert first_tool_calls is not None and len(first_tool_calls) == 1 - assert first_tool_calls[0].function.name == "lookup" - for c in chunks[1:]: - assert c.choices[0].delta.tool_calls is None - assert c.choices[0].delta.function_call is None + called: Final = [i for i, c in enumerate(chunks) if c.choices[0].delta.tool_calls] + last_content: Final = max(i for i, c in enumerate(chunks) if c.choices[0].delta.content) + assert called == [last_content + 1] and last_content > 0 + assert chunks[called[0]].choices[0].delta.tool_calls[0].function.name == "lookup" + assert chunks[called[0]].choices[0].delta.content is None + assert all(c.choices[0].delta.function_call is None for c in chunks) + assert chunks[-1].choices[0].finish_reason == "stop" and len(chunks) == called[0] + 2 @pytest.mark.asyncio @@ -385,7 +385,7 @@ def test_sync_multi_chunk_and_lossless_join(): assert chunks[-1].usage.total_tokens == 12 -def test_sync_delta_and_choice_metadata_only_on_first_chunk(): +def test_sync_reasoning_leads_and_choice_metadata_rides_the_first_content_slice(): thinking_blocks = [{"type": "thinking", "thinking": "cached thinking", "signature": "sig"}] payload = { "id": "chatcmpl-test-sync", @@ -408,25 +408,22 @@ def test_sync_delta_and_choice_metadata_only_on_first_chunk(): ], } chunks = list(convert_to_streaming_response(payload)) - assert len(chunks) > 1 - first_choice = chunks[0].choices[0] - assert getattr(first_choice.delta, "reasoning_content", None) == "cached reasoning" - assert getattr(first_choice.delta, "thinking_blocks", None) == thinking_blocks - assert first_choice.logprobs is not None - assert first_choice.enhancements == {"source": "cache"} - for c in chunks[1:]: + lead: Final = chunks[0].choices[0] + assert getattr(lead.delta, "reasoning_content", None) == "cached reasoning" + assert getattr(lead.delta, "thinking_blocks", None) == thinking_blocks + assert lead.delta.content is None + first_text: Final = chunks[1].choices[0] + assert first_text.delta.content and first_text.logprobs is not None + assert first_text.enhancements == {"source": "cache"} + for index, c in enumerate(chunks): choice = c.choices[0] - assert getattr(choice.delta, "reasoning_content", None) is None - assert getattr(choice.delta, "thinking_blocks", None) is None - assert choice.logprobs is None - assert getattr(choice, "enhancements", None) is None + assert index == 0 or getattr(choice.delta, "reasoning_content", None) is None + assert index == 0 or getattr(choice.delta, "thinking_blocks", None) is None + assert index == 1 or choice.logprobs is None + assert index == 1 or getattr(choice, "enhancements", None) is None -def test_sync_non_enumerated_delta_fields_only_on_first_chunk(): - # annotations (and any other Delta field beyond the role/tool_call/reasoning - # set) must also stay on the first slice. Rebuilding later slices as a bare - # content delta drops the whole class, so this holds without enumerating - # every field by hand. +def test_sync_annotations_arrive_once_with_the_last_content_slice(): annotations = [ { "type": "url_citation", @@ -456,7 +453,161 @@ def test_sync_non_enumerated_delta_fields_only_on_first_chunk(): ], } chunks = list(convert_to_streaming_response(payload)) - assert len(chunks) > 1 - assert getattr(chunks[0].choices[0].delta, "annotations", None) == annotations - for c in chunks[1:]: - assert getattr(c.choices[0].delta, "annotations", None) is None + annotated: Final = [i for i, c in enumerate(chunks) if getattr(c.choices[0].delta, "annotations", None)] + last_content: Final = max(i for i, c in enumerate(chunks) if c.choices[0].delta.content) + assert annotated == [last_content] and last_content > 0 + assert chunks[last_content].choices[0].delta.annotations == annotations + + +async def _client_stream( + message: dict[str, JsonValue], replay: Literal["async", "sync"] +) -> list[dict[str, JsonValue]]: + payload: Final = { + **_async_payload(), + "choices": [{"index": 0, "message": {"role": "assistant", **message}, "finish_reason": "stop"}], + } + logging_obj: Final = litellm.Logging( + model="gpt-4o-mini", + messages=[{"role": "user", "content": "hi"}], + stream=True, + call_type="acompletion", + start_time=datetime.now(), + litellm_call_id="cached-replay", + function_id="cached-replay", + ) + source: Final = ( + convert_to_streaming_response_async(response_object=payload) + if replay == "async" + else convert_to_streaming_response(response_object=payload) + ) + wrapper: Final = CustomStreamWrapper( + completion_stream=source, model="gpt-4o-mini", custom_llm_provider="cached_response", logging_obj=logging_obj + ) + chunks: Final = [chunk async for chunk in wrapper] if replay == "async" else list(wrapper) + return [ + {**chunk.choices[0].delta.model_dump(exclude_none=True), "finish": chunk.choices[0].finish_reason} + for chunk in chunks + ] + + +@pytest.mark.asyncio +@pytest.mark.parametrize("replay", ["async", "sync"]) +@pytest.mark.parametrize( + "field, value", + [ + ("audio", {"id": "audio_1", "data": "UklGRg==", "expires_at": 1700000600, "transcript": "Hello"}), + ("images", [{"type": "image_url", "image_url": {"url": "data:image/png;base64,iVBORw0KGgo="}, "index": 0}]), + ], +) +async def test_a_cached_answer_without_text_streams_its_role_and_payload_to_the_client( + replay: Literal["async", "sync"], field: str, value: JsonValue +) -> None: + deltas: Final = await _client_stream({"content": None, field: value}, replay) + assert deltas[0]["role"] == "assistant" + assert [delta[field] for delta in deltas if field in delta] == [value] + assert deltas[-1]["finish"] == "stop" + + +@pytest.mark.asyncio +@pytest.mark.parametrize("replay", ["async", "sync"]) +async def test_a_cached_answer_streams_reasoning_before_the_text_and_citations_after_it( + replay: Literal["async", "sync"], +) -> None: + annotations: Final[JsonValue] = [ + { + "type": "url_citation", + "url_citation": {"url": "https://example.com", "title": "Example", "start_index": 0, "end_index": 23}, + } + ] + deltas: Final = await _client_stream( + { + "content": "The answer is 14 apples", + "reasoning_content": "Count the apples in each basket", + "annotations": annotations, + }, + replay, + ) + reasoning: Final = [i for i, delta in enumerate(deltas) if "reasoning_content" in delta] + text: Final = [i for i, delta in enumerate(deltas) if delta.get("content")] + cited: Final = [i for i, delta in enumerate(deltas) if "annotations" in delta] + assert "".join(str(deltas[i]["reasoning_content"]) for i in reasoning) == "Count the apples in each basket" + assert "".join(str(deltas[i]["content"]) for i in text) == "The answer is 14 apples" + assert max(reasoning) < min(text) + assert cited == [max(text)] and deltas[max(text)]["annotations"] == annotations + assert deltas[-1]["finish"] == "stop" + + +@pytest.mark.asyncio +@pytest.mark.parametrize("replay", ["async", "sync"]) +async def test_a_cached_answer_streams_its_tool_call_after_the_reasoning_and_the_text( + replay: Literal["async", "sync"], +) -> None: + tool_call: Final[JsonValue] = { + "id": "call_1", + "type": "function", + "function": {"name": "count_apples", "arguments": '{"basket": "all"}'}, + "index": 0, + } + deltas: Final = await _client_stream( + { + "content": "Let me count the apples", + "reasoning_content": "I need the basket totals", + "tool_calls": [tool_call], + }, + replay, + ) + reasoning: Final = [i for i, delta in enumerate(deltas) if "reasoning_content" in delta] + text: Final = [i for i, delta in enumerate(deltas) if delta.get("content")] + called: Final = [i for i, delta in enumerate(deltas) if delta.get("tool_calls")] + assert "".join(str(deltas[i]["content"]) for i in text) == "Let me count the apples" + assert reasoning and max(reasoning) < min(text) + assert called == [max(text) + 1] + assert deltas[called[0]]["tool_calls"] == [tool_call] + assert deltas[-1]["finish"] == "stop" + + +@pytest.mark.asyncio +@pytest.mark.parametrize("replay", ["async", "sync"]) +async def test_a_cached_tool_turn_without_text_streams_the_reasoning_before_the_tool_call( + replay: Literal["async", "sync"], +) -> None: + tool_call: Final[JsonValue] = { + "id": "call_1", + "type": "function", + "function": {"name": "count_apples", "arguments": '{"basket": "all"}'}, + "index": 0, + } + deltas: Final = await _client_stream( + {"content": None, "reasoning_content": "I need the basket totals", "tool_calls": [tool_call]}, replay + ) + reasoning: Final = [i for i, delta in enumerate(deltas) if "reasoning_content" in delta] + called: Final = [i for i, delta in enumerate(deltas) if delta.get("tool_calls")] + assert deltas[0]["role"] == "assistant" + assert "".join(str(deltas[i]["reasoning_content"]) for i in reasoning) == "I need the basket totals" + assert reasoning and called and max(reasoning) < min(called) + assert [deltas[i]["tool_calls"] for i in called] == [[tool_call]] + assert deltas[-1]["finish"] == "stop" + + +@pytest.mark.asyncio +@pytest.mark.parametrize("replay", ["async", "sync"]) +@pytest.mark.parametrize("whitespace", ["\n\n", " "], ids=["newlines", "space"]) +async def test_a_cached_tool_turn_keeps_its_whitespace_only_text_between_the_reasoning_and_the_tool_call( + replay: Literal["async", "sync"], whitespace: str +) -> None: + tool_call: Final[JsonValue] = { + "id": "call_1", + "type": "function", + "function": {"name": "count_apples", "arguments": '{"basket": "all"}'}, + "index": 0, + } + deltas: Final = await _client_stream( + {"content": whitespace, "reasoning_content": "I need the basket totals", "tool_calls": [tool_call]}, replay + ) + reasoning: Final = [i for i, delta in enumerate(deltas) if "reasoning_content" in delta] + text: Final = [i for i, delta in enumerate(deltas) if delta.get("content")] + called: Final = [i for i, delta in enumerate(deltas) if delta.get("tool_calls")] + assert "".join(str(deltas[i]["content"]) for i in text) == whitespace + assert reasoning and text and called and max(reasoning) < min(text) and max(text) < min(called) + assert [deltas[i]["tool_calls"] for i in called] == [[tool_call]] + assert deltas[-1]["finish"] == "stop" diff --git a/tests/unit/litellm_core_utils/test_chat_completion_agentic_loop.py b/tests/unit/litellm_core_utils/test_chat_completion_agentic_loop.py index 3cbc3591009..6167e91c37f 100644 --- a/tests/unit/litellm_core_utils/test_chat_completion_agentic_loop.py +++ b/tests/unit/litellm_core_utils/test_chat_completion_agentic_loop.py @@ -364,6 +364,49 @@ async def test_dispatcher_runs_followup_with_incremented_depth_and_patched_messa assert logger.cleanup_calls == 1 +@pytest.mark.asyncio +@pytest.mark.parametrize( + "model,custom_llm_provider,routed_model", + [ + ("cohere/command-a-03-2025", "cohere_chat", "command-a-03-2025"), + ("anthropic/claude-2", "anthropic_text", "claude-2"), + ], +) +async def test_sdk_followup_keeps_a_model_whose_prefix_already_routes_to_its_provider( + model: str, custom_llm_provider: str, routed_model: str +) -> None: + plan: Final = AgenticLoopPlan( + run_agentic_loop=True, + request_patch=AgenticLoopRequestPatch(messages=_patched_messages()), + ) + gate: Final = _GateOnlyLogger(plan=plan, tool_calls={"tool_calls": [{"id": "call_abc"}]}) + logging_obj: Final = Logging( + model=model, + messages=[{"role": "user", "content": "what is 6*7?"}], + stream=False, + call_type="acompletion", + start_time=datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc), + litellm_call_id="call-prefix", + function_id="fn-prefix", + dynamic_success_callbacks=[gate], + ) + + followup: Final = await maybe_run_chat_completion_agentic_loop( + response=_tool_call_model_response(), + model=model, + messages=[{"role": "user", "content": "what is 6*7?"}], + optional_params={}, + kwargs={"mock_response": "done"}, + logging_obj=logging_obj, + custom_llm_provider=custom_llm_provider, + stream=False, + ) + + assert isinstance(followup, ModelResponse) + assert followup.model == routed_model + assert followup._hidden_params["custom_llm_provider"] == custom_llm_provider + + @pytest.mark.asyncio async def test_dispatcher_followup_does_not_repeat_a_request_param_found_in_request_kwargs( restore_callbacks, diff --git a/tests/unit/litellm_core_utils/test_core_helpers.py b/tests/unit/litellm_core_utils/test_core_helpers.py index 66a8e6bbda6..a4a3ae285b1 100644 --- a/tests/unit/litellm_core_utils/test_core_helpers.py +++ b/tests/unit/litellm_core_utils/test_core_helpers.py @@ -26,6 +26,8 @@ from litellm.litellm_core_utils.core_helpers import ( normalize_drop_params, preserve_upstream_non_openai_attributes, process_response_headers, + qualify_agentic_followup_model, + qualify_provider_stripped_model, reconstruct_model_name, redact_nested_match_and_regex_keys, set_provider_response_headers_in_hidden_params, @@ -637,6 +639,87 @@ class TestProviderResponseHeadersInHiddenParams: assert "additional_headers" not in sibling._hidden_params +@pytest.mark.parametrize( + "model,provider,expected", + [ + ("mantle/anthropic.claude-sonnet-5", "bedrock", "bedrock/mantle/anthropic.claude-sonnet-5"), + ("invoke/anthropic.claude-v2", "bedrock", "bedrock/invoke/anthropic.claude-v2"), + ("openai/gpt-4o", "openrouter", "openrouter/openai/gpt-4o"), + ("gpt-4o", "openai", "openai/gpt-4o"), + ("claude-sonnet-4-5", "anthropic", "anthropic/claude-sonnet-4-5"), + ], +) +def test_the_provider_prefix_is_restored(model: str, provider: str, expected: str) -> None: + assert qualify_provider_stripped_model(model, provider) == expected + + +@pytest.mark.parametrize( + "model,provider", + [ + ("bedrock/mantle/anthropic.claude-sonnet-5", "bedrock"), + ("openai/gpt-4o", "openai"), + ("cohere/command-a-03-2025", "cohere_chat"), + ("ai21/jamba-1.5-large", "ai21_chat"), + ("anthropic/claude-2", "anthropic_text"), + ], +) +def test_an_already_qualified_model_is_left_alone(model: str, provider: str) -> None: + assert qualify_provider_stripped_model(model, provider) == model + + +def test_a_provider_that_only_shares_a_prefix_is_still_qualified() -> None: + assert qualify_provider_stripped_model("openai_like/foo", "openai") == "openai/openai_like/foo" + + +def test_no_provider_leaves_the_model_untouched() -> None: + assert qualify_provider_stripped_model("gpt-4o", "") == "gpt-4o" + + +def test_a_cross_provider_patched_model_is_dispatched_as_the_hook_asked() -> None: + assert qualify_agentic_followup_model("anthropic/claude-sonnet-4-5", "gpt-4o", "openai") == ( + "anthropic/claude-sonnet-4-5" + ) + + +def test_a_bare_patched_model_takes_the_request_provider() -> None: + assert qualify_agentic_followup_model("gpt-4o-mini", "gpt-4o", "openai") == "openai/gpt-4o-mini" + + +def test_a_sub_path_request_model_keeps_its_provider() -> None: + assert qualify_agentic_followup_model(None, "mantle/anthropic.claude-sonnet-5", "bedrock") == ( + "bedrock/mantle/anthropic.claude-sonnet-5" + ) + + +def test_an_unpatched_ordinary_model_takes_the_request_provider() -> None: + assert qualify_agentic_followup_model(None, "gpt-4o", "openai") == "openai/gpt-4o" + + +def test_a_patched_model_already_holding_the_request_provider_is_left_alone() -> None: + assert qualify_agentic_followup_model("openai/gpt-4o", "gpt-4o", "openai") == "openai/gpt-4o" + + +@pytest.mark.parametrize("patched", ["", None]) +def test_a_patch_without_a_model_falls_back_to_the_request_model(patched: str | None) -> None: + assert qualify_agentic_followup_model(patched, "gpt-4o", "openai") == "openai/gpt-4o" + + +def test_a_patched_sub_path_model_still_takes_the_request_provider() -> None: + assert qualify_agentic_followup_model("mantle/anthropic.claude-sonnet-5", "gpt-4o", "bedrock") == ( + "bedrock/mantle/anthropic.claude-sonnet-5" + ) + + +def test_a_patched_model_on_a_json_configured_provider_reaches_that_provider() -> None: + assert qualify_agentic_followup_model("aihubmix/gpt-4o", "gpt-4o", "openai") == "aihubmix/gpt-4o" + + +def test_a_patch_echoing_the_request_model_keeps_the_request_provider() -> None: + assert qualify_agentic_followup_model("openai/gpt-4o", "openai/gpt-4o", "openrouter") == ( + "openrouter/openai/gpt-4o" + ) + + def test_spend_metadata_merge_survives_the_logging_thread_inserting_into_the_request_metadata() -> None: metadata: Final[dict[str, object]] = {} diff --git a/tests/unit/litellm_core_utils/test_exception_mapping_utils.py b/tests/unit/litellm_core_utils/test_exception_mapping_utils.py index 55e7c47e2c9..19e5387da9a 100644 --- a/tests/unit/litellm_core_utils/test_exception_mapping_utils.py +++ b/tests/unit/litellm_core_utils/test_exception_mapping_utils.py @@ -24,6 +24,7 @@ from litellm.llms.base_llm.chat.transformation import BaseLLMException from litellm.llms.bedrock.common_utils import BedrockError from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler from litellm.llms.openai.common_utils import OpenAIError +from litellm.proxy.common_utils.openai_error_payload import openai_error_type from litellm.types.utils import LlmProviders @@ -1015,6 +1016,138 @@ def test_azure_404_with_invalid_request_error_type_maps_to_not_found(): assert "Response with id 'resp_abc' not found." in excinfo.value.message +@pytest.mark.parametrize("provider", ["openai", "groq", "deepseek"]) +@pytest.mark.parametrize( + "status_code, message, expected_exception", + [ + (400, "Invalid 'temperature'", litellm.BadRequestError), + (401, "Invalid API key", litellm.AuthenticationError), + (404, "The model does not exist", litellm.NotFoundError), + ], +) +def test_openai_invalid_request_error_respects_status_code( + provider: str, status_code: int, message: str, expected_exception: type[openai.APIStatusError] +) -> None: + error_message: Final = json.dumps({"error": {"message": message, "type": "invalid_request_error"}}) + original_exception: Final = OpenAIError(status_code=status_code, message=error_message, headers={}) + + with pytest.raises(openai.APIError) as excinfo: + exception_type( + model="some-model", + original_exception=original_exception, + custom_llm_provider=provider, + ) + + assert type(excinfo.value) is expected_exception + assert excinfo.value.status_code == status_code + assert message in excinfo.value.message + + +@pytest.mark.parametrize("provider", ["openai", "groq", "deepseek"]) +def test_openai_invalid_request_error_without_a_status_code_maps_to_bad_request(provider: str) -> None: + message: Final = "Missing required parameter: 'input'." + original_exception: Final = Exception(json.dumps({"error": {"message": message, "type": "invalid_request_error"}})) + + with pytest.raises(openai.APIError) as excinfo: + exception_type( + model="some-model", + original_exception=original_exception, + custom_llm_provider=provider, + ) + + assert type(excinfo.value) is litellm.BadRequestError + assert message in excinfo.value.message + + +@pytest.mark.parametrize("provider", ["openai", "groq", "deepseek"]) +@pytest.mark.parametrize("status_code", [403, 409, 422, 429, 500, 503]) +def test_openai_invalid_request_error_keeps_bad_request_for_statuses_other_than_401_and_404( + provider: str, status_code: int +) -> None: + message: Final = "Cannot cancel a batch that is already completed." + error_message: Final = json.dumps({"error": {"message": message, "type": "invalid_request_error"}}) + original_exception: Final = OpenAIError(status_code=status_code, message=error_message, headers={}) + + with pytest.raises(openai.APIError) as excinfo: + exception_type( + model="some-model", + original_exception=original_exception, + custom_llm_provider=provider, + ) + + assert type(excinfo.value) is litellm.BadRequestError + assert message in excinfo.value.message + + +def _openai_sdk_error( + sdk_error: type[openai.APIStatusError], status_code: int, provider_error: dict[str, str | None] +) -> openai.APIStatusError: + request: Final = httpx.Request("POST", "https://api.openai.com/v1/chat/completions") + return sdk_error( + f"Error code: {status_code} - {{'error': {provider_error}}}", + response=httpx.Response(status_code, request=request), + body=provider_error, + ) + + +@pytest.mark.parametrize( + "sdk_error, status_code, expected_exception, param, code", + [ + (openai.AuthenticationError, 401, litellm.AuthenticationError, "organization", "mismatched_organization"), + (openai.NotFoundError, 404, litellm.NotFoundError, "id", None), + ], +) +def test_openai_invalid_request_error_401_and_404_keep_the_provider_error_fields( + sdk_error: type[openai.APIStatusError], + status_code: int, + expected_exception: type[openai.APIStatusError], + param: str, + code: str | None, +) -> None: + provider_error: Final = { + "message": "The request was rejected.", + "type": "invalid_request_error", + "param": param, + "code": code, + } + + with pytest.raises(openai.APIError) as excinfo: + exception_type( + model="gpt-5-mini", + original_exception=_openai_sdk_error(sdk_error, status_code, provider_error), + custom_llm_provider="openai", + ) + + assert type(excinfo.value) is expected_exception + assert (excinfo.value.status_code, excinfo.value.type, excinfo.value.param, excinfo.value.code) == ( + status_code, + "invalid_request_error", + param, + code, + ) + assert excinfo.value.body == provider_error + assert openai_error_type(excinfo.value, status_code) == "invalid_request_error" + assert excinfo.value.message.count(expected_exception.__name__) == 1 + + +def test_openai_incorrect_api_key_keeps_the_proxy_authentication_error_type() -> None: + provider_error: Final = { + "message": "Incorrect API key provided: sk-litellmqa.", + "type": "invalid_request_error", + "param": None, + "code": "invalid_api_key", + } + + with pytest.raises(litellm.AuthenticationError) as excinfo: + exception_type( + model="gpt-5-mini", + original_exception=_openai_sdk_error(openai.AuthenticationError, 401, provider_error), + custom_llm_provider="openai", + ) + + assert openai_error_type(excinfo.value, 401) == "authentication_error" + + class _UpstreamHTTPError(Exception): def __init__(self, status_code: int) -> None: super().__init__("upstream failure") diff --git a/tests/unit/litellm_core_utils/test_health_check_helpers.py b/tests/unit/litellm_core_utils/test_health_check_helpers.py index 44b36a355c6..6979d72ff14 100644 --- a/tests/unit/litellm_core_utils/test_health_check_helpers.py +++ b/tests/unit/litellm_core_utils/test_health_check_helpers.py @@ -795,9 +795,12 @@ async def test_ahealth_check_keeps_the_noul_probe_for_other_decisions_providers( ("us.anthropic.claude-haiku-4-5-20251001-v1:0", "bedrock", None), ("claude-haiku-4-5", "anthropic", None), ("anthropic.claude-haiku-4-5", None, None), + ("stt-rt-v5", "soniox", "realtime"), + ("stt-rt-preview", "soniox", "realtime"), + ("stt-async-v5", "soniox", None), ), ) -def test_native_health_check_mode_is_messages_only_for_mantle_claude( +def test_native_health_check_mode_routes_mantle_claude_to_messages_and_soniox_realtime_models_to_realtime( model: str, custom_llm_provider: str | None, expected: str | None ) -> None: assert native_health_check_mode(model=model, custom_llm_provider=custom_llm_provider) == expected diff --git a/tests/unit/litellm_core_utils/test_litellm_logging.py b/tests/unit/litellm_core_utils/test_litellm_logging.py index 299088a6c4f..a6d99a5ba55 100644 --- a/tests/unit/litellm_core_utils/test_litellm_logging.py +++ b/tests/unit/litellm_core_utils/test_litellm_logging.py @@ -50,6 +50,7 @@ from litellm.litellm_core_utils.litellm_logging import ( get_standard_logging_object_payload, set_callbacks, ) +from litellm.litellm_core_utils.llm_response_utils.response_metadata import update_response_metadata from litellm.litellm_core_utils.logging_worker import GLOBAL_LOGGING_WORKER from litellm.llms.base_llm.ocr.transformation import OCRUsageInfo from litellm.proxy._types import UserAPIKeyAuth @@ -1118,6 +1119,115 @@ class TestZeroCostDiagnostic: litellm.model_cost.pop(served_model, None) +class TestPerSecondMessagesCost: + DEPLOYMENT_ID: Final = "per-second-priced-deployment" + MODEL_GROUP: Final = "per-second-priced-claude" + RATE: Final = 0.00042 + PER_SECOND_PRICING: Final = {"input_cost_per_second": RATE, "output_cost_per_second": RATE} + MESSAGES_RESPONSE: Final = { + "id": "msg_per-second-call", + "type": "message", + "role": "assistant", + "model": "claude-haiku-5-5", + "content": [{"type": "text", "text": "Hi there friend"}], + "stop_reason": "end_turn", + "stop_sequence": None, + "usage": {"input_tokens": 10, "output_tokens": 5}, + } + + @pytest.fixture(autouse=True) + def registered_deployment(self) -> Iterator[None]: + litellm.register_model(model_cost={self.DEPLOYMENT_ID: self.PER_SECOND_PRICING}, persist_across_reloads=False) + try: + yield + finally: + litellm.model_cost.pop(self.DEPLOYMENT_ID, None) + + def _routed_messages_call( + self, *, stream: bool, received_seconds_before_start: float | None = None + ) -> LitellmLogging: + started: Final = datetime.datetime.now() + proxy_receive_stamp: Final = ( + {} + if received_seconds_before_start is None + else { + "litellm_received_at": (started - datetime.timedelta(seconds=received_seconds_before_start)).astimezone( + datetime.timezone.utc + ) + } + ) + logging_obj: Final = LitellmLogging( + model=self.MODEL_GROUP, + messages=[{"role": "user", "content": "Say hi in three words"}], + stream=stream, + call_type="anthropic_messages", + start_time=started, + litellm_call_id="per-second-call", + function_id="fn", + kwargs={"model": self.MODEL_GROUP}, + ) + logging_obj.update_from_kwargs( + kwargs={ + "model": "anthropic/claude-haiku-5-5", + "litellm_metadata": { + "deployment": "anthropic/claude-haiku-5-5", + "model_info": {"id": self.DEPLOYMENT_ID}, + **proxy_receive_stamp, + }, + }, + model="claude-haiku-5-5", + optional_params={"max_tokens": 32}, + litellm_params={ + "preset_cache_key": None, + "stream_response": {}, + "model_info": None, + **self.PER_SECOND_PRICING, + "max_tokens": 32, + }, + custom_llm_provider="anthropic", + ) + return logging_obj + + @staticmethod + def _response_handed_back_after(logging_obj: LitellmLogging, seconds: float) -> None: + started: Final = logging_obj.start_time + update_response_metadata( + result=dict(TestPerSecondMessagesCost.MESSAGES_RESPONSE), + logging_obj=logging_obj, + model="claude-haiku-5-5", + kwargs={}, + start_time=started, + end_time=started + datetime.timedelta(seconds=seconds), + ) + + def test_messages_call_prices_the_reported_response_time_however_late_the_cost_is_read(self) -> None: + logging_obj: Final = self._routed_messages_call(stream=False) + self._response_handed_back_after(logging_obj, seconds=1.0) + result: Final = logging_obj._handle_anthropic_messages_response_logging(result=dict(self.MESSAGES_RESPONSE)) + cost_for_the_response_header: Final = logging_obj._response_cost_calculator(result=result) + logging_obj.model_call_details["end_time"] = logging_obj.start_time + datetime.timedelta(seconds=1.5) + cost_for_the_spend_log: Final = logging_obj._response_cost_calculator(result=result) + + assert cost_for_the_response_header == pytest.approx(self.RATE) + assert cost_for_the_spend_log == pytest.approx(self.RATE) + + def test_messages_call_bills_from_logging_setup_not_from_proxy_receive(self) -> None: + logging_obj: Final = self._routed_messages_call(stream=False, received_seconds_before_start=2.0) + self._response_handed_back_after(logging_obj, seconds=1.0) + result: Final = logging_obj._handle_anthropic_messages_response_logging(result=dict(self.MESSAGES_RESPONSE)) + + assert logging_obj.response_timing_metrics["_response_ms"] == pytest.approx(3000) + assert logging_obj._response_cost_calculator(result=result) == pytest.approx(self.RATE) + + def test_streamed_messages_call_bills_the_whole_stream_not_the_first_byte(self) -> None: + logging_obj: Final = self._routed_messages_call(stream=True) + self._response_handed_back_after(logging_obj, seconds=0.25) + logging_obj.model_call_details["end_time"] = logging_obj.start_time + datetime.timedelta(seconds=1.0) + result: Final = logging_obj._handle_anthropic_messages_response_logging(result=dict(self.MESSAGES_RESPONSE)) + + assert logging_obj._response_cost_calculator(result=result) == pytest.approx(self.RATE) + + class TestGetRouterModelId: """Tests for the get_router_model_id helper method.""" @@ -3723,9 +3833,9 @@ def test_get_extra_header_tags_returns_none_for_absent_or_empty_header( monkeypatch.setattr(litellm, "extra_spend_tag_headers", ["x-org-id"], raising=False) assert StandardLoggingPayloadSetup._get_extra_header_tags(proxy_server_request={"headers": {}}) is None - assert StandardLoggingPayloadSetup._get_extra_header_tags( - proxy_server_request={"headers": {"x-org-id": ""}} - ) is None + assert ( + StandardLoggingPayloadSetup._get_extra_header_tags(proxy_server_request={"headers": {"x-org-id": ""}}) is None + ) def test_get_extra_header_tags_preserves_configured_header_order( @@ -11939,7 +12049,7 @@ def test_get_additional_headers(): def all_fields_present(standard_logging_metadata: StandardLoggingMetadata): - for field in StandardLoggingMetadata.__annotations__.keys(): + for field in StandardLoggingMetadata.__required_keys__: assert field in standard_logging_metadata @@ -11975,6 +12085,26 @@ def test_get_standard_logging_metadata(metadata_key, metadata_value): assert standard_logging_metadata[metadata_key] == metadata_value +@pytest.mark.usefixtures("_vcr_outcome_gate", "drain_logging_worker", "isolate_litellm_state", "setup_and_teardown") +def test_module_level_get_standard_logging_metadata_drops_only_a_caller_sent_silent_experiment_flag(): + from litellm.litellm_core_utils.litellm_logging import get_standard_logging_metadata + + caller_metadata: Final = { + "is_silent_experiment": True, + "user_api_key_alias": "alias", + "usage_object": {"total_tokens": 3}, + "used_client_oauth_token": True, + "kubernetes_pod_routing": {"pod": "a"}, + } + + result = get_standard_logging_metadata(caller_metadata) + + assert "is_silent_experiment" not in result + assert {key: result[key] for key in caller_metadata.keys() - {"is_silent_experiment"}} == { + key: value for key, value in caller_metadata.items() if key != "is_silent_experiment" + } + + @pytest.mark.usefixtures("_vcr_outcome_gate", "drain_logging_worker", "isolate_litellm_state", "setup_and_teardown") def test_get_standard_logging_metadata_user_api_key_hash(): valid_hash = "a" * 64 # 64 character string @@ -13801,7 +13931,9 @@ def test_record_native_bedrock_guardrail_bills_each_call_its_own_trace_when_call ) logging_obj.model_call_details["litellm_params"] = {"metadata": shared_metadata} logging_obj.record_native_bedrock_guardrail({"trace": trace}) - recorded = logging_obj.model_call_details["litellm_params"]["metadata"]["standard_logging_guardrail_information"] + recorded = logging_obj.model_call_details["litellm_params"]["metadata"][ + "standard_logging_guardrail_information" + ] costs.append(guardrail_information_cost(recorded)) (first_cost, second_cost) = costs diff --git a/tests/unit/litellm_core_utils/test_logging_worker.py b/tests/unit/litellm_core_utils/test_logging_worker.py index f48495d1062..fccc11617bb 100644 --- a/tests/unit/litellm_core_utils/test_logging_worker.py +++ b/tests/unit/litellm_core_utils/test_logging_worker.py @@ -82,6 +82,252 @@ class _RecordCollector(logging.Handler): self.records.append(record) +@pytest.mark.asyncio +@pytest.mark.parametrize("running", [False, True]) +async def test_stop_discards_waiting_callbacks_without_executing_them(running: bool) -> None: + worker: Final = LoggingWorker(concurrency=1) + started: Final = asyncio.Event() + cancelled: Final = asyncio.Event() + executed: Final = asyncio.Event() + + async def blocked() -> None: + started.set() + try: + await asyncio.Event().wait() + finally: + cancelled.set() + + async def waiting() -> None: + executed.set() + + if running: + worker.ensure_initialized_and_enqueue(blocked()) + await started.wait() + else: + worker._ensure_queue() + worker.enqueue(waiting()) + await worker.stop(discard_pending=True) + assert not executed.is_set() + assert cancelled.is_set() == running + assert worker._queue is not None + assert worker._queue.empty() + await asyncio.wait_for(worker._queue.join(), timeout=1) + + +@pytest.mark.asyncio +async def test_stop_does_not_replay_a_callback_dequeued_before_its_processor_started() -> None: + worker: Final = LoggingWorker(concurrency=1) + executed: Final = asyncio.Event() + + async def callback() -> None: + executed.set() + + worker.ensure_initialized_and_enqueue(callback()) + await asyncio.sleep(0) + await worker.stop(discard_pending=True) + assert not executed.is_set() + worker._flush_on_exit() + assert not executed.is_set() + assert worker._queue is not None + await asyncio.wait_for(worker._queue.join(), timeout=1) + + +@pytest.mark.asyncio +async def test_stop_timeout_keeps_callbacks_observable_until_queue_cleanup_finishes() -> None: + worker: Final = LoggingWorker(concurrency=1) + started: Final = asyncio.Event() + release: Final = asyncio.Event() + completed: Final = asyncio.Event() + + async def running() -> None: + started.set() + await asyncio.Event().wait() + + async def queued() -> None: + await release.wait() + completed.set() + + worker.ensure_initialized_and_enqueue(running()) + await started.wait() + worker.enqueue(queued()) + try: + await worker.stop(timeout=0) + assert not completed.is_set() + release.set() + await worker.flush() + assert completed.is_set(), "Queue cleanup must survive a bounded cancellation wait" + finally: + release.set() + await worker.stop() + + +@pytest.mark.asyncio +async def test_overflow_callback_does_not_undercount_the_unfinished_queue() -> None: + """A callback rejected by a full queue is run by the aggressive clear. It must not consume a + queue.task_done() it never contributed, otherwise flush() returns while a real callback is still + running and the shutdown drain cancels it with budget to spare. + """ + worker: Final = LoggingWorker(timeout=5.0, max_queue_size=2, concurrency=1) + gates: Final = {name: asyncio.Event() for name in ("first", "second", "third")} + completed: Final[list[str]] = [] + + async def gated(name: str) -> None: + await gates[name].wait() + completed.append(name) + + async def instant() -> None: + completed.append("overflow") + + worker.start() + worker.enqueue(gated("first")) + await asyncio.sleep(0) + worker.enqueue(gated("second")) + worker.enqueue(gated("third")) + assert worker._queue is not None and worker._queue.full(), "precondition: the queue is full" + worker.enqueue(instant()) + for _ in range(5): + await asyncio.sleep(0) + + gates["first"].set() + gates["second"].set() + asyncio.get_running_loop().call_later(0.2, gates["third"].set) + await asyncio.wait_for(worker.flush(), timeout=2.0) + assert "third" in completed, "flush returned while a queued callback was still unfinished" + assert sorted(completed) == ["first", "overflow", "second", "third"] + await worker.stop() + + +@pytest.mark.asyncio +@pytest.mark.parametrize("queue_size", (1, 2)) +async def test_flush_waits_for_delayed_overflow_and_preserves_callback_context(queue_size: int) -> None: + retry_started: Final = asyncio.Event() + release_retry: Final = asyncio.Event() + completed: Final[asyncio.Queue[tuple[int, str]]] = asyncio.Queue() + identity: Final = contextvars.ContextVar("overflow_identity", default="outside") + + async def retry_sleep(delay: float) -> None: + retry_started.set() + await release_retry.wait() + + worker: Final = LoggingWorker(max_queue_size=queue_size, concurrency=1, retry_sleep=retry_sleep) + + async def callback(index: int) -> None: + completed.put_nowait((index, identity.get())) + + worker.start() + token: Final = identity.set("request") + for index in range(4): + worker.enqueue(callback(index)) + identity.reset(token) + flushing: Final = asyncio.create_task(worker.flush()) + try: + await asyncio.wait_for(retry_started.wait(), timeout=2) + assert worker._queue is not None + await asyncio.wait_for(worker._queue.join(), timeout=2) + await asyncio.sleep(0) + assert not flushing.done(), "Queue completion must not hide delayed overflow callbacks" + assert worker.pending_count > 0 + release_retry.set() + await asyncio.wait_for(flushing, timeout=2) + assert sorted(completed.get_nowait() for _ in range(completed.qsize())) == [ + (index, "request") for index in range(4) + ] + assert worker.pending_count == 0 + finally: + release_retry.set() + await worker.stop(discard_pending=True) + await asyncio.gather(flushing, return_exceptions=True) + + +@pytest.mark.asyncio +@pytest.mark.parametrize("start_retry", (False, True)) +async def test_shutdown_discards_overflow_without_late_enqueue_or_replay(start_retry: bool) -> None: + import inspect + + retry_started: Final = asyncio.Event() + release_retry: Final = asyncio.Event() + executed: Final[asyncio.Queue[int]] = asyncio.Queue() + + async def retry_sleep(delay: float) -> None: + retry_started.set() + await release_retry.wait() + + worker: Final = LoggingWorker(max_queue_size=2, concurrency=1, retry_sleep=retry_sleep) + + async def callback(index: int) -> None: + executed.put_nowait(index) + + callbacks: Final = tuple(callback(index) for index in range(4)) + worker._ensure_queue() + for callback_coro in callbacks: + worker.enqueue(callback_coro) + if start_retry: + await asyncio.wait_for(retry_started.wait(), timeout=2) + await worker.stop(discard_pending=True, timeout=1) + completed_at_stop: Final = executed.qsize() + release_retry.set() + await worker.flush() + try: + assert executed.qsize() == completed_at_stop, "Discarded overflow must not run when the worker restarts" + assert worker.pending_count == 0 + assert all(inspect.getcoroutinestate(coro) == inspect.CORO_CLOSED for coro in callbacks) + finally: + await worker.stop(discard_pending=True) + + +@pytest.mark.asyncio +async def test_shutdown_cancels_aggressive_clear_callbacks_before_returning() -> None: + started: Final = asyncio.Event() + cancelled: Final = asyncio.Event() + worker: Final = LoggingWorker(max_queue_size=2, concurrency=1) + + async def blocked() -> None: + started.set() + try: + await asyncio.Event().wait() + finally: + cancelled.set() + + async def noop() -> None: + pass + + worker._ensure_queue() + worker.enqueue(blocked()) + worker.enqueue(noop()) + worker.enqueue(noop()) + await asyncio.wait_for(started.wait(), timeout=2) + await worker.stop(discard_pending=True, timeout=1) + assert cancelled.is_set(), "Overflow execution must participate in shutdown cancellation" + assert worker.pending_count == 0 + assert worker._queue is not None + await asyncio.wait_for(worker._queue.join(), timeout=1) + + +@pytest.mark.asyncio +async def test_stopped_overflow_can_be_flushed_when_not_discarded() -> None: + release_retry: Final = asyncio.Event() + completed: Final[asyncio.Queue[int]] = asyncio.Queue() + + async def retry_sleep(delay: float) -> None: + await release_retry.wait() + + worker: Final = LoggingWorker(max_queue_size=2, concurrency=1, retry_sleep=retry_sleep) + + async def callback(index: int) -> None: + completed.put_nowait(index) + + worker._ensure_queue() + for index in range(4): + worker.enqueue(callback(index)) + await worker.stop() + release_retry.set() + try: + await asyncio.wait_for(worker.flush(), timeout=2) + assert sorted(completed.get_nowait() for _ in range(completed.qsize())) == list(range(4)) + finally: + await worker.stop(discard_pending=True) + + class TestLoggingWorker: """Test cases for LoggingWorker functionality.""" diff --git a/tests/unit/litellm_core_utils/test_realtime_streaming.py b/tests/unit/litellm_core_utils/test_realtime_streaming.py index 7d85da3f756..a8543ebcfc2 100644 --- a/tests/unit/litellm_core_utils/test_realtime_streaming.py +++ b/tests/unit/litellm_core_utils/test_realtime_streaming.py @@ -24,6 +24,7 @@ from litellm.litellm_core_utils.realtime_streaming import ( client_sent_openai_beta_realtime_header, ) from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj +from litellm.llms.base_llm.realtime.transcription_protocol import RealtimeTranscriptionProtocolError from litellm.llms.base_llm.realtime.transformation import BaseRealtimeConfig from litellm.llms.xai.realtime.transformation import XAIRealtimeNormalizer from litellm.llms.xai.realtime.usage_meter import XAIRealtimeUsageMeter @@ -827,9 +828,7 @@ async def test_usage_meter_snapshot_stored_in_model_call_details(): backend_ws.send = AsyncMock() backend_ws.recv = AsyncMock( side_effect=[ - json.dumps( - {"type": "session.created", "session": {"turn_detection": {"type": None}}} - ).encode(), + json.dumps({"type": "session.created", "session": {"turn_detection": {"type": None}}}).encode(), json.dumps( {"type": "response.output_audio.delta", "delta": base64.b64encode(b"x" * 24000).decode()} ).encode(), @@ -3187,6 +3186,7 @@ async def test_session_close_flushes_unbilled_transcription_usage(): provider_config: Final = MagicMock() provider_config.transform_realtime_response = MagicMock(return_value=transcript_frame) provider_config.unbilled_usage_on_session_close = MagicMock(return_value=usage) + provider_config.session_refusal = MagicMock(return_value=None) streaming: Final = RealTimeStreaming( client_ws, @@ -3320,10 +3320,14 @@ def _backend_ws_closing_with(*frames: bytes | Exception) -> MagicMock: return backend_ws -def _relay_session(client_ws: MagicMock, backend_ws: MagicMock) -> _RelaySession: +def _relay_session( + client_ws: MagicMock, backend_ws: MagicMock, provider_config: BaseRealtimeConfig | None = None +) -> _RelaySession: logging: Final = _RecordingLogging() worker: Final = _InlineLoggingWorker() - streaming: Final = RealTimeStreaming(client_ws, backend_ws, logging, model="gpt-realtime", logging_worker=worker) + streaming: Final = RealTimeStreaming( + client_ws, backend_ws, logging, provider_config=provider_config, model="gpt-realtime", logging_worker=worker + ) return _RelaySession(streaming=streaming, logging=logging, worker=worker) @@ -3408,6 +3412,103 @@ async def test_upstream_refusal_before_any_frame_logs_a_failure_not_a_success(): assert session.logging.logged_sessions == () +class _RefusingRealtimeConfig(_PassthroughRealtimeConfig): + def __init__(self, refusal: Exception | None) -> None: + self._refusal: Final = refusal + + def requires_session_configuration(self) -> bool: + return True + + def session_refusal(self) -> Exception | None: + return self._refusal + + +@pytest.mark.asyncio +async def test_a_session_the_provider_refused_in_band_logs_a_failure_even_after_a_normal_close(): + refusal: Final = ValueError("Soniox realtime error 401: Invalid API key.") + session: Final = _relay_session( + _client_ws_that_never_sends(), + _backend_ws_closing_with( + json.dumps({"type": "error", "error": {"message": str(refusal)}}).encode(), + ConnectionClosed(Close(1000, ""), None), + ), + _RefusingRealtimeConfig(refusal), + ) + + await session.run() + + assert session.logging.logged_failures == (refusal,) + assert session.logging.logged_sessions == () + + +@pytest.mark.asyncio +async def test_a_provider_session_without_a_refusal_logs_a_success_after_a_normal_close(): + session: Final = _relay_session( + _client_ws_that_never_sends(), + _backend_ws_closing_with( + json.dumps({"type": "session.created", "session": {"id": "sess_1"}}).encode(), + ConnectionClosed(Close(1000, ""), None), + ), + _RefusingRealtimeConfig(None), + ) + + await session.run() + + assert session.logging.logged_failures == () + assert len(session.logging.logged_sessions) == 1 + + +class _TranscriptionConfigRejectingSessionUpdates(_PassthroughRealtimeConfig): + def __init__(self, *, defers_setup: bool) -> None: + self._defers_setup: Final = defers_setup + + def requires_session_configuration(self) -> bool: + return not self._defers_setup + + def transform_realtime_request( + self, message: str, model: str, session_configuration_request: str | None = None + ) -> Sequence[str | bytes]: + if json.loads(message).get("type") == "session.update": + raise RealtimeTranscriptionProtocolError("requires pcm16 input audio") + return (message,) + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "defers_setup,client_frames_read,audio_reaches_backend", + ((True, 1, False), (False, 3, True)), + ids=["deferred_setup_session_ends", "eager_setup_session_continues"], +) +async def test_a_rejected_transcription_session_update_sends_an_error_and_ends_only_a_deferred_setup_session( + defers_setup: bool, client_frames_read: int, audio_reaches_backend: bool +): + append: Final = json.dumps({"type": "input_audio_buffer.append", "audio": "AAE="}) + client_ws: Final = MagicMock() + client_ws.headers = {} + client_ws.receive_text = AsyncMock( + side_effect=[json.dumps({"type": "session.update", "session": {"type": "transcription"}}), append, RuntimeError] + ) + client_ws.send_text = AsyncMock() + backend_ws: Final = MagicMock() + backend_ws.send = AsyncMock() + streaming: Final = RealTimeStreaming( + client_ws, + backend_ws, + _RecordingLogging(), + provider_config=_TranscriptionConfigRejectingSessionUpdates(defers_setup=defers_setup), + model="chirp_3", + logging_worker=_InlineLoggingWorker(), + ) + + await asyncio.wait_for(streaming.client_ack_messages(), timeout=2) + + assert _error_events_sent_to(client_ws) == [ + {"type": "error", "error": {"type": "invalid_request_error", "message": "requires pcm16 input audio"}} + ] + assert client_ws.receive_text.await_count == client_frames_read + assert (append in [call.args[0] for call in backend_ws.send.await_args_list]) is audio_reaches_backend + + @pytest.mark.asyncio async def test_upstream_refusal_after_a_synthetic_session_created_still_logs_a_failure(): """LIT-6973: deferred Gemini Live setup stores a synthetic ``session.created`` before @@ -4056,9 +4157,7 @@ _XAI_TYPED_VAD_SESSION: Final = { pytest.param(({"type": "realtime", "instructions": "hi"},), id="no-turn-detection"), pytest.param(({"type": "realtime", "audio": {"input": {"turn_detection": None}}},), id="push-to-talk"), pytest.param((_XAI_TYPED_VAD_SESSION,), id="typed-server-vad"), - pytest.param( - ({"type": "realtime", "instructions": "hi"}, _XAI_TYPED_VAD_SESSION), id="later-typed-server-vad" - ), + pytest.param(({"type": "realtime", "instructions": "hi"}, _XAI_TYPED_VAD_SESSION), id="later-typed-server-vad"), ], ) async def test_xai_session_update_gate_leaves_out_the_vad_type( @@ -4070,9 +4169,9 @@ async def test_xai_session_update_gate_leaves_out_the_vad_type( monkeypatch, *client_sessions, event_normalizer=XAIRealtimeNormalizer() ) - assert [session["audio"]["input"]["turn_detection"] for session in forwarded] == [ - {"create_response": False} - ] * (len(client_sessions) + 1) + assert [session["audio"]["input"]["turn_detection"] for session in forwarded] == [{"create_response": False}] * ( + len(client_sessions) + 1 + ) @pytest.mark.asyncio @@ -4206,7 +4305,9 @@ async def test_client_never_sees_the_error_caused_by_the_guardrail_cancel(monkey await RealTimeStreaming(client_ws, backend_ws, logging_obj).backend_to_client_send_messages() client_errors: Final = [ - event["error"] for call in client_ws.send_text.await_args_list if (event := json.loads(call.args[0]))["type"] == "error" + event["error"] + for call in client_ws.send_text.await_args_list + if (event := json.loads(call.args[0]))["type"] == "error" ] assert [(error["code"], error.get("event_id")) for error in client_errors] == [ ("content_policy_violation", None), diff --git a/tests/unit/litellm_core_utils/test_served_output_texts.py b/tests/unit/litellm_core_utils/test_served_output_texts.py index 637a20571c8..e01c710dfdb 100644 --- a/tests/unit/litellm_core_utils/test_served_output_texts.py +++ b/tests/unit/litellm_core_utils/test_served_output_texts.py @@ -9,7 +9,16 @@ from litellm.litellm_core_utils.served_output_texts import ( served_output_texts, served_stream_output_texts, ) -from litellm.types.utils import Choices, Delta, Message, ModelResponse, ModelResponseStream, StreamingChoices +from litellm.types.utils import ( + Choices, + Delta, + Message, + ModelResponse, + ModelResponseStream, + StreamingChoices, + TextChoices, + TextCompletionResponse, +) RAW = "Card: 4111 1111 1111 1111" MASKED = "Card: " @@ -83,6 +92,30 @@ def test_served_stream_output_texts_keeps_every_choice_index_when_chunks_carry_o assert served_stream_output_texts(chunks) == ("first choice", MASKED) +def _text_completion_chunk(text: str | None, index: int = 0) -> TextCompletionResponse: + return TextCompletionResponse(object="text_completion", choices=[TextChoices(index=index, text=text)]) + + +def test_served_stream_output_texts_joins_text_completion_chunks_per_choice(): + chunks = [ + _text_completion_chunk("Card: ", 0), + _text_completion_chunk("second", 1), + _text_completion_chunk("", 0), + _text_completion_chunk(None, 1), + TextCompletionResponse(object="text_completion", choices=[]), + ] + assert served_stream_output_texts(chunks) == (MASKED, "second") + + +def test_overlay_replaces_text_completion_choice_text_with_served_text(): + logged = {"id": "x", "object": "text_completion", "choices": [{"index": 0, "text": RAW}, {"index": 1, "text": RAW}]} + assert overlay_served_output_texts(logged, (MASKED,)) == { + "id": "x", + "object": "text_completion", + "choices": [{"index": 0, "text": MASKED}, {"index": 1, "text": RAW}], + } + + @pytest.mark.parametrize("content", (None, "", "first")) def test_null_choice_index_joins_choice_zero_without_changing_other_choices(content: str | None) -> None: chunks: Final = ( diff --git a/tests/unit/litellm_core_utils/test_text_completion_conversion.py b/tests/unit/litellm_core_utils/test_text_completion_conversion.py index 4a2f91093a3..8a913bc8058 100644 --- a/tests/unit/litellm_core_utils/test_text_completion_conversion.py +++ b/tests/unit/litellm_core_utils/test_text_completion_conversion.py @@ -1,10 +1,12 @@ import asyncio +from typing import Final from unittest.mock import MagicMock, patch import pytest import litellm from litellm import text_completion +from litellm.llms.custom_llm import CustomLLM from litellm.types.utils import ModelResponse, TextCompletionResponse, Usage from litellm.utils import LiteLLMResponseObjectHandler @@ -1298,3 +1300,30 @@ def mock_post(*args, **kwargs): "usage": {"prompt_tokens": 2, "total_tokens": 18, "completion_tokens": 16}, } return mock_response + + +def test_text_completion_reads_a_custom_provider_dict_reply(monkeypatch: pytest.MonkeyPatch) -> None: + class DictReplyLLM(CustomLLM): + def completion(self, *args: object, **kwargs: object) -> dict[str, object]: + return { + "id": "dict-reply", + "object": "chat.completion", + "created": 1, + "model": "x", + "choices": [ + { + "index": 0, + "message": {"role": "assistant", "content": "hi from a dict reply"}, + "finish_reason": "stop", + } + ], + } + + monkeypatch.setattr(litellm, "custom_provider_map", [{"provider": "dictreply", "custom_handler": DictReplyLLM()}]) + monkeypatch.setattr(litellm, "provider_list", [*litellm.provider_list, "dictreply"]) + monkeypatch.setattr(litellm, "_custom_providers", [*litellm._custom_providers, "dictreply"]) + + response: Final = text_completion(model="dictreply/x", prompt="hi") + + assert response.choices[0].text == "hi from a dict reply" + assert response.choices[0].finish_reason == "stop" diff --git a/tests/unit/litellm_proxy_extras/test_migration_data_checks.py b/tests/unit/litellm_proxy_extras/test_migration_data_checks.py index d4490927e29..37377498d89 100644 --- a/tests/unit/litellm_proxy_extras/test_migration_data_checks.py +++ b/tests/unit/litellm_proxy_extras/test_migration_data_checks.py @@ -12,98 +12,15 @@ from litellm_proxy_extras.migration_data_checks import ( _count_query, btree_max_entry_bytes, checks_for_index, - checks_for_migration, - checks_for_statement, migration_data_check_timeout_seconds, report, - statements, ) -MIGRATIONS: Final = Path(__file__).resolve().parents[3] / "litellm-proxy-extras" / "litellm_proxy_extras" / "migrations" -API_KEY_INDEX_AS_SHIPPED_IN_V1_103_0: Final = 'CREATE INDEX IF NOT EXISTS "LiteLLM_SpendLogs_api_key_startTime_idx" ON "LiteLLM_SpendLogs"("api_key", "startTime")' - def _shape(checks: tuple[Check, ...]) -> tuple[tuple[str, str, tuple[str, ...]], ...]: return tuple((check.kind, check.table, check.columns) for check in checks) -@pytest.mark.parametrize( - ("statement", "expected"), - ( - ( - API_KEY_INDEX_AS_SHIPPED_IN_V1_103_0, - (("too_long", "LiteLLM_SpendLogs", ("api_key", "startTime")),), - ), - ( - 'CREATE UNIQUE INDEX CONCURRENTLY "T_a_b_key" ON "public"."T" USING btree ("a", "b" DESC)', - (("too_long", "T", ("a", "b")), ("duplicates", "T", ("a", "b"))), - ), - ('CREATE INDEX "T_a_idx" ON "T" USING gin ("a")', ()), - ('CREATE INDEX "T_a_idx" ON "T" (lower("a"))', ()), - ('ALTER TABLE "T" ALTER COLUMN "a" SET NOT NULL', (("nulls", "T", ("a",)),)), - ('ALTER TABLE "T" ADD COLUMN "a" TEXT NOT NULL', (("any_row", "T", ("a",)),)), - ('ALTER TABLE "T" ADD COLUMN "a" TEXT NOT NULL DEFAULT \'x\'', ()), - ('ALTER TABLE "T" ADD COLUMN "a" TEXT', ()), - ( - 'ALTER TABLE "T" ADD CONSTRAINT "T_r_fkey" FOREIGN KEY ("r") REFERENCES "R"("id") ON DELETE CASCADE', - (("orphans", "T", ("r",)),), - ), - ('ALTER TABLE "T" ADD CONSTRAINT "T_r_fkey" FOREIGN KEY ("r") REFERENCES "R"("id") NOT VALID', ()), - ('ALTER TABLE "T" ADD CONSTRAINT "T_a_key" UNIQUE ("a", "b")', (("duplicates", "T", ("a", "b")),)), - ( - 'ALTER TABLE "T" ADD CONSTRAINT "T_a_check" CHECK ("a" > 0 AND "b" IS NOT NULL)', - (("check_violations", "T", ()),), - ), - ('ALTER TABLE "T" ADD CONSTRAINT "T_a_check" CHECK ("a" > 0) NOT VALID', ()), - ( - 'ALTER TABLE "T" ADD COLUMN "c" TEXT, ALTER COLUMN "b" SET NOT NULL, ADD CONSTRAINT "u" UNIQUE ("c")', - (("nulls", "T", ("b",)), ("duplicates", "T", ("c",))), - ), - ('CREATE TABLE "T" ("id" TEXT NOT NULL, CONSTRAINT "T_pkey" PRIMARY KEY ("id"))', ()), - ('DROP INDEX IF EXISTS "T_a_idx"', ()), - ("SELECT 1", ()), - ), -) -def test_a_statement_gets_one_check_per_way_existing_rows_can_make_it_fail( - statement: str, expected: tuple[tuple[str, str, tuple[str, ...]], ...] -) -> None: - assert _shape(checks_for_statement(statement, "migration x")) == expected - - -def test_a_foreign_key_check_keeps_the_referenced_table_and_columns() -> None: - (check,) = checks_for_statement( - 'ALTER TABLE "T" ADD CONSTRAINT "fk" FOREIGN KEY ("r", "s") REFERENCES "R"("id", "sub")', "migration x" - ) - assert (check.referenced_table, check.referenced_columns) == ("R", ("id", "sub")) - - -def test_a_check_constraint_keeps_its_expression_for_the_count_query() -> None: - (check,) = checks_for_statement('ALTER TABLE "T" ADD CONSTRAINT "c" CHECK ("a" > 0)', "migration x") - assert check.expression == '"a" > 0' - - -def test_comments_and_blank_statements_are_dropped_before_matching() -> None: - migration_sql: Final = "-- CreateIndex\n" + API_KEY_INDEX_AS_SHIPPED_IN_V1_103_0 + ";\n\n-- done; really\n" - assert statements(migration_sql) == (API_KEY_INDEX_AS_SHIPPED_IN_V1_103_0,) - assert _shape(checks_for_migration("x", migration_sql)) == _shape( - checks_for_statement(API_KEY_INDEX_AS_SHIPPED_IN_V1_103_0, "x") - ) - - -def test_every_shipped_migration_is_readable_and_the_later_indexes_on_text_columns_get_a_check() -> None: - derived: Final = { - folder.name: checks_for_migration(folder.name, (folder / "migration.sql").read_text()) - for folder in MIGRATIONS.iterdir() - if folder.is_dir() - } - assert len(derived) > 200 - everywhere: Final = tuple(check for checks in derived.values() for check in checks) # comprehension-ok: flattening - assert any( - check.kind == "too_long" and check.table == "LiteLLM_SpendLogs" and "session_id" in check.columns - for check in everywhere - ) - - def test_a_request_log_index_gets_one_entry_size_check_over_all_its_columns() -> None: assert _shape( checks_for_index("LiteLLM_SpendLogs", "LiteLLM_SpendLogs_api_key_startTime_idx", ("api_key", "startTime")) @@ -142,14 +59,19 @@ def test_the_report_names_the_rows_the_statement_and_the_fix_and_says_nothing_wa assert 'blocks: CREATE INDEX "LiteLLM_SpendLogs_api_key_startTime_idx" (request-log index)' in text assert 'fix: Shorten or hash the long "LiteLLM_SpendLogs" (api_key, startTime) values' in text assert text.startswith("Existing rows would make a pending schema change fail:") - assert "No schema change was made" in text + assert "No index was built" in text def test_a_scan_that_timed_out_is_reported_as_unverified_with_the_knob_to_turn() -> None: - (check,) = checks_for_statement('ALTER TABLE "T" ALTER COLUMN "a" SET NOT NULL', "migration 20270101000000_x") + (check,) = checks_for_index( + "LiteLLM_SpendLogs", "LiteLLM_SpendLogs_api_key_startTime_idx", ("api_key", "startTime") + ) text: Final = report((CouldNotVerify(check, 600.0),)) - assert '"T".a: could not be checked within 600 s, refusing rather than guessing' in text - assert 'blocks: ALTER TABLE "T" ALTER COLUMN "a" SET NOT NULL (migration 20270101000000_x)' in text + assert ( + '"LiteLLM_SpendLogs".api_key, startTime: could not be checked within 600 s, refusing rather than guessing' + in text + ) + assert 'blocks: CREATE INDEX "LiteLLM_SpendLogs_api_key_startTime_idx" (request-log index)' in text assert MIGRATION_DATA_CHECK_TIMEOUT_ENV_VAR in text diff --git a/tests/unit/litellm_proxy_extras/test_migration_dry_run.py b/tests/unit/litellm_proxy_extras/test_migration_dry_run.py new file mode 100644 index 00000000000..75a7fea6c89 --- /dev/null +++ b/tests/unit/litellm_proxy_extras/test_migration_dry_run.py @@ -0,0 +1,80 @@ +from typing import Final + +import pytest +from litellm_proxy_extras.migration_dry_run import Applied, Baseline, DryRun, Rejected, report, transactional + +INDEX: Final = 'CREATE INDEX IF NOT EXISTS "a_idx" ON "T"("c")' + + +@pytest.mark.parametrize( + ("migration_sql", "expected"), + ( + ('CREATE INDEX CONCURRENTLY IF NOT EXISTS "a_idx" ON "T"("c")', INDEX), + ('create unique index concurrently "u" on "T"("d")', 'create unique index "u" on "T"("d")'), + ('DROP INDEX CONCURRENTLY "x"', 'DROP INDEX "x"'), + ('REINDEX INDEX CONCURRENTLY "y"', 'REINDEX INDEX "y"'), + (INDEX, INDEX), + ('ALTER TABLE "T" ALTER COLUMN "c" SET NOT NULL', 'ALTER TABLE "T" ALTER COLUMN "c" SET NOT NULL'), + ("-- built CONCURRENTLY by the index job\n" + INDEX, "-- built CONCURRENTLY by the index job\n" + INDEX), + ), + ids=("concurrently", "unique_lowercase", "drop", "reindex", "plain_index", "alter", "word_in_comment"), +) +def test_only_concurrent_index_statements_change_so_the_migration_can_run_in_a_transaction( + migration_sql: str, expected: str +) -> None: + assert transactional(migration_sql) == expected + + +def test_a_clean_dry_run_lists_each_migration_with_the_time_the_upgrade_will_hold_its_locks() -> None: + text: Final = report(DryRun((Applied("20270101000000_a", 0.04), Applied("20270102000000_b", 12.345)), None)) + assert text.startswith( + "2 pending migration(s) applied one at a time in a dry run, each rolled back before the next" + ) + assert "\n 20270101000000_a: 0.0 s\n 20270102000000_b: 12.3 s" in text + + +def test_no_pending_migration_is_said_plainly() -> None: + assert report(DryRun((), None)) == "No pending migrations, this database is on this version's schema." + + +def test_a_rejected_migration_is_reported_with_postgres_own_words_and_what_ran_before_it() -> None: + error: Final = 'column "team_alias" of relation "LiteLLM_TeamTable" contains null values' + text: Final = report(DryRun((Applied("20270101000000_a", 0.5),), Rejected("20270102000000_b", error, 3.25))) + assert text.startswith( + "Migration 20270102000000_b fails on this database after 3.2 s, nothing was applied.\n" + error + ) + assert text.endswith("Applied before it in the dry run:\n 20270101000000_a: 0.5 s") + + +def test_a_rejection_on_the_first_migration_lists_nothing_before_it() -> None: + text: Final = report(DryRun((), Rejected("20270101000000_a", 'syntax error at or near "NOPE"', 0.0))) + assert "Applied before it" not in text + assert text.endswith('syntax error at or near "NOPE"') + + +def test_a_migration_that_needed_the_one_before_it_says_which_were_replayed_in_front() -> None: + text: Final = report( + DryRun((Applied("20270101000000_a", 0.1), Applied("20270102000000_b", 0.4, ("20270101000000_a",))), None) + ) + assert text.endswith( + "\n 20270101000000_a: 0.1 s\n 20270102000000_b: 0.4 s (depends on 20270101000000_a, replayed in front of it)" + ) + + +def test_a_database_without_history_that_matches_this_build_has_nothing_to_dry_run() -> None: + result: Final = DryRun((), None, Baseline(None)) + assert result.ok is True + assert report(result) == ( + "No migration history and the schema matches this build, the proxy baselines it at boot, nothing to dry-run." + ) + + +def test_a_database_without_history_the_boot_refuses_to_baseline_reports_the_refusal() -> None: + result: Final = DryRun( + (), None, Baseline('the schema differs from this build. prisma migrate diff:\n-- AlterTable\nALTER TABLE "x"') + ) + assert result.ok is False + assert report(result) == ( + "No migration history and the proxy cannot baseline it at boot: " + 'the schema differs from this build. prisma migrate diff:\n-- AlterTable\nALTER TABLE "x"' + ) diff --git a/tests/unit/llms/anthropic/chat/test_anthropic_chat_handler.py b/tests/unit/llms/anthropic/chat/test_anthropic_chat_handler.py index 355d407001c..5d3fc523c0c 100644 --- a/tests/unit/llms/anthropic/chat/test_anthropic_chat_handler.py +++ b/tests/unit/llms/anthropic/chat/test_anthropic_chat_handler.py @@ -537,7 +537,11 @@ def test_streamed_thinking_blocks_accumulate_in_the_openai_sdk_and_replay_withou {"type": "content_block_delta", "index": 2, "delta": {"type": "thinking_delta", "thinking": "Step 3."}}, {"type": "content_block_delta", "index": 2, "delta": {"type": "signature_delta", "signature": "sig-b"}}, {"type": "content_block_stop", "index": 2}, - {"type": "message_delta", "delta": {"stop_reason": "end_turn", "stop_sequence": None}, "usage": {"output_tokens": 9}}, + { + "type": "message_delta", + "delta": {"stop_reason": "end_turn", "stop_sequence": None}, + "usage": {"output_tokens": 9}, + }, {"type": "message_stop"}, ) @@ -558,7 +562,11 @@ def test_streamed_thinking_blocks_accumulate_in_the_openai_sdk_and_replay_withou "thinking_blocks": message["thinking_blocks"], } replayed: Final = anthropic_messages_pt( - messages=[{"role": "user", "content": "Capital of France?"}, next_turn, {"role": "user", "content": "And Spain?"}], + messages=[ + {"role": "user", "content": "Capital of France?"}, + next_turn, + {"role": "user", "content": "And Spain?"}, + ], model="claude-haiku-4-5", llm_provider="anthropic", ) @@ -3650,3 +3658,79 @@ def test_streamed_end_turn_carries_no_stop_details(): provider_fields: Final = assembled.choices[0].message.provider_specific_fields or {} assert "stop_details" not in provider_fields assert "stop_reason" not in provider_fields + + +def _anthropic_chat_request_for_bound_thinking( + extra_headers: dict[str, str] | None, drop_params: bool | None = None +) -> httpx.Request: + captured_requests: Final[list[httpx.Request]] = [] + + def respond(request: httpx.Request) -> httpx.Response: + captured_requests.append(request) + return httpx.Response( + 200, + json={ + "id": "msg_bound", + "type": "message", + "role": "assistant", + "model": "claude-opus-4-8", + "content": [{"type": "text", "text": "Hello"}], + "stop_reason": "end_turn", + "stop_sequence": None, + "usage": {"input_tokens": 1, "output_tokens": 1}, + }, + ) + + thinking: Final = {"type": "adaptive", "block_binding": {"prefix_mismatch_behavior": "error"}} + client: Final = HTTPHandler(client=httpx.Client(transport=httpx.MockTransport(respond))) + try: + litellm.completion( + model="anthropic/claude-opus-4-8", + messages=[{"role": "user", "content": "Hello"}], + api_key="test-key", + client=client, + thinking=thinking, + extra_headers=extra_headers, + drop_params=drop_params, + ) + finally: + client.close() + assert thinking == {"type": "adaptive", "block_binding": {"prefix_mismatch_behavior": "error"}} + return captured_requests[0] + + +def _anthropic_chat_body_for_bound_thinking( + extra_headers: dict[str, str] | None, drop_params: bool | None = None +) -> dict[str, object]: + return json.loads(_anthropic_chat_request_for_bound_thinking(extra_headers, drop_params).content) + + +def test_anthropic_chat_keeps_block_binding_when_the_final_header_carries_the_binding_beta( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setenv("LITELLM_LOCAL_ANTHROPIC_BETA_HEADERS", "true") + body: Final = _anthropic_chat_body_for_bound_thinking( + {"anthropic-beta": "thinking-binding-controls-2026-08-01"}, drop_params=True + ) + assert body["thinking"] == {"type": "adaptive", "block_binding": {"prefix_mismatch_behavior": "error"}} + + +@pytest.mark.parametrize("drop_params", [True, False]) +def test_anthropic_chat_adds_the_binding_beta_so_block_binding_is_kept( + drop_params: bool, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setenv("LITELLM_LOCAL_ANTHROPIC_BETA_HEADERS", "true") + monkeypatch.setattr(litellm, "drop_params", False) + request: Final = _anthropic_chat_request_for_bound_thinking(None, drop_params=drop_params) + assert json.loads(request.content)["thinking"] == { + "type": "adaptive", + "block_binding": {"prefix_mismatch_behavior": "error"}, + } + assert "thinking-binding-controls-2026-08-01" in request.headers["anthropic-beta"].split(",") + + +def test_anthropic_chat_sends_block_binding_as_is_without_drop_params(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setenv("LITELLM_LOCAL_ANTHROPIC_BETA_HEADERS", "true") + monkeypatch.setattr(litellm, "drop_params", False) + body: Final = _anthropic_chat_body_for_bound_thinking(None) + assert body["thinking"] == {"type": "adaptive", "block_binding": {"prefix_mismatch_behavior": "error"}} diff --git a/tests/unit/llms/anthropic/messages/adapters/test_anthropic_messages_adapters_transformation.py b/tests/unit/llms/anthropic/messages/adapters/test_anthropic_messages_adapters_transformation.py index 51ec71aaea0..c30644e7fdc 100644 --- a/tests/unit/llms/anthropic/messages/adapters/test_anthropic_messages_adapters_transformation.py +++ b/tests/unit/llms/anthropic/messages/adapters/test_anthropic_messages_adapters_transformation.py @@ -5286,6 +5286,40 @@ def test_eager_input_streaming_tool_reaches_bedrock_converse_as_beta(): assert data["toolConfig"]["tools"][0]["toolSpec"]["inputSchema"]["json"] == EAGER_INPUT_SCHEMA +@pytest.mark.parametrize( + ("title", "expected_filename"), + [(None, "document.pdf"), ("Quarterly report", "Quarterly report")], +) +def test_user_pdf_document_reaches_openai_compatible_providers_as_file_part( + title: str | None, expected_filename: str +) -> None: + document: Final = _base64_pdf_block() | ({"title": title} if title else {}) + request_messages: Final = _run_chat_completions_pipeline( + [ + AnthropicMessagesUserMessageParam( + role="user", + content=[document, {"type": "text", "text": "What is the codeword?"}], + ) + ] + ) + + assert request_messages == [ + { + "role": "user", + "content": [ + { + "type": "file", + "file": { + "filename": expected_filename, + "file_data": f"data:application/pdf;base64,{TOOL_RESULT_PDF_B64}", + }, + }, + {"type": "text", "text": "What is the codeword?"}, + ], + } + ] + + _CHAT_BRIDGE_MODEL: Final = "azure/gpt-5.6" _CHAT_BRIDGE_API_BASE: Final = "https://tool-result-text.openai.azure.com" _CHAT_BRIDGE_URL: Final = r".*tool-result-text\.openai\.azure\.com/openai/deployments/gpt-5\.6/chat/completions.*" diff --git a/tests/unit/llms/anthropic/messages/native/test_anthropic_messages_router.py b/tests/unit/llms/anthropic/messages/native/test_anthropic_messages_router.py index 19e03d8ce46..596cb968a0e 100644 --- a/tests/unit/llms/anthropic/messages/native/test_anthropic_messages_router.py +++ b/tests/unit/llms/anthropic/messages/native/test_anthropic_messages_router.py @@ -4,8 +4,10 @@ import asyncio import base64 import json import struct +import time import uuid -from collections.abc import AsyncIterable, Mapping +from collections.abc import AsyncIterable, AsyncIterator, Mapping +from datetime import datetime from typing import Final from zlib import crc32 @@ -445,6 +447,135 @@ async def test_router_aanthropic_messages_streaming_logs_usage_model_and_cost( assert payload["completion_tokens"] == max(usage.get("output_tokens", 0) for usage in usages) == 20 +_PER_SECOND_RATE: Final = 0.5 +_UPSTREAM_PAUSE_SECONDS: Final = 0.3 + + +def _priced_router(**pricing: float) -> Router: + return Router( + model_list=[ + { + "model_name": _ALIAS, + "litellm_params": {"model": _ANTHROPIC_MODEL, "api_key": "fake-key", **pricing}, + } + ] + ) + + +def _per_second_router() -> Router: + return _priced_router(input_cost_per_second=_PER_SECOND_RATE, output_cost_per_second=_PER_SECOND_RATE) + + +def _anthropic_sse_event(event: Mapping[str, object]) -> bytes: + return f"event: {event['type']}\ndata: {json.dumps(event)}\n\n".encode() + + +async def _route_like_the_proxy(router: Router, **request: object) -> object: + logging_obj, routed_request = litellm.utils.function_setup( + original_function="aanthropic_messages", + rules_obj=litellm.utils.Rules(), + start_time=datetime.now(), + litellm_call_id=str(uuid.uuid4()), + model=_ALIAS, + max_tokens=100, + **request, + ) + return await router.aanthropic_messages(**routed_request, litellm_logging_obj=logging_obj) + + +async def _sse_pausing_after_the_first_event() -> AsyncIterator[bytes]: + yield _anthropic_sse_event(_STREAM_EVENTS[0]) + await asyncio.sleep(_UPSTREAM_PAUSE_SECONDS) + for event in _STREAM_EVENTS[1:]: + yield _anthropic_sse_event(event) + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "pricing", + [ + {"input_cost_per_second": _PER_SECOND_RATE, "output_cost_per_second": _PER_SECOND_RATE}, + {"cost_per_second": _PER_SECOND_RATE}, + ], +) +async def test_router_aanthropic_messages_bills_a_per_second_deployment_for_the_call_duration( + respx_mock, monkeypatch, pricing: Mapping[str, float] +): + messages: Final = _unique_messages() + recorder: Final = _RecordingLogger(messages) + monkeypatch.setattr(litellm, "callbacks", [recorder]) + + async def slow_upstream(request: httpx.Request) -> httpx.Response: + await asyncio.sleep(_UPSTREAM_PAUSE_SECONDS) + return httpx.Response(200, json=_anthropic_body()) + + respx_mock.post(url__regex=_ANTHROPIC_URL).mock(side_effect=slow_upstream) + started: Final = time.monotonic() + await _route_like_the_proxy(_priced_router(**pricing), messages=messages) + await _wait_for_payload(recorder) + elapsed: Final = time.monotonic() - started + + assert ( + _PER_SECOND_RATE * _UPSTREAM_PAUSE_SECONDS + <= recorder.payloads[0]["response_cost"] + <= _PER_SECOND_RATE * elapsed + ) + + +@pytest.mark.asyncio +async def test_router_aanthropic_messages_bills_a_per_second_deployment_for_the_whole_stream(respx_mock, monkeypatch): + messages: Final = _unique_messages() + recorder: Final = _RecordingLogger(messages) + monkeypatch.setattr(litellm, "callbacks", [recorder]) + respx_mock.post(url__regex=_ANTHROPIC_URL).mock( + side_effect=lambda request: httpx.Response( + 200, headers={"content-type": "text/event-stream"}, content=_sse_pausing_after_the_first_event() + ) + ) + started: Final = time.monotonic() + stream: Final = await _route_like_the_proxy(_per_second_router(), messages=messages, stream=True) + await _stream_events(stream) + await _wait_for_payload(recorder) + elapsed: Final = time.monotonic() - started + + assert ( + _PER_SECOND_RATE * _UPSTREAM_PAUSE_SECONDS + <= recorder.payloads[0]["response_cost"] + <= _PER_SECOND_RATE * elapsed + ) + + +async def _wait_for_payloads(recorder: _RecordingLogger, count: int) -> None: + async def arrived() -> None: + while len(recorder.payloads) < count: + await asyncio.sleep(0.05) + + await asyncio.wait_for(arrived(), timeout=30.0) + + +@pytest.mark.asyncio +async def test_router_aanthropic_messages_bills_a_short_call_on_a_long_context_rate_like_a_deployment_without_one( + respx_mock, monkeypatch +): + messages: Final = _unique_messages() + recorder: Final = _RecordingLogger(messages) + monkeypatch.setattr(litellm, "callbacks", [recorder]) + respx_mock.post(url__regex=_ANTHROPIC_URL).mock( + side_effect=lambda request: httpx.Response(200, json=_anthropic_body()) + ) + + await _route_like_the_proxy(_priced_router(), messages=messages) + await _route_like_the_proxy( + _priced_router(input_cost_per_token_above_200k_tokens=1.0, output_cost_per_token_above_200k_tokens=1.0), + messages=messages, + ) + await _wait_for_payloads(recorder, 2) + + costs: Final = {payload["response_cost"] for payload in recorder.payloads} + assert len(costs) == 1, costs + assert costs.pop() > 0 + + _LARGE_SYSTEM_PROMPT: Final = "This is a comprehensive legal agreement between Party A and Party B. " * 100 diff --git a/tests/unit/llms/anthropic/messages/native/test_anthropic_messages_transformation.py b/tests/unit/llms/anthropic/messages/native/test_anthropic_messages_transformation.py index 13729ecb6cb..1c004d8c36e 100644 --- a/tests/unit/llms/anthropic/messages/native/test_anthropic_messages_transformation.py +++ b/tests/unit/llms/anthropic/messages/native/test_anthropic_messages_transformation.py @@ -1,3 +1,4 @@ +import logging from collections.abc import Mapping from datetime import datetime, timezone from pathlib import Path @@ -6,9 +7,11 @@ from unittest.mock import patch import httpx import pytest +import respx from pydantic import JsonValue import litellm +from litellm import anthropic_beta_headers_manager from litellm.litellm_core_utils.litellm_logging import Logging from litellm.llms.anthropic.messages.native.transformation import ( AnthropicMessagesConfig, @@ -498,3 +501,133 @@ def test_drop_params_forwards_a_non_object_message_item_as_sent_on_the_native_re assert request["messages"][1] == 7 assert request["messages"][2]["content"][0]["content"] == [{"type": "text", "text": "sunny"}] + + +BINDING_AND_DISPLAY_BETAS: Final = "thinking-binding-controls-2026-08-01,thinking-display-updates-2026-08-18" + + +def test_messages_headers_add_the_binding_beta_when_thinking_carries_block_binding() -> None: + headers: Final = AnthropicMessagesConfig()._update_headers_with_anthropic_beta( + headers={}, + optional_params={"thinking": {"type": "adaptive", "block_binding": {"prefix_mismatch_behavior": "error"}}}, + messages=[], + ) + assert "thinking-binding-controls-2026-08-01" in headers["anthropic-beta"].split(",") + + +def test_messages_headers_add_no_binding_beta_without_block_binding() -> None: + headers: Final = AnthropicMessagesConfig()._update_headers_with_anthropic_beta( + headers={}, optional_params={"thinking": {"type": "adaptive"}}, messages=[] + ) + assert "thinking-binding-controls-2026-08-01" not in headers.get("anthropic-beta", "") + + +def _bound_adaptive_request(headers: dict[str, str], drop_params: bool | None = True) -> dict[str, object]: + thinking: Final = { + "type": "adaptive", + "block_binding": {"prefix_mismatch_behavior": "error"}, + "display": "updates", + } + optional_params: Final = {"max_tokens": 64, "thinking": thinking} + request: Final = AnthropicMessagesConfig().transform_anthropic_messages_request( + model="claude-opus-4-8", + messages=[{"role": "user", "content": "hi"}], + anthropic_messages_optional_request_params=optional_params, + litellm_params=GenericLiteLLMParams(drop_params=drop_params), + headers=headers, + ) + assert thinking == { + "type": "adaptive", + "block_binding": {"prefix_mismatch_behavior": "error"}, + "display": "updates", + }, "the caller's thinking dict must survive for a fallback leg" + return request + + +def test_messages_request_keeps_beta_gated_thinking_fields_when_the_final_header_carries_the_betas() -> None: + request: Final = _bound_adaptive_request({"anthropic-beta": BINDING_AND_DISPLAY_BETAS}) + assert request["thinking"] == { + "type": "adaptive", + "block_binding": {"prefix_mismatch_behavior": "error"}, + "display": "updates", + } + + +def test_messages_request_drops_beta_gated_thinking_fields_the_final_header_does_not_carry( + caplog: pytest.LogCaptureFixture, +) -> None: + with caplog.at_level(logging.WARNING, logger="LiteLLM"): + request: Final = _bound_adaptive_request({"anthropic-beta": "thinking-binding-controls-2026-08-01"}) + assert request["thinking"] == {"type": "adaptive", "block_binding": {"prefix_mismatch_behavior": "error"}} + assert any("thinking.display=updates" in record.getMessage() for record in caplog.records) + + +def test_messages_request_sends_beta_gated_thinking_fields_as_is_without_drop_params( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setattr(litellm, "drop_params", False) + request: Final = _bound_adaptive_request({}, drop_params=None) + assert request["thinking"] == { + "type": "adaptive", + "block_binding": {"prefix_mismatch_behavior": "error"}, + "display": "updates", + } + + +def test_messages_request_drops_clear_at_from_a_mid_conversation_system_turn_without_its_beta() -> None: + system_turn: Final = {"role": "system", "content": "be terse", "clear_at": "end_turn"} + request: Final = AnthropicMessagesConfig().transform_anthropic_messages_request( + model="claude-opus-4-8", + messages=[{"role": "user", "content": "hi"}, system_turn, {"role": "user", "content": "go"}], + anthropic_messages_optional_request_params={"max_tokens": 64}, + litellm_params=GenericLiteLLMParams(drop_params=True), + headers={"anthropic-beta": "mid-conversation-system-2026-07-01"}, + ) + assert request["messages"][1] == {"role": "system", "content": "be terse"} + assert system_turn == {"role": "system", "content": "be terse", "clear_at": "end_turn"} + + +def test_a_provider_without_a_beta_headers_row_forwards_beta_gated_fields_under_drop_params( + local_beta_headers_config: None, +) -> None: + thinking: Final = {"type": "enabled", "budget_tokens": 1024, "display": "updates"} + request: Final = MinimaxMessagesConfig().transform_anthropic_messages_request( + model="MiniMax-M2", + messages=[ + {"role": "user", "content": "hi"}, + {"role": "assistant", "content": "Hello"}, + {"role": "user", "content": "how are you"}, + {"role": "system", "content": "Answer in French", "output_config": {"effort": "low"}}, + ], + anthropic_messages_optional_request_params={"max_tokens": 64, "thinking": dict(thinking)}, + litellm_params=GenericLiteLLMParams(drop_params=True), + headers={}, + ) + assert request["thinking"] == thinking + assert request["messages"][-1]["output_config"] == {"effort": "low"} + + +def test_a_first_party_messages_request_never_fetches_the_remote_beta_headers_config( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.delenv("LITELLM_LOCAL_ANTHROPIC_BETA_HEADERS", raising=False) + monkeypatch.setattr(anthropic_beta_headers_manager, "_BETA_HEADERS_CONFIG", None) + + with respx.mock(assert_all_called=False) as router: + remote_config: Final = router.get(litellm.anthropic_beta_headers_url).mock( + return_value=httpx.Response(200, json={}) + ) + request: Final = AnthropicMessagesConfig().transform_anthropic_messages_request( + model="claude-opus-4-8", + messages=[ + {"role": "user", "content": "hi"}, + {"role": "system", "content": "be terse", "clear_at": "end_turn"}, + {"role": "user", "content": "go"}, + ], + anthropic_messages_optional_request_params={"max_tokens": 64}, + litellm_params=GenericLiteLLMParams(drop_params=True), + headers={}, + ) + + assert request["messages"][1] == {"role": "system", "content": "be terse"} + assert not remote_config.called diff --git a/tests/unit/llms/anthropic/messages/native/test_response_cache.py b/tests/unit/llms/anthropic/messages/native/test_response_cache.py index 133bcd96433..bc3b1f6ac76 100644 --- a/tests/unit/llms/anthropic/messages/native/test_response_cache.py +++ b/tests/unit/llms/anthropic/messages/native/test_response_cache.py @@ -2,6 +2,7 @@ import asyncio from typing import Any, AsyncIterator, Dict, Final, List import pytest +import pytest_asyncio import datetime @@ -67,7 +68,7 @@ async def _collect(stream: AsyncIterator[bytes]) -> List[bytes]: return [chunk async for chunk in stream] -@pytest.fixture(autouse=True) +@pytest_asyncio.fixture(autouse=True, loop_scope="function") async def _drain_logging_worker(): yield await GLOBAL_LOGGING_WORKER.flush() diff --git a/tests/unit/llms/anthropic/test_anthropic_common_utils.py b/tests/unit/llms/anthropic/test_anthropic_common_utils.py index d408433e59c..b37b2534b73 100644 --- a/tests/unit/llms/anthropic/test_anthropic_common_utils.py +++ b/tests/unit/llms/anthropic/test_anthropic_common_utils.py @@ -4295,7 +4295,14 @@ def test_shared_legacy_thinking_translation_carries_block_binding( @pytest.mark.usefixtures("local_model_cost_map") @pytest.mark.parametrize( - ("custom_llm_provider", "model", "requested_thinking", "rejected_keys", "expected_thinking"), + ( + "custom_llm_provider", + "model", + "requested_thinking", + "rejected_keys", + "expected_thinking", + "provider_rejects_block_binding", + ), ( ( "anthropic", @@ -4303,6 +4310,7 @@ def test_shared_legacy_thinking_translation_carries_block_binding( {"type": "enabled", "budget_tokens": 2048}, {"not_a_thinking_key": True}, {"type": "adaptive"}, + False, ), ( "bedrock", @@ -4310,6 +4318,7 @@ def test_shared_legacy_thinking_translation_carries_block_binding( {"type": "enabled", "budget_tokens": 2048}, {"not_a_thinking_key": True}, {"type": "adaptive"}, + False, ), ( "bedrock", @@ -4317,6 +4326,7 @@ def test_shared_legacy_thinking_translation_carries_block_binding( {"type": "enabled", "budget_tokens": 2048}, {"not_a_thinking_key": True}, {"type": "adaptive"}, + False, ), ( "bedrock", @@ -4324,6 +4334,7 @@ def test_shared_legacy_thinking_translation_carries_block_binding( {"type": "adaptive", "display": "updates"}, {"not_a_thinking_key": True, "budget_tokens": 2048}, {"type": "adaptive", "display": "updates"}, + False, ), ( "vertex_ai", @@ -4331,6 +4342,7 @@ def test_shared_legacy_thinking_translation_carries_block_binding( {"type": "enabled", "budget_tokens": 2048}, {"not_a_thinking_key": True}, {"type": "adaptive"}, + False, ), ( "databricks", @@ -4338,6 +4350,7 @@ def test_shared_legacy_thinking_translation_carries_block_binding( {"type": "enabled", "budget_tokens": 2048}, {"not_a_thinking_key": True}, {"type": "adaptive"}, + True, ), ), ids=( @@ -4356,6 +4369,7 @@ def test_chat_thinking_drops_unknown_keys_only_under_drop_params( requested_thinking: dict[str, object], rejected_keys: dict[str, object], expected_thinking: dict[str, object], + provider_rejects_block_binding: bool, drop_params_source: str | None, monkeypatch: pytest.MonkeyPatch, ) -> None: @@ -4370,7 +4384,10 @@ def test_chat_thinking_drops_unknown_keys_only_under_drop_params( ) kept_rejected_keys: Final = {} if drop_params_source else rejected_keys - assert optional_params["thinking"] == {**expected_thinking, "block_binding": block_binding, **kept_rejected_keys} + kept_block_binding: Final = ( + {} if drop_params_source and provider_rejects_block_binding else {"block_binding": block_binding} + ) + assert optional_params["thinking"] == {**expected_thinking, **kept_block_binding, **kept_rejected_keys} @pytest.mark.usefixtures("local_model_cost_map", "local_beta_headers_config") diff --git a/tests/unit/llms/anthropic/test_anthropic_prompt_cache_prediction.py b/tests/unit/llms/anthropic/test_anthropic_prompt_cache_prediction.py index 7a6b6534131..4d0ebef4f2d 100644 --- a/tests/unit/llms/anthropic/test_anthropic_prompt_cache_prediction.py +++ b/tests/unit/llms/anthropic/test_anthropic_prompt_cache_prediction.py @@ -13,7 +13,7 @@ import litellm from litellm.caching.dual_cache import DualCache from litellm.caching.llm_caching_handler import LLMClientCache from litellm.llms.anthropic.count_tokens import handler as count_handler -from litellm.llms.anthropic.messages.native.transformation import DEFAULT_ANTHROPIC_API_VERSION +from litellm.llms.anthropic.messages.native.transformation import DEFAULT_ANTHROPIC_API_VERSION, AnthropicMessagesConfig from litellm.llms.anthropic.prompt_cache_prediction import ( CountedPromptCachePlan, NativePredictionTarget, @@ -26,6 +26,7 @@ from litellm.llms.anthropic.prompt_cache_prediction import ( parse_cache_plan, parse_observed_cache, parse_prompt, + prepare_native_baseline_body, resolve_baseline_prediction_target, resolve_prediction_target, supported_prediction_headers, @@ -36,7 +37,7 @@ from litellm.proxy import proxy_server from litellm.proxy.hooks.prompt_cache_prediction import PromptCacheObserver from litellm.proxy.management_endpoints.prompt_cache_prediction import predict_arm from litellm.proxy.utils import InternalUsageCache -from litellm.types.router import Deployment, LiteLLM_Params, ModelInfo +from litellm.types.router import Deployment, GenericLiteLLMParams, LiteLLM_Params, ModelInfo from litellm.types.utils import CacheCreationTokenDetails, ModelResponse, PromptTokensDetailsWrapper, Usage _MODEL: Final = "claude-sonnet-5" @@ -50,9 +51,12 @@ def _body() -> dict[str, JsonValue]: "model": _MODEL, "system": "Keep this context", "tools": [{"name": "lookup", "input_schema": {"type": "object"}}], - "messages": [{"role": "user", "content": [ - {"type": "text", "text": "A cacheable prefix", "cache_control": {"type": "ephemeral"}} - ]}], + "messages": [ + { + "role": "user", + "content": [{"type": "text", "text": "A cacheable prefix", "cache_control": {"type": "ephemeral"}}], + } + ], } @@ -88,7 +92,8 @@ async def test_observer_records_only_version_supported_by_token_counter(version: "httpx_response": httpx.Response(200, request=wire), "first_api_call_start_time": datetime.fromtimestamp(1000.0), "standard_logging_object": { - "status": "success", "model_id": _DEPLOYMENT, + "status": "success", + "model_id": _DEPLOYMENT, "metadata": {"user_api_key_hash": _CALLER}, }, }, @@ -104,13 +109,16 @@ async def test_observer_records_only_version_supported_by_token_counter(version: assert await lookup(cache, other_scope, prefix, now=1010.0) is None -@pytest.mark.parametrize("headers, supported", [ - ({}, True), - ({"Anthropic-Version": DEFAULT_ANTHROPIC_API_VERSION}, True), - ({"anthropic-version": "2099-01-01"}, False), - ({"Anthropic-Beta": ""}, False), - ({"anthropic-beta": "future-feature"}, False), -]) +@pytest.mark.parametrize( + "headers, supported", + [ + ({}, True), + ({"Anthropic-Version": DEFAULT_ANTHROPIC_API_VERSION}, True), + ({"anthropic-version": "2099-01-01"}, False), + ({"Anthropic-Beta": ""}, False), + ({"anthropic-beta": "future-feature"}, False), + ], +) def test_prediction_header_eligibility(headers: Mapping[str, str], supported: bool) -> None: assert supported_prediction_headers(headers) is supported @@ -150,49 +158,89 @@ async def test_provider_count_uses_same_version_and_preserves_native_input( @pytest.mark.parametrize("source", ["static", "database"]) @pytest.mark.asyncio async def test_environment_credential_matches_native_count_and_observed_scope( - source: str, monkeypatch: pytest.MonkeyPatch, + source: str, + monkeypatch: pytest.MonkeyPatch, ) -> None: monkeypatch.setenv("LIT7658_PROVIDER_KEY", _KEY) monkeypatch.setattr(litellm, "disable_aiohttp_transport", True) monkeypatch.setattr(litellm, "in_memory_llm_clients_cache", LLMClientCache()) params: Final = { - "model": f"anthropic/{_MODEL}", "api_key": "os.environ/LIT7658_PROVIDER_KEY", + "model": f"anthropic/{_MODEL}", + "api_key": "os.environ/LIT7658_PROVIDER_KEY", "api_base": "https://api.anthropic.com", } - router: Final = litellm.Router(model_list=[{ - "model_name": "test-native", "litellm_params": dict(params), "model_info": {"id": _DEPLOYMENT}, - }] if source == "static" else [], num_retries=0) + router: Final = litellm.Router( + model_list=[ + { + "model_name": "test-native", + "litellm_params": dict(params), + "model_info": {"id": _DEPLOYMENT}, + } + ] + if source == "static" + else [], + num_retries=0, + ) if source == "database": monkeypatch.setattr(proxy_server, "llm_router", router) - assert proxy_server.ProxyConfig()._add_deployment([SimpleNamespace( - model_id=_DEPLOYMENT, model_name="test-native", model_info={}, litellm_params=dict(params), - )]) == 1 + assert ( + proxy_server.ProxyConfig()._add_deployment( + [ + SimpleNamespace( + model_id=_DEPLOYMENT, + model_name="test-native", + model_info={}, + litellm_params=dict(params), + ) + ] + ) + == 1 + ) deployment: Final = router.get_deployment(_DEPLOYMENT) assert deployment is not None target: Final = resolve_prediction_target(deployment.litellm_params) assert isinstance(target, NativePredictionTarget) body: Final = _body() with respx.mock() as upstream: - native: Final = upstream.post("https://api.anthropic.com/v1/messages").respond(200, json={ - "id": "msg_test", "type": "message", "role": "assistant", "model": _MODEL, - "content": [{"type": "text", "text": "Hello"}], "stop_reason": "end_turn", "stop_sequence": None, - "usage": {"input_tokens": 11, "output_tokens": 1, "cache_read_input_tokens": 300}, - }) + native: Final = upstream.post("https://api.anthropic.com/v1/messages").respond( + 200, + json={ + "id": "msg_test", + "type": "message", + "role": "assistant", + "model": _MODEL, + "content": [{"type": "text", "text": "Hello"}], + "stop_reason": "end_turn", + "stop_sequence": None, + "usage": {"input_tokens": 11, "output_tokens": 1, "cache_read_input_tokens": 300}, + }, + ) counter: Final = upstream.post("https://api.anthropic.com/v1/messages/count_tokens").respond( - 200, json={"input_tokens": 311}, + 200, + json={"input_tokens": 311}, ) await router.aanthropic_messages( - model="test-native", max_tokens=1, **{key: value for key, value in body.items() if key != "model"}, + model="test-native", + max_tokens=1, + **{key: value for key, value in body.items() if key != "model"}, ) assert await count_prompt_tokens(target.model, target.api_key, body) == 311 assert native.call_count == counter.call_count == 1 assert native.calls.last.request.headers["x-api-key"] == counter.calls.last.request.headers["x-api-key"] == _KEY - observed: Final = parse_observed_cache(native.calls.last.request, ModelResponse( - model=_MODEL, usage=Usage( - prompt_tokens=311, completion_tokens=1, total_tokens=312, - prompt_tokens_details=PromptTokensDetailsWrapper(cached_tokens=300), + observed: Final = parse_observed_cache( + native.calls.last.request, + ModelResponse( + model=_MODEL, + usage=Usage( + prompt_tokens=311, + completion_tokens=1, + total_tokens=312, + prompt_tokens_details=PromptTokensDetailsWrapper(cached_tokens=300), + ), ), - ), _CALLER, _DEPLOYMENT) + _CALLER, + _DEPLOYMENT, + ) assert observed is not None assert observed.scope == cache_scope(_CALLER, _DEPLOYMENT, target.api_key, target.model) @@ -200,15 +248,26 @@ async def test_environment_credential_matches_native_count_and_observed_scope( @pytest.mark.parametrize("inline_key", [None, _KEY]) @pytest.mark.asyncio async def test_named_credential_is_explicitly_unsupported_before_count( - inline_key: str | None, monkeypatch: pytest.MonkeyPatch, + inline_key: str | None, + monkeypatch: pytest.MonkeyPatch, ) -> None: - monkeypatch.setattr(litellm, "credential_list", [CredentialItem( - credential_name="test-named", credential_info={}, credential_values={"api_key": "test-named-provider-key"}, - )]) + monkeypatch.setattr( + litellm, + "credential_list", + [ + CredentialItem( + credential_name="test-named", + credential_info={}, + credential_values={"api_key": "test-named-provider-key"}, + ) + ], + ) deployment: Final = Deployment( model_name="test-native", litellm_params=LiteLLM_Params( - model=f"anthropic/{_MODEL}", api_key=inline_key, litellm_credential_name="test-named", + model=f"anthropic/{_MODEL}", + api_key=inline_key, + litellm_credential_name="test-named", ), model_info=ModelInfo(id=_DEPLOYMENT), ) @@ -232,8 +291,7 @@ def _cache_plan(body: Mapping[str, JsonValue]) -> PromptCachePlan: def _text(text: str, ttl: str | None = None) -> dict[str, JsonValue]: - return {"type": "text", "text": text, - **({"cache_control": {"type": "ephemeral", "ttl": ttl}} if ttl else {})} + return {"type": "text", "text": text, **({"cache_control": {"type": "ephemeral", "ttl": ttl}} if ttl else {})} def _prompt(*blocks: dict[str, JsonValue], role: str = "user", **options: JsonValue) -> dict[str, JsonValue]: @@ -251,16 +309,19 @@ def test_public_predictor_preserves_string_message_policy(text: str, supported: def test_cache_plan_preserves_hierarchical_prefixes_and_public_policy() -> None: body: Final = _prompt( - _text("First turn", "5m"), system=[_text("Stable instructions", "1h")], - tools=[{"name": "lookup", "input_schema": {"type": "object"}, - "cache_control": {"type": "ephemeral", "ttl": "1h"}}], + _text("First turn", "5m"), + system=[_text("Stable instructions", "1h")], + tools=[ + {"name": "lookup", "input_schema": {"type": "object"}, "cache_control": {"type": "ephemeral", "ttl": "1h"}} + ], ) plan: Final = _cache_plan(body) changed: Final = _cache_plan({**body, "system": [_text("Changed instructions", "1h")]}) assert tuple(marker.ttl_seconds for marker in plan.breakpoints) == (3600, 3600, 300) assert plan.breakpoints[0].fingerprint == changed.breakpoints[0].fingerprint - assert all(left.fingerprint != right.fingerprint for left, right - in zip(plan.breakpoints[1:], changed.breakpoints[1:])) + assert all( + left.fingerprint != right.fingerprint for left, right in zip(plan.breakpoints[1:], changed.breakpoints[1:]) + ) assert plan.breakpoints[0].prefix_body == { "tools": [{"name": "lookup", "input_schema": {"type": "object"}}], "messages": [], @@ -268,51 +329,80 @@ def test_cache_plan_preserves_hierarchical_prefixes_and_public_policy() -> None: assert parse_prompt(body) is None -@pytest.mark.parametrize("kind, added, matches", [ - ("text", 19, True), ("text", 20, False), ("tool_use", 30, True), - ("tool_result", 30, True), -]) +@pytest.mark.parametrize( + "kind, added, matches", + [ + ("text", 19, True), + ("text", 20, False), + ("tool_use", 30, True), + ("tool_result", 30, True), + ], +) def test_cache_plan_lookback_counts_native_positions( - kind: str, added: int, matches: bool, + kind: str, + added: int, + matches: bool, ) -> None: previous: Final = _cache_plan(_body()) appended: Final[list[dict[str, JsonValue]]] = [ {"type": "tool_use", "id": f"tool_{index}", "name": "lookup", "input": {}} - if kind == "tool_use" else - {"type": "tool_result", "tool_use_id": f"tool_{index}", "content": "done"} - if kind == "tool_result" else - {"type": "text", "text": f"Added {index}"} + if kind == "tool_use" + else {"type": "tool_result", "tool_use_id": f"tool_{index}", "content": "done"} + if kind == "tool_result" + else {"type": "text", "text": f"Added {index}"} for index in range(added) ] - current: Final = _cache_plan({**_body(), **_prompt( - _text("A cacheable prefix"), *appended[:-1], - {**appended[-1], "cache_control": {"type": "ephemeral"}}, - )}) - assert (previous.breakpoints[0].fingerprint - in current.breakpoints[0].lookback_fingerprints) is matches + current: Final = _cache_plan( + { + **_body(), + **_prompt( + _text("A cacheable prefix"), + *appended[:-1], + {**appended[-1], "cache_control": {"type": "ephemeral"}}, + ), + } + ) + assert (previous.breakpoints[0].fingerprint in current.breakpoints[0].lookback_fingerprints) is matches -@pytest.mark.parametrize("change, same_prefix, same_content", [ - ("tool_order", False, False), ("effort", False, False), - ("standard_speed", True, True), ("ttl", False, True), -]) +@pytest.mark.parametrize( + "change, same_prefix, same_content", + [ + ("tool_order", False, False), + ("effort", False, False), + ("standard_speed", True, True), + ("ttl", False, True), + ], +) def test_cache_plan_identity_respects_settings_and_preserves_content( - change: str, same_prefix: bool, same_content: bool, + change: str, + same_prefix: bool, + same_content: bool, ) -> None: tool_input: Final[dict[str, JsonValue]] = {"a": 1, "b": 2, "cache_control": {"ttl": "user-data"}} block: Final[dict[str, JsonValue]] = { - "type": "tool_use", "id": "tool_1", "name": "lookup", "input": tool_input, + "type": "tool_use", + "id": "tool_1", + "name": "lookup", + "input": tool_input, "cache_control": {"type": "ephemeral", "ttl": "5m"}, } changed_block: Final = ( - {**block, "input": dict(reversed(tool_input.items()))} if change == "tool_order" else - {**block, "cache_control": {"type": "ephemeral", "ttl": "1h"}} if change == "ttl" else block + {**block, "input": dict(reversed(tool_input.items()))} + if change == "tool_order" + else {**block, "cache_control": {"type": "ephemeral", "ttl": "1h"}} + if change == "ttl" + else block ) before: Final = _cache_plan(_prompt(block, role="assistant", output_config={"effort": "low"})).breakpoints[0] - after: Final = _cache_plan(_prompt( - changed_block, role="assistant", output_config={"effort": "high" if change == "effort" else "low"}, - **({"speed": "standard"} if change == "standard_speed" else {}), - )).breakpoints[0] + after: Final = _cache_plan( + _prompt( + changed_block, + role="assistant", + output_config={"effort": "high" if change == "effort" else "low"}, + **({"speed": "standard"} if change == "standard_speed" else {}), + ) + ).breakpoints[0] assert (before.fingerprint == after.fingerprint) is same_prefix assert (before.fingerprint in after.lookback_fingerprints) is same_prefix assert (before.content_fingerprint == after.content_fingerprint) is same_content @@ -323,30 +413,41 @@ def test_cache_plan_identity_respects_settings_and_preserves_content( def test_cache_plan_automatic_cache_and_thinking_use_last_cacheable_block() -> None: body: Final = _prompt( - _text("A stable answer"), {"type": "thinking", "thinking": "Thinking", "signature": "signature"}, - role="assistant", thinking={"type": "adaptive"}, cache_control={"type": "ephemeral", "ttl": "1h"}, + _text("A stable answer"), + {"type": "thinking", "thinking": "Thinking", "signature": "signature"}, + role="assistant", + thinking={"type": "adaptive"}, + cache_control={"type": "ephemeral", "ttl": "1h"}, ) plan: Final = _cache_plan(body) assert len(plan.breakpoints) == 1 assert plan.breakpoints[0].ttl_seconds == 3600 assert plan.breakpoints[0].prefix_body == { "thinking": {"type": "adaptive"}, - "messages": [{"role": "assistant", "content": [ - {"type": "text", "text": "A stable answer"}, - ]}], + "messages": [ + { + "role": "assistant", + "content": [ + {"type": "text", "text": "A stable answer"}, + ], + } + ], } assert parse_prompt(body) is None -@pytest.mark.parametrize("body, reason", [ - (_prompt({"type": "image"}), "unsupported_prompt_shape"), - ({**_body(), "unknown_native_setting": True}, "unsupported_prompt_shape"), - ({**_body(), "cache_control": {"type": "ephemeral", "ttl": "1h"}}, - "conflicting_cache_ttl"), - (_prompt(_text("five", "5m"), _text("hour", "1h")), "invalid_cache_ttl_order"), -]) +@pytest.mark.parametrize( + "body, reason", + [ + (_prompt({"type": "image"}), "unsupported_prompt_shape"), + ({**_body(), "unknown_native_setting": True}, "unsupported_prompt_shape"), + ({**_body(), "cache_control": {"type": "ephemeral", "ttl": "1h"}}, "conflicting_cache_ttl"), + (_prompt(_text("five", "5m"), _text("hour", "1h")), "invalid_cache_ttl_order"), + ], +) def test_cache_plan_unsupported_is_explicit( - body: Mapping[str, JsonValue], reason: str, + body: Mapping[str, JsonValue], + reason: str, ) -> None: result: Final = parse_cache_plan(body) assert isinstance(result, UnsupportedCachePlan) @@ -354,30 +455,46 @@ def test_cache_plan_unsupported_is_explicit( @pytest.mark.asyncio -@pytest.mark.parametrize("model, counts, reason", [ - (None, (100, 150, 200), None), - (None, (100, 201, 200), "inconsistent_prefix_token_count"), - (None, (151, 150, 200), "inconsistent_prefix_token_count"), - (None, (None, 150, 200), "token_count_unavailable"), - ("claude-opus-5", (100, 150, 200), None), - ("claude-sonnet-5", (100, 150, 200), None), - ("declared-cache-model", (100, 150, 200), None), - ("unknown-cache-model", (100, 150, 200), "unsupported_thinking_cache_semantics"), - ("claude-haiku-4-5", (100, 150, 200), "unsupported_thinking_cache_semantics"), - ("claude-sonnet-4-5", (100, 150, 200), "unsupported_thinking_cache_semantics"), -]) +@pytest.mark.parametrize( + "model, counts, reason", + [ + (None, (100, 150, 200), None), + (None, (100, 201, 200), "inconsistent_prefix_token_count"), + (None, (151, 150, 200), "inconsistent_prefix_token_count"), + (None, (None, 150, 200), "token_count_unavailable"), + ("claude-opus-5", (100, 150, 200), None), + ("claude-sonnet-5", (100, 150, 200), None), + ("declared-cache-model", (100, 150, 200), None), + ("unknown-cache-model", (100, 150, 200), "unsupported_thinking_cache_semantics"), + ("claude-haiku-4-5", (100, 150, 200), "unsupported_thinking_cache_semantics"), + ("claude-sonnet-4-5", (100, 150, 200), "unsupported_thinking_cache_semantics"), + ], +) async def test_cache_plan_count_conserves_total_and_rejects_unknown( - model: str | None, counts: tuple[int | None, int | None, int], reason: str | None, + model: str | None, + counts: tuple[int | None, int | None, int], + reason: str | None, monkeypatch: pytest.MonkeyPatch, ) -> None: - monkeypatch.setitem(litellm.model_cost, "declared-cache-model", { - "litellm_provider": "anthropic", "mode": "chat", "supports_thinking_cache_preservation": True, - }) - plan: Final = _cache_plan(_prompt( - {"type": "thinking", "thinking": "Retained thought", "signature": "signature"} - if model else _text("first", "5m"), - _text("second", "5m"), _text("uncached"), role="assistant" if model else "user", - )) + monkeypatch.setitem( + litellm.model_cost, + "declared-cache-model", + { + "litellm_provider": "anthropic", + "mode": "chat", + "supports_thinking_cache_preservation": True, + }, + ) + plan: Final = _cache_plan( + _prompt( + {"type": "thinking", "thinking": "Retained thought", "signature": "signature"} + if model + else _text("first", "5m"), + _text("second", "5m"), + _text("uncached"), + role="assistant" if model else "user", + ) + ) async def count(model: str, api_key: str, body: Mapping[str, JsonValue]) -> int | None: assert reason != "unsupported_thinking_cache_semantics", "Unverified thinking retention must skip counting" @@ -399,12 +516,15 @@ async def test_cache_plan_count_conserves_total_and_rejects_unknown( @pytest.mark.parametrize("section", ["system", "tools"]) @pytest.mark.parametrize("rejects_prefix", (False, True)) async def test_native_count_preserves_settings_and_requires_every_prefix( - section: str, rejects_prefix: bool, monkeypatch: pytest.MonkeyPatch, + section: str, + rejects_prefix: bool, + monkeypatch: pytest.MonkeyPatch, ) -> None: monkeypatch.setattr(litellm, "disable_aiohttp_transport", True) monkeypatch.setattr(litellm, "in_memory_llm_clients_cache", LLMClientCache()) params: Final = LiteLLM_Params( - model=f"anthropic/{_MODEL}", api_key=_KEY, + model=f"anthropic/{_MODEL}", + api_key=_KEY, api_base="https://gateway.example/v1/messages", ) target: Final = resolve_baseline_prediction_target(params) @@ -413,19 +533,38 @@ async def test_native_count_preserves_settings_and_requires_every_prefix( assert not isinstance(resolve_prediction_target(params), NativePredictionTarget) body: Final = _body() marker: Final[dict[str, JsonValue]] = {"type": "ephemeral", "ttl": "1h"} - body[section] = ([_text("A cached system", "1h")] if section == "system" else [{ - "name": "lookup", "input_schema": {"type": "object"}, "cache_control": marker, - }]) - plan: Final = _cache_plan({**body, **_prompt( - _text("A later prefix", "5m"), _text("An uncached suffix"), - thinking={"type": "adaptive"}, tool_choice={"type": "auto"}, output_config={"effort": "high"}, - )}) + body[section] = ( + [_text("A cached system", "1h")] + if section == "system" + else [ + { + "name": "lookup", + "input_schema": {"type": "object"}, + "cache_control": marker, + } + ] + ) + plan: Final = _cache_plan( + { + **body, + **_prompt( + _text("A later prefix", "5m"), + _text("An uncached suffix"), + thinking={"type": "adaptive"}, + tool_choice={"type": "auto"}, + output_config={"effort": "high"}, + ), + } + ) assert len(plan.breakpoints) == 2 assert plan.breakpoints[0].prefix_body["messages"] == [] async def count(model: str, api_key: str, body: Mapping[str, JsonValue]) -> int | None: return await count_prompt_tokens( - model, api_key, {**body, "max_tokens": 100}, api_base=target.api_base, + model, + api_key, + {**body, "max_tokens": 100}, + api_base=target.api_base, ) with respx.mock(assert_all_called=False) as upstream: @@ -434,12 +573,16 @@ async def test_native_count_preserves_settings_and_requires_every_prefix( upstream.post(endpoint, json={**body, "model": _MODEL}).respond( 400 if rejects_prefix and index == 1 else 200, json={"detail": {"error": "messages parameter is required"}} - if rejects_prefix and index == 1 else {"input_tokens": tokens}, + if rejects_prefix and index == 1 + else {"input_tokens": tokens}, + ) + for index, (body, tokens) in enumerate( + ( + (plan.full_body, 6000), + (plan.breakpoints[0].prefix_body, 5000), + (plan.breakpoints[1].prefix_body, 5800), + ) ) - for index, (body, tokens) in enumerate(( - (plan.full_body, 6000), (plan.breakpoints[0].prefix_body, 5000), - (plan.breakpoints[1].prefix_body, 5800), - )) ) unexpected: Final = upstream.post(endpoint).respond(200, json={"input_tokens": 1}) result: Final = await count_cache_plan(target.model, target.api_key, plan, count) @@ -452,3 +595,32 @@ async def test_native_count_preserves_settings_and_requires_every_prefix( assert tuple(marker.prefix_tokens for marker in result.breakpoints) == (5000, 5800) assert tuple(route.call_count for route in routes) == (1, 1, 1) assert unexpected.call_count == 0 + + +def test_native_baseline_body_matches_the_sent_body_when_drop_params_meets_an_auto_added_beta( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setattr(litellm, "drop_params", True) + thinking: Final = {"type": "adaptive", "block_binding": {"prefix_mismatch_behavior": "error"}} + messages: Final = [{"role": "user", "content": "Say hi in French"}] + predicted: Final = prepare_native_baseline_body( + {"model": "claude-opus-4-8", "messages": messages, "max_tokens": 64, "thinking": thinking}, "claude-opus-4-8" + ) + config: Final = AnthropicMessagesConfig() + headers, _ = config.validate_anthropic_messages_environment( + headers={}, + model="claude-opus-4-8", + messages=[dict(message) for message in messages], + optional_params={"max_tokens": 64, "thinking": dict(thinking)}, + litellm_params={}, + api_key="sk-ant-test", + ) + sent: Final = config.transform_anthropic_messages_request( + model="claude-opus-4-8", + messages=[dict(message) for message in messages], + anthropic_messages_optional_request_params={"max_tokens": 64, "thinking": dict(thinking)}, + litellm_params=GenericLiteLLMParams(), + headers=headers, + ) + assert predicted is not None + assert predicted["thinking"] == sent["thinking"] == thinking diff --git a/tests/unit/llms/azure/chat/test_azure_gpt_reasoning_transformation.py b/tests/unit/llms/azure/chat/test_azure_gpt_reasoning_transformation.py index a82dd98e31d..2d9381ec16a 100644 --- a/tests/unit/llms/azure/chat/test_azure_gpt_reasoning_transformation.py +++ b/tests/unit/llms/azure/chat/test_azure_gpt_reasoning_transformation.py @@ -1,3 +1,4 @@ +import logging from collections.abc import Mapping from typing import Final @@ -279,6 +280,29 @@ def test_azure_gpt5_reasoning_effort_none_dropped(config: AzureOpenAIGPT5Config) assert "reasoning_effort" not in params or params.get("reasoning_effort") != "none" +@pytest.mark.parametrize(("model", "names_base_model"), [("azure/litellm-qa-sol-alias", True), ("azure/gpt-5", False)]) +def test_azure_dropped_effort_warning_suggests_base_model_only_for_a_name_the_map_lacks( + config: AzureOpenAIGPT5Config, caplog: pytest.LogCaptureFixture, model: str, names_base_model: bool +) -> None: + with caplog.at_level(logging.WARNING, logger="LiteLLM"): + params: Final = config.map_openai_params( + non_default_params={"reasoning_effort": "none"}, + optional_params={}, + model=model, + drop_params=True, + api_version="2025-04-01-preview", + declared_reasoning_efforts=(), + ) + + assert "reasoning_effort" not in params + drop_warnings: Final = [ + record.getMessage() for record in caplog.records if "Dropping reasoning_effort='none'" in record.getMessage() + ] + assert len(drop_warnings) == 1 + assert "model_info.reasoning_effort_levels" in drop_warnings[0] + assert ("model_info.base_model" in drop_warnings[0]) is names_base_model + + # Logprobs support tests for Azure GPT-5.2 def test_azure_gpt5_2_supports_logprobs(config: AzureOpenAIGPT5Config): """Test that Azure GPT-5.2 models support logprobs parameters. diff --git a/tests/unit/llms/azure_ai/chat/test_azure_ai_transformation.py b/tests/unit/llms/azure_ai/chat/test_azure_ai_transformation.py index 42dc222e420..6269fd367fe 100644 --- a/tests/unit/llms/azure_ai/chat/test_azure_ai_transformation.py +++ b/tests/unit/llms/azure_ai/chat/test_azure_ai_transformation.py @@ -1101,6 +1101,58 @@ def test_map_azure_model_group(model_group_header, expected_model): assert config._map_azure_model_group(model_group_header) == expected_model +def test_azure_ai_sends_file_parts_instead_of_flattening_them_to_text() -> None: + file_part: Final = { + "type": "file", + "file": {"filename": "document.pdf", "file_data": "data:application/pdf;base64,JVBERi0xLjQK"}, + } + content: Final = [file_part, {"type": "text", "text": "What is the codeword?"}] + + request: Final = AzureAIStudioConfig().transform_request( + model="Kimi-K2.6", + messages=[{"role": "user", "content": content}], + optional_params={}, + litellm_params={}, + headers={}, + ) + + assert request["messages"] == [{"role": "user", "content": content}] + + +def test_azure_ai_passes_dict_message_content_through_for_the_provider_to_judge() -> None: + content: Final = {"type": "text", "text": "Say hi"} + + request: Final = AzureAIStudioConfig().transform_request( + model="gpt-5.6-sol", + messages=[{"role": "user", "content": content}], + optional_params={}, + litellm_params={}, + headers={}, + ) + + assert request["messages"] == [{"role": "user", "content": content}] + + +@pytest.mark.parametrize( + "file", + [ + {"filename": "notes.txt", "file_data": "data:text/plain;base64,aGVsbG8="}, + {"file_id": "file-abc"}, + ], + ids=["text_data_url", "file_id_only"], +) +def test_azure_ai_flattens_a_non_pdf_file_part_to_its_text_as_before(file: dict[str, str]) -> None: + request: Final = AzureAIStudioConfig().transform_request( + model="Llama-3.3-70B-Instruct", + messages=[{"role": "user", "content": [{"type": "text", "text": "summarize"}, {"type": "file", "file": file}]}], + optional_params={}, + litellm_params={}, + headers={}, + ) + + assert request["messages"] == [{"role": "user", "content": "summarize"}] + + def test_translate_developer_role_hoists_a_later_developer_message_for_a_model_that_rejects_it( declare_mid_conversation_system: Callable[[str, bool], None], ) -> None: diff --git a/tests/unit/llms/azure_ai/claude/test_azure_anthropic_handler.py b/tests/unit/llms/azure_ai/claude/test_azure_anthropic_handler.py index 052c4c422f2..d886303da8f 100644 --- a/tests/unit/llms/azure_ai/claude/test_azure_anthropic_handler.py +++ b/tests/unit/llms/azure_ai/claude/test_azure_anthropic_handler.py @@ -6,6 +6,7 @@ sys.path.insert( ) import json +from typing import Final from unittest.mock import MagicMock, patch import pytest @@ -272,3 +273,71 @@ def test_completion_surfaces_the_status_and_headers_of_a_rejected_request(): assert rejected.value.status_code == 400 assert rejected.value.litellm_response_headers["x-request-id"] == "req-1" assert "max_tokens too large" in str(rejected.value) + + +@respx.mock +@pytest.mark.parametrize("drop_params", [True, False]) +def test_completion_adds_the_binding_beta_so_block_binding_is_kept( + drop_params: bool, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setattr(litellm, "drop_params", False) + route: Final = respx.post("https://example-resource.services.ai.azure.com/anthropic/v1/messages").mock( + return_value=Response( + 200, + json={ + "id": "msg_bound", + "type": "message", + "role": "assistant", + "model": "claude-sonnet-4-6", + "content": [{"type": "text", "text": "Hello"}], + "stop_reason": "end_turn", + "stop_sequence": None, + "usage": {"input_tokens": 1, "output_tokens": 1}, + }, + ) + ) + block_binding: Final = {"prefix_mismatch_behavior": "error"} + + litellm.completion( + model="azure_ai/claude-sonnet-4-6", + messages=[{"role": "user", "content": "hi"}], + api_base="https://example-resource.services.ai.azure.com/anthropic", + api_key="azure-test-key", + thinking={"type": "enabled", "budget_tokens": 1024, "block_binding": block_binding}, + drop_params=drop_params, + ) + + sent: Final = json.loads(route.calls.last.request.content) + assert sent["thinking"] == {"type": "enabled", "budget_tokens": 1024, "block_binding": block_binding} + assert "thinking-binding-controls-2026-08-01" in route.calls.last.request.headers["anthropic-beta"].split(",") + + +@respx.mock +def test_completion_sends_the_binding_beta_next_to_the_callers_own_betas(local_beta_headers_config: None) -> None: + route: Final = respx.post("https://example-resource.services.ai.azure.com/anthropic/v1/messages").mock( + return_value=Response( + 200, + json={ + "id": "msg_bound", + "type": "message", + "role": "assistant", + "model": "claude-sonnet-4-6", + "content": [{"type": "text", "text": "Hello"}], + "stop_reason": "end_turn", + "stop_sequence": None, + "usage": {"input_tokens": 1, "output_tokens": 1}, + }, + ) + ) + + litellm.completion( + model="azure_ai/claude-sonnet-4-6", + messages=[{"role": "user", "content": "hi"}], + api_base="https://example-resource.services.ai.azure.com/anthropic", + api_key="azure-test-key", + thinking={"type": "enabled", "budget_tokens": 1024, "block_binding": {"prefix_mismatch_behavior": "error"}}, + extra_headers={"anthropic-beta": "context-1m-2025-08-07"}, + ) + + sent_betas: Final = set(route.calls.last.request.headers["anthropic-beta"].split(",")) + assert {"context-1m-2025-08-07", "thinking-binding-controls-2026-08-01"} <= sent_betas diff --git a/tests/unit/llms/azure_ai/passthrough/test_azure_ai_passthrough_transformation.py b/tests/unit/llms/azure_ai/passthrough/test_azure_ai_passthrough_transformation.py index 24f21751065..2d8ea27edb7 100644 --- a/tests/unit/llms/azure_ai/passthrough/test_azure_ai_passthrough_transformation.py +++ b/tests/unit/llms/azure_ai/passthrough/test_azure_ai_passthrough_transformation.py @@ -1,5 +1,7 @@ import json +from collections.abc import Mapping from datetime import datetime +from typing import Final from unittest.mock import MagicMock import httpx @@ -519,6 +521,162 @@ def test_cohere_rerank_relay_is_costed_per_search_unit(): assert logging_obj.response_cost_calculator(result=result) == pytest.approx(2 * per_query) +COHERE_EMBED_BODY: Final[Mapping[str, object]] = { + "id": "embed-1", + "texts": ["hello world"], + "embeddings": {"float": [[0.1, 0.2]]}, + "meta": {"api_version": {"version": "2"}, "billed_units": {"input_tokens": 2, "image_tokens": 0}}, + "response_type": "embeddings_by_type", +} +COHERE_V1_EMBED_META: Final[Mapping[str, object]] = { + "api_version": {"version": "1"}, + "billed_units": {"input_tokens": 2, "image_tokens": 0}, +} +TWO_VECTORS: Final = [[0.1, 0.2], [0.3, 0.4]] + + +def _cohere_embed_relay_result( + body: Mapping[str, object], native_path: str = "providers/cohere/v2/embed" +) -> tuple[object, Logging]: + return _relay_logging_result(AzureAIPassthroughConfig(), "embed-v-4-0", native_path, body) + + +def _foundry_embed_price(price_key: str) -> float: + return litellm.get_model_info("azure_ai/embed-v-4-0")[price_key] + + +def test_native_cohere_embed_relay_is_costed_per_billed_input_token() -> None: + result, logging_obj = _cohere_embed_relay_result(COHERE_EMBED_BODY) + per_token: Final = _foundry_embed_price("input_cost_per_token") + + assert isinstance(result, EmbeddingResponse) + assert logging_obj.call_type == "aembedding" + assert per_token > 0 + assert logging_obj.response_cost_calculator(result=result) == pytest.approx(2 * per_token) + + +@pytest.mark.parametrize( + "body", + [ + { + "id": "embed-1", + "texts": ["hello world", "second text"], + "embeddings": {"float": TWO_VECTORS}, + "meta": COHERE_V1_EMBED_META, + "response_type": "embeddings_by_type", + }, + { + "id": "embed-1", + "texts": ["hello world", "second text"], + "embeddings": TWO_VECTORS, + "meta": COHERE_V1_EMBED_META, + "response_type": "embeddings_floats", + }, + ], + ids=["embeddings_by_type", "embeddings_floats"], +) +def test_native_cohere_v1_embed_relay_is_costed_per_billed_input_token(body: Mapping[str, object]) -> None: + result, logging_obj = _cohere_embed_relay_result(body, "providers/cohere/v1/embed") + per_token: Final = _foundry_embed_price("input_cost_per_token") + + assert isinstance(result, EmbeddingResponse) + assert [(row["index"], row["embedding"]) for row in result.data] == list(enumerate(TWO_VECTORS)) + assert logging_obj.call_type == "aembedding" + assert per_token > 0 + assert logging_obj.response_cost_calculator(result=result) == pytest.approx(2 * per_token) + + +def test_native_cohere_embed_relay_prices_an_image_at_the_input_tokens_foundry_already_folded_it_into() -> None: + body: Final = { + **COHERE_EMBED_BODY, + "texts": [], + "images": [{"width": 64, "height": 64, "format": "image/png", "bit_depth": 8}], + "meta": {"api_version": {"version": "2"}, "billed_units": {"input_tokens": 24, "image_tokens": 6}}, + } + result, logging_obj = _cohere_embed_relay_result(body) + per_token: Final = _foundry_embed_price("input_cost_per_token") + per_image_token: Final = _foundry_embed_price("input_cost_per_image_token") + + assert isinstance(result, EmbeddingResponse) + assert per_token > 0 + assert per_image_token > 0 + assert logging_obj.response_cost_calculator(result=result) == pytest.approx(24 * per_token) + + + +def test_native_cohere_embed_relay_rows_keep_their_embedding_type() -> None: + result, _ = _cohere_embed_relay_result({**COHERE_EMBED_BODY, "embeddings": {"float": TWO_VECTORS, "int8": [[1, 2]]}}) + + assert isinstance(result, EmbeddingResponse) + assert [(row["type"], row["index"], row["embedding"]) for row in result.data] == [ + ("float", 0, TWO_VECTORS[0]), + ("float", 1, TWO_VECTORS[1]), + ("int8", 0, [1, 2]), + ] + +@pytest.mark.parametrize("texts", [None, "hello world", [1, 2], ["hello", 3]]) +def test_native_cohere_embed_relay_with_billed_units_is_costed_whatever_texts_holds(texts: object) -> None: + result, logging_obj = _cohere_embed_relay_result({**COHERE_EMBED_BODY, "texts": texts}) + + assert isinstance(result, EmbeddingResponse) + assert logging_obj.call_type == "aembedding" + assert logging_obj.response_cost_calculator(result=result) == pytest.approx( + 2 * _foundry_embed_price("input_cost_per_token") + ) + + +def test_native_cohere_embed_relay_without_texts_is_costed_per_billed_input_token() -> None: + body: Final = {key: value for key, value in COHERE_EMBED_BODY.items() if key != "texts"} + result, logging_obj = _cohere_embed_relay_result(body) + + assert isinstance(result, EmbeddingResponse) + assert logging_obj.call_type == "aembedding" + assert logging_obj.response_cost_calculator(result=result) == pytest.approx( + 2 * _foundry_embed_price("input_cost_per_token") + ) + + +@pytest.mark.parametrize( + ("texts", "counted_texts"), + [ + (["hello world", "a longer second text"], ["hello world", "a longer second text"]), + (["before <|endoftext|> after"], ["before <|endoftext|> after"]), + ("hello world", []), + (["hello", 3], []), + (None, []), + ], +) +def test_native_cohere_embed_relay_without_billed_units_is_costed_from_the_echoed_texts( + texts: object, counted_texts: list[str] +) -> None: + result, logging_obj = _cohere_embed_relay_result({**COHERE_EMBED_BODY, "texts": texts, "meta": {}}) + per_token: Final = _foundry_embed_price("input_cost_per_token") + echoed_tokens: Final = sum(len(litellm.encoding.encode(text, disallowed_special=())) for text in counted_texts) + + assert isinstance(result, EmbeddingResponse) + assert logging_obj.call_type == "aembedding" + assert result.usage.prompt_tokens == echoed_tokens + assert logging_obj.response_cost_calculator(result=result) == pytest.approx(echoed_tokens * per_token) + + +@pytest.mark.parametrize( + "body", + [ + {"message": "no embeddings here"}, + {**COHERE_EMBED_BODY, "embeddings": None}, + {**COHERE_EMBED_BODY, "embeddings": "vectors"}, + {**COHERE_EMBED_BODY, "meta": "not an object"}, + ], +) +def test_native_cohere_embed_relay_with_a_non_embed_body_keeps_the_passthrough_object_and_call_type( + body: Mapping[str, object], +) -> None: + result, logging_obj = _cohere_embed_relay_result(body) + + assert result == {"response": body} + assert logging_obj.call_type == "allm_passthrough_route" + + def test_image_generation_relay_is_costed_per_image(): result, logging_obj = _relay_logging_result( AzureAIPassthroughConfig(), "FLUX.2-pro", "openai/deployments/FLUX.2-pro/images/generations", IMAGE_BODY diff --git a/tests/unit/llms/bedrock/chat/invoke_transformations/test_bedrock_chat_invoke_transformations_anthropic_claude_transformation.py b/tests/unit/llms/bedrock/chat/invoke_transformations/test_bedrock_chat_invoke_transformations_anthropic_claude_transformation.py index 86c4b29eb53..d40abb4860b 100644 --- a/tests/unit/llms/bedrock/chat/invoke_transformations/test_bedrock_chat_invoke_transformations_anthropic_claude_transformation.py +++ b/tests/unit/llms/bedrock/chat/invoke_transformations/test_bedrock_chat_invoke_transformations_anthropic_claude_transformation.py @@ -2008,3 +2008,25 @@ def test_invoke_uses_opt_in_native_output_is_false_when_native_output_did_not_co assert not AmazonAnthropicClaudeConfig().uses_opt_in_native_output( model, optional_params, {"model_info": model_info} ) + + +@pytest.mark.usefixtures("local_model_cost_map", "local_beta_headers_config") +@pytest.mark.parametrize("drop_params", [True, False]) +def test_bedrock_chat_invoke_adds_the_binding_beta_so_block_binding_is_kept( + drop_params: bool, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setattr(litellm, "drop_params", False) + block_binding: Final = {"prefix_mismatch_behavior": "error"} + result: Final = AmazonAnthropicClaudeConfig().transform_request( + model="us.anthropic.claude-sonnet-4-6", + messages=[{"role": "user", "content": "Hello"}], + optional_params={ + "max_tokens": 2048, + "thinking": {"type": "enabled", "budget_tokens": 1024, "block_binding": block_binding}, + }, + litellm_params={"drop_params": drop_params}, + headers={}, + ) + + assert result["thinking"] == {"type": "enabled", "budget_tokens": 1024, "block_binding": block_binding} + assert "thinking-binding-controls-2026-08-01" in result["anthropic_beta"] diff --git a/tests/unit/llms/bedrock/chat/test_converse_transformation.py b/tests/unit/llms/bedrock/chat/test_converse_transformation.py index 154797bab5a..d82ba192b1f 100644 --- a/tests/unit/llms/bedrock/chat/test_converse_transformation.py +++ b/tests/unit/llms/bedrock/chat/test_converse_transformation.py @@ -1,6 +1,7 @@ import base64 import copy import json +import logging import os import re from collections.abc import Iterator, Mapping @@ -23,9 +24,9 @@ from litellm.llms.anthropic.chat.transformation import ( NATIVE_OUTPUT_MAX_OPTIONAL_PARAMETERS, NATIVE_OUTPUT_MAX_UNION_PARAMETERS, ) -from litellm.llms.bedrock.common_utils import BEDROCK_NATIVE_OUTPUT_MAX_PROPERTIES from litellm.llms.bedrock.base_aws_llm import BaseAWSLLM from litellm.llms.bedrock.chat.converse_transformation import AmazonConverseConfig +from litellm.llms.bedrock.common_utils import BEDROCK_NATIVE_OUTPUT_MAX_PROPERTIES from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler from litellm.types.llms.bedrock import ContentBlock, ConverseTokenUsageBlock, RequestObject from litellm.types.llms.openai import AllMessageValues @@ -966,7 +967,9 @@ def test_reasoning_effort_maps_to_reasoning_effort_for_openai_gpt5_converse(mode "us.openai.gpt-6-astra", ], ) -def test_openai_gpt5_converse_rejects_effort_level_disabled_in_model_map(model, local_model_cost_map): +def test_openai_gpt5_converse_rejects_effort_level_disabled_in_model_map( + model: str, local_model_cost_map: None, caplog: pytest.LogCaptureFixture +) -> None: config = AmazonConverseConfig() assert litellm.utils.is_explicitly_disabled_factory( model=model, custom_llm_provider="bedrock_converse", key="supports_minimal_reasoning_effort" @@ -980,15 +983,19 @@ def test_openai_gpt5_converse_rejects_effort_level_disabled_in_model_map(model, drop_params=False, ) - optional_params = config.map_openai_params( - non_default_params={"reasoning_effort": "minimal"}, - optional_params={}, - model=model, - drop_params=True, - ) + with caplog.at_level(logging.WARNING, logger="LiteLLM"): + optional_params: Final = config.map_openai_params( + non_default_params={"reasoning_effort": "minimal"}, + optional_params={}, + model=model, + drop_params=True, + ) _, additional_request_params, _, _ = config._prepare_request_params(optional_params, model) assert "reasoning" not in additional_request_params assert "thinking" not in additional_request_params + dropped: Final = [r.getMessage() for r in caplog.records if "Dropping reasoning_effort='minimal'" in r.getMessage()] + assert len(dropped) == 1 and model in dropped[0] + assert "model_info" not in dropped[0] @pytest.mark.parametrize( @@ -5348,7 +5355,6 @@ def test_recursive_output_config_format_follows_the_profile_native_key( assert ("outputConfig" in request) is forwarded - def test_sonnet_4_6_json_schema_still_uses_native_output_config(monkeypatch): monkeypatch.setenv("LITELLM_LOCAL_MODEL_COST_MAP", "True") monkeypatch.setattr(litellm, "model_cost", litellm.get_model_cost_map(url="")) @@ -10665,7 +10671,10 @@ def test_uses_opt_in_native_output_reads_the_base_model_of_an_application_infere _CALLER_OUTPUT_CONFIG_WITH_A_SCHEMA_OBJECT: Final = { - "textFormat": {"type": "json_schema", "structure": {"jsonSchema": {"name": "capital", "schema": {"type": "object"}}}} + "textFormat": { + "type": "json_schema", + "structure": {"jsonSchema": {"name": "capital", "schema": {"type": "object"}}}, + } } _BEDROCK_SCHEMA_TYPE_ERROR: Final = "outputConfig.textFormat.structure.jsonSchema.schema must be a string" @@ -10789,3 +10798,121 @@ def test_a_deployment_with_a_batch_control_plane_endpoint_keeps_it_out_of_chat_r assert response.choices[0].message.content == "ok" assert "aws_bedrock_control_plane_endpoint" not in route.calls[0].request.content.decode() + + +def _converse_bound_thinking_request( + headers: dict[str, str] | None, body_betas: tuple[str, ...] = (), drop_params: bool = True +) -> dict[str, object]: + thinking: Final = { + "type": "adaptive", + "block_binding": {"prefix_mismatch_behavior": "error"}, + "display": "updates", + } + data: Final = AmazonConverseConfig()._transform_request_helper( + model="anthropic.claude-opus-4-8-v1:0", + system_content_blocks=[], + optional_params={"thinking": thinking, **({"anthropic_beta": list(body_betas)} if body_betas else {})}, + messages=[{"role": "user", "content": "hi"}], + headers=headers, + drop_params=drop_params, + ) + assert thinking == { + "type": "adaptive", + "block_binding": {"prefix_mismatch_behavior": "error"}, + "display": "updates", + }, "the caller's thinking dict must survive for a fallback leg" + return data["additionalModelRequestFields"] + + +def test_converse_keeps_beta_gated_thinking_fields_the_bedrock_converse_row_carries( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setenv("LITELLM_LOCAL_ANTHROPIC_BETA_HEADERS", "true") + fields: Final = _converse_bound_thinking_request( + {"anthropic-beta": "thinking-binding-controls-2026-08-01,thinking-display-updates-2026-08-18"} + ) + assert fields["thinking"] == { + "type": "adaptive", + "block_binding": {"prefix_mismatch_behavior": "error"}, + "display": "updates", + } + + +def test_converse_adds_the_thinking_betas_so_gated_thinking_fields_are_kept( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setenv("LITELLM_LOCAL_ANTHROPIC_BETA_HEADERS", "true") + fields: Final = _converse_bound_thinking_request(None) + assert fields["thinking"] == { + "type": "adaptive", + "block_binding": {"prefix_mismatch_behavior": "error"}, + "display": "updates", + } + assert sorted(fields["anthropic_beta"]) == [ + "thinking-binding-controls-2026-08-01", + "thinking-display-updates-2026-08-18", + ] + + +def test_converse_keeps_the_callers_body_betas_next_to_the_added_thinking_betas( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setenv("LITELLM_LOCAL_ANTHROPIC_BETA_HEADERS", "true") + fields: Final = _converse_bound_thinking_request(None, body_betas=("context-1m-2025-08-07",)) + assert sorted(fields["anthropic_beta"]) == [ + "context-1m-2025-08-07", + "thinking-binding-controls-2026-08-01", + "thinking-display-updates-2026-08-18", + ] + + +def test_converse_sends_beta_gated_thinking_fields_as_is_without_drop_params(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setenv("LITELLM_LOCAL_ANTHROPIC_BETA_HEADERS", "true") + monkeypatch.setattr(litellm, "drop_params", False) + fields: Final = _converse_bound_thinking_request(None, drop_params=False) + assert fields["thinking"] == { + "type": "adaptive", + "block_binding": {"prefix_mismatch_behavior": "error"}, + "display": "updates", + } + + +def test_converse_keeps_beta_gated_thinking_fields_the_body_anthropic_beta_carries( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setenv("LITELLM_LOCAL_ANTHROPIC_BETA_HEADERS", "true") + fields: Final = _converse_bound_thinking_request( + None, body_betas=("thinking-binding-controls-2026-08-01", "thinking-display-updates-2026-08-18") + ) + assert fields["thinking"] == { + "type": "adaptive", + "block_binding": {"prefix_mismatch_behavior": "error"}, + "display": "updates", + } + + +def test_converse_drops_body_betas_the_bedrock_converse_row_rejects_when_adding_thinking_betas( + local_beta_headers_config: None, +) -> None: + fields: Final = _converse_bound_thinking_request( + None, body_betas=("per-turn-control-2026-07-01", "context-1m-2025-08-07") + ) + assert sorted(fields["anthropic_beta"]) == [ + "context-1m-2025-08-07", + "thinking-binding-controls-2026-08-01", + "thinking-display-updates-2026-08-18", + ] + + +def test_converse_sends_only_the_header_betas_when_header_and_body_both_carry_betas( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setenv("LITELLM_LOCAL_ANTHROPIC_BETA_HEADERS", "true") + fields: Final = _converse_bound_thinking_request( + {"anthropic-beta": "context-1m-2025-08-07"}, body_betas=("not-a-real-beta-2099",) + ) + assert sorted(fields["anthropic_beta"]) == [ + "context-1m-2025-08-07", + "thinking-binding-controls-2026-08-01", + "thinking-display-updates-2026-08-18", + ] diff --git a/tests/unit/llms/bedrock/embed/test_bedrock_embedding.py b/tests/unit/llms/bedrock/embed/test_bedrock_embedding.py index c98844fef55..889faf784bf 100644 --- a/tests/unit/llms/bedrock/embed/test_bedrock_embedding.py +++ b/tests/unit/llms/bedrock/embed/test_bedrock_embedding.py @@ -189,6 +189,290 @@ def test_bedrock_cohere_embedding_returns_the_scripted_vector(respx_mock: respx. assert response.data == [{"object": "embedding", "index": 0, "embedding": [0.3, 0.2, 0.1]}] +GOVCLOUD_WEST_RUNTIME: Final = "https://bedrock-runtime.us-gov-west-1.amazonaws.com" + + +@pytest.mark.parametrize( + ("model_id", "embed_response"), + [ + ("amazon.titan-embed-text-v2:0", titan_embedding_response), + ( + "amazon.nova-2-multimodal-embeddings-v1:0", + {"embeddings": [{"embeddingType": "TEXT", "embedding": [0.1, 0.2, 0.3]}]}, + ), + ("cohere.embed-english-v3", cohere_embedding_response), + ], +) +@pytest.mark.parametrize("sync_mode", [True, False]) +async def test_bedrock_embedding_calls_the_model_id_region_with_the_bare_model_id( + respx_mock: respx.MockRouter, + monkeypatch: pytest.MonkeyPatch, + model_id: str, + embed_response: dict[str, object], + sync_mode: bool, +) -> None: + monkeypatch.setenv("AWS_REGION_NAME", "us-east-1") + monkeypatch.setattr(litellm, "disable_aiohttp_transport", True) + route = respx_mock.post(f"{GOVCLOUD_WEST_RUNTIME}/model/{model_id}/invoke").mock( + return_value=httpx.Response(200, json=embed_response) + ) + request: Final = { + "model": f"bedrock/us-gov-west-1/{model_id}", + "input": [test_input], + "api_key": "test-bearer-token", + } + + response: Final = litellm.embedding(**request) if sync_mode else await litellm.aembedding(**request) + + assert route.call_count == 1 + assert response.data[0]["embedding"] == [0.1, 0.2, 0.3] + + +def test_bedrock_embedding_prices_a_region_prefixed_model_from_its_region_row( + respx_mock: respx.MockRouter, +) -> None: + model: Final = "bedrock/us-gov-west-1/amazon.titan-embed-text-v2:0" + respx_mock.post(f"{GOVCLOUD_WEST_RUNTIME}/model/amazon.titan-embed-text-v2:0/invoke").mock( + return_value=httpx.Response(200, json=titan_embedding_response) + ) + + response: Final = litellm.embedding(model=model, input=[test_input], api_key="test-bearer-token") + + assert response._hidden_params["response_cost"] == pytest.approx( + response.usage.prompt_tokens * litellm.model_cost[model]["input_cost_per_token"] + ) + + +def test_bedrock_embedding_explicit_aws_region_name_wins_over_the_model_id_region_for_routing_and_price( + respx_mock: respx.MockRouter, +) -> None: + route = respx_mock.post( + "https://bedrock-runtime.us-gov-east-1.amazonaws.com/model/amazon.titan-embed-text-v2:0/invoke" + ).mock(return_value=httpx.Response(200, json=titan_embedding_response)) + + response: Final = litellm.embedding( + model="bedrock/us-gov-west-1/amazon.titan-embed-text-v2:0", + input=[test_input], + api_key="test-bearer-token", + aws_region_name="us-gov-east-1", + ) + + assert route.call_count == 1 + assert response._hidden_params["response_cost"] == pytest.approx( + response.usage.prompt_tokens + * litellm.model_cost["bedrock/us-gov-east-1/amazon.titan-embed-text-v2:0"]["input_cost_per_token"] + ) + + +def test_bedrock_embedding_model_id_arn_region_wins_over_the_model_path_region_and_env( + respx_mock: respx.MockRouter, + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setenv("AWS_REGION_NAME", "us-west-2") + route = respx_mock.post(url__startswith=f"{GOVCLOUD_WEST_RUNTIME}/model/").mock( + return_value=httpx.Response(200, json=titan_embedding_response) + ) + + litellm.embedding( + model="bedrock/us-east-1/amazon.titan-embed-text-v2:0", + input=[test_input], + api_key="test-bearer-token", + model_id="arn:aws-us-gov:bedrock:us-gov-west-1::foundation-model/amazon.titan-embed-text-v2:0", + ) + + assert route.call_count == 1 + + +def test_bedrock_embedding_plain_model_id_calls_the_env_region_like_chat( + respx_mock: respx.MockRouter, + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setenv("AWS_REGION_NAME", "us-west-2") + route = respx_mock.post("https://bedrock-runtime.us-west-2.amazonaws.com/model/cohere.embed-english-v3/invoke").mock( + return_value=httpx.Response(200, json=cohere_embedding_response) + ) + + litellm.embedding( + model="bedrock/us-east-1/cohere.embed-english-v3", + input=[test_input], + api_key="test-bearer-token", + model_id="cohere.embed-english-v3", + ) + + assert route.call_count == 1 + + +def test_bedrock_embedding_rejects_an_empty_aws_region_name_instead_of_using_the_path_region( + respx_mock: respx.MockRouter, +) -> None: + route = respx_mock.post(url__startswith=GOVCLOUD_WEST_RUNTIME).mock( + return_value=httpx.Response(200, json=titan_embedding_response) + ) + + with pytest.raises(litellm.APIConnectionError, match="Invalid AWS region format"): + litellm.embedding( + model="bedrock/us-gov-west-1/amazon.titan-embed-text-v2:0", + input=[test_input], + api_key="test-bearer-token", + aws_region_name="", + ) + + assert route.call_count == 0 + + +@pytest.mark.parametrize( + "model,extra_params", + [ + ("bedrock/invoke/us-gov-west-1/amazon.titan-embed-text-v2:0", {}), + ("bedrock/us-gov-west-1/amazon.titan-embed-text-v2:0", {"model_id": ""}), + ], +) +def test_bedrock_embedding_region_path_variants_call_the_path_region_with_the_bare_id_and_its_price( + respx_mock: respx.MockRouter, + monkeypatch: pytest.MonkeyPatch, + model: str, + extra_params: dict[str, str], +) -> None: + monkeypatch.setenv("AWS_REGION_NAME", "us-east-1") + route = respx_mock.post(f"{GOVCLOUD_WEST_RUNTIME}/model/amazon.titan-embed-text-v2:0/invoke").mock( + return_value=httpx.Response(200, json=titan_embedding_response) + ) + + response: Final = litellm.embedding(model=model, input=[test_input], api_key="test-bearer-token", **extra_params) + + assert route.call_count == 1 + assert response._hidden_params["response_cost"] == pytest.approx( + response.usage.prompt_tokens + * litellm.model_cost["bedrock/us-gov-west-1/amazon.titan-embed-text-v2:0"]["input_cost_per_token"] + ) + + +def test_bedrock_embedding_explicit_region_outside_the_known_list_returns_the_vector_at_the_bare_price( + respx_mock: respx.MockRouter, +) -> None: + route = respx_mock.post( + "https://bedrock-runtime.zz-unlisted-1.amazonaws.com/model/amazon.titan-embed-text-v2:0/invoke" + ).mock(return_value=httpx.Response(200, json=titan_embedding_response)) + + response: Final = litellm.embedding( + model="bedrock/us-east-1/amazon.titan-embed-text-v2:0", + input=[test_input], + api_key="test-bearer-token", + aws_region_name="zz-unlisted-1", + ) + + assert route.call_count == 1 + assert response.data[0]["embedding"] == [0.1, 0.2, 0.3] + assert response._hidden_params["response_cost"] == pytest.approx( + response.usage.prompt_tokens * litellm.model_cost["amazon.titan-embed-text-v2:0"]["input_cost_per_token"] + ) + + +@pytest.mark.parametrize("sync_mode", [True, False]) +async def test_bedrock_embedding_without_a_region_path_bills_the_row_of_the_region_it_calls( + respx_mock: respx.MockRouter, + monkeypatch: pytest.MonkeyPatch, + sync_mode: bool, +) -> None: + monkeypatch.setattr(litellm, "disable_aiohttp_transport", True) + route = respx_mock.post(f"{GOVCLOUD_WEST_RUNTIME}/model/amazon.titan-embed-text-v2:0/invoke").mock( + return_value=httpx.Response(200, json=titan_embedding_response) + ) + request: Final = { + "model": "bedrock/amazon.titan-embed-text-v2:0", + "input": [test_input], + "api_key": "test-bearer-token", + "aws_region_name": "us-gov-west-1", + } + + response: Final = litellm.embedding(**request) if sync_mode else await litellm.aembedding(**request) + + assert route.call_count == 1 + assert response.model == "amazon.titan-embed-text-v2:0" + assert response._hidden_params["response_cost"] == pytest.approx( + response.usage.prompt_tokens + * litellm.model_cost["bedrock/us-gov-west-1/amazon.titan-embed-text-v2:0"]["input_cost_per_token"] + ) + + +def test_bedrock_cohere_embedding_bills_the_row_of_the_region_it_calls( + respx_mock: respx.MockRouter, + monkeypatch: pytest.MonkeyPatch, +) -> None: + regional_key: Final = "bedrock/us-gov-west-1/cohere.embed-english-v3" + monkeypatch.setitem( + litellm.model_cost, + regional_key, + {**litellm.model_cost["cohere.embed-english-v3"], "input_cost_per_token": 7e-07}, + ) + respx_mock.post(f"{GOVCLOUD_WEST_RUNTIME}/model/cohere.embed-english-v3/invoke").mock( + return_value=httpx.Response(200, json=cohere_embedding_response) + ) + + response: Final = litellm.embedding( + model="bedrock/cohere.embed-english-v3", + input=[test_input], + api_key="test-bearer-token", + aws_region_name="us-gov-west-1", + ) + + assert response._hidden_params["response_cost"] == pytest.approx( + response.usage.prompt_tokens * litellm.model_cost[regional_key]["input_cost_per_token"] + ) + + +def test_bedrock_embedding_with_a_govcloud_arn_model_id_bills_at_the_arn_region_row( + respx_mock: respx.MockRouter, +) -> None: + arn: Final = "arn:aws-us-gov:bedrock:us-gov-west-1::foundation-model/amazon.nova-2-multimodal-embeddings-v1:0" + route = respx_mock.post(url__startswith=f"{GOVCLOUD_WEST_RUNTIME}/model/").mock( + return_value=httpx.Response( + 200, json={"embeddings": [{"embeddingType": "TEXT", "embedding": [0.1, 0.2, 0.3]}]} + ) + ) + + response: Final = litellm.embedding( + model="bedrock/amazon.nova-2-multimodal-embeddings-v1:0", + input=[test_input], + api_key="test-bearer-token", + model_id=arn, + ) + + assert route.call_count == 1 + assert response._hidden_params["response_cost"] == pytest.approx( + response.usage.prompt_tokens + * litellm.model_cost["bedrock/us-gov-west-1/amazon.nova-2-multimodal-embeddings-v1:0"]["input_cost_per_token"] + ) + + +@pytest.mark.parametrize( + "model", + [ + "bedrock/us-gov-west-1/async_invoke/amazon.nova-2-multimodal-embeddings-v1:0", + "bedrock/async_invoke/us-gov-west-1/amazon.nova-2-multimodal-embeddings-v1:0", + ], +) +def test_bedrock_async_invoke_embedding_with_a_region_path_posts_the_bare_id_to_that_region( + respx_mock: respx.MockRouter, + model: str, +) -> None: + route = respx_mock.post(f"{GOVCLOUD_WEST_RUNTIME}/async-invoke").mock( + return_value=httpx.Response( + 200, json={"invocationArn": "arn:aws-us-gov:bedrock:us-gov-west-1:123456789012:async-invoke/abc123"} + ) + ) + + litellm.embedding( + model=model, + input=[test_input], + api_key="test-bearer-token", + output_s3_uri="s3://test-bucket/async-invoke-output/", + ) + + assert route.call_count == 1 + assert json.loads(route.calls[0].request.content)["modelId"] == "amazon.nova-2-multimodal-embeddings-v1:0" + + @pytest.mark.asyncio async def test_async_bedrock_embedding_with_bearer_token(): """Test async embedding functionality with bearer token authentication""" diff --git a/tests/unit/llms/bedrock/messages/invoke_transformations/test_anthropic_claude_transformation.py b/tests/unit/llms/bedrock/messages/invoke_transformations/test_anthropic_claude_transformation.py index c096e2126ae..74f6f3030cb 100644 --- a/tests/unit/llms/bedrock/messages/invoke_transformations/test_anthropic_claude_transformation.py +++ b/tests/unit/llms/bedrock/messages/invoke_transformations/test_anthropic_claude_transformation.py @@ -16,7 +16,7 @@ import httpx import pytest import requests_mock as requests_mock_lib import respx -from pydantic import TypeAdapter +from pydantic import BaseModel, ConfigDict, TypeAdapter import litellm from litellm.constants import ( @@ -4200,6 +4200,98 @@ def test_bedrock_messages_without_guardrail_config_sends_no_guardrail_headers() assert not any(name.startswith("X-Amzn-Bedrock-Guardrail") for name in headers) +@pytest.mark.usefixtures("local_model_cost_map", "local_beta_headers_config") +def test_bedrock_messages_keeps_mid_conversation_output_config_its_auto_added_beta_gates() -> None: + """Invoke learns its body betas after the shared transform, so the gated field it supports must survive.""" + from litellm.types.router import GenericLiteLLMParams + + messages: Final = [ + {"role": "user", "content": [{"type": "text", "text": "Hello"}]}, + {"role": "assistant", "content": [{"type": "text", "text": "Hi"}]}, + {"role": "system", "content": "Answer briefly", "output_config": {"effort": "high"}}, + {"role": "user", "content": [{"type": "text", "text": "Go on"}]}, + ] + result: Final = AmazonAnthropicClaudeMessagesConfig().transform_anthropic_messages_request( + model="anthropic.claude-opus-4-8-v1:0", + messages=messages, + anthropic_messages_optional_request_params={"max_tokens": 4096}, + litellm_params=GenericLiteLLMParams(drop_params=True), + headers={}, + ) + + assert "mid-conversation-output-config-2026-07-01" in result["anthropic_beta"] + assert {"role": "system", "content": "Answer briefly", "output_config": {"effort": "high"}} in result["messages"] + + +class _SentMessage(BaseModel): + model_config = ConfigDict(frozen=True) + role: str + content: list[dict[str, object]] + + +_SENT_MESSAGES: Final = TypeAdapter(list[_SentMessage]) + + +@pytest.mark.usefixtures("local_model_cost_map", "local_beta_headers_config") +@pytest.mark.parametrize("drop_params", [True, False]) +def test_bedrock_messages_drops_tool_changes_from_a_system_turn_an_older_model_gets_as_a_user_turn( + drop_params: bool, monkeypatch: pytest.MonkeyPatch +) -> None: + """A model without mid-conversation system support sends no tool-changes beta, so under drop_params the + converted turn must not carry the blocks that beta gates, and without it they go out as sent.""" + monkeypatch.setattr(litellm, "drop_params", False) + from litellm.types.router import GenericLiteLLMParams + + messages: Final = [ + {"role": "user", "content": [{"type": "text", "text": "Hello"}]}, + {"role": "assistant", "content": [{"type": "text", "text": "Hi"}]}, + { + "role": "system", + "content": [ + {"type": "text", "text": "A weather tool is now available"}, + {"type": "tool_addition", "tool": {"type": "tool_reference", "name": "get_weather"}}, + ], + }, + {"role": "user", "content": [{"type": "text", "text": "Go on"}]}, + ] + result: Final = AmazonAnthropicClaudeMessagesConfig().transform_anthropic_messages_request( + model="anthropic.claude-3-7-sonnet-20250219-v1:0", + messages=messages, + anthropic_messages_optional_request_params={"max_tokens": 4096}, + litellm_params=GenericLiteLLMParams(drop_params=drop_params), + headers={}, + ) + + sent: Final = _SENT_MESSAGES.validate_python(result["messages"]) + blocks: Final = [ + block for message in sent for block in message.content + ] # comprehension-ok: flattening the sent content blocks + assert "mid-conversation-tool-changes-2026-07-01" not in result.get("anthropic_beta", []) + assert any(block["type"] == "tool_addition" for block in blocks) is not drop_params + assert {"type": "text", "text": "A weather tool is now available"} in blocks + assert all(message.role != "system" for message in sent) + + +@pytest.mark.usefixtures("local_model_cost_map", "local_beta_headers_config") +@pytest.mark.parametrize("drop_params", (True, False)) +def test_bedrock_messages_sends_the_binding_beta_with_block_binding_whatever_drop_params(drop_params: bool) -> None: + from litellm.types.router import GenericLiteLLMParams + + result: Final = AmazonAnthropicClaudeMessagesConfig().transform_anthropic_messages_request( + model="us.anthropic.claude-sonnet-4-6", + messages=[{"role": "user", "content": "Reply with OK"}], + anthropic_messages_optional_request_params={ + "max_tokens": 2048, + "thinking": {"type": "enabled", "budget_tokens": 1024, "block_binding": BLOCK_BINDING}, + }, + litellm_params=GenericLiteLLMParams(drop_params=drop_params), + headers={}, + ) + + assert result["thinking"]["block_binding"] == BLOCK_BINDING + assert "thinking-binding-controls-2026-08-01" in result["anthropic_beta"] + + @dataclass(slots=True) class BedrockDownAnthropicUp: anthropic_bodies: tuple[dict[str, object], ...] = () diff --git a/tests/unit/llms/bedrock/responses/test_bedrock_openai_responses.py b/tests/unit/llms/bedrock/responses/test_bedrock_openai_responses.py index 6da131f38cc..a1466cfdae4 100644 --- a/tests/unit/llms/bedrock/responses/test_bedrock_openai_responses.py +++ b/tests/unit/llms/bedrock/responses/test_bedrock_openai_responses.py @@ -7,13 +7,17 @@ falls back to the Chat Completions bridge and rides Converse. import json import logging from importlib.resources import files +from typing import Final from unittest.mock import patch import pytest import litellm from litellm.llms.bedrock.common_utils import bedrock_supports_openai_responses -from litellm.llms.bedrock.responses.transformation import BedrockOpenAIResponsesConfig +from litellm.llms.bedrock.responses.transformation import ( + BedrockOpenAIResponsesConfig, + _without_disabled_reasoning_effort, +) from litellm.responses.file_search.emulated_handler import should_use_emulated_file_search from litellm.types.router import GenericLiteLLMParams from litellm.types.utils import LlmProviders @@ -337,13 +341,37 @@ class TestDisabledReasoningEffort: ) @pytest.mark.parametrize("model", ["us.openai.gpt-5.6-luna", MODEL]) - def test_effort_level_disabled_in_model_map_is_dropped_with_drop_params(self, model, local_model_cost_map): - params = _cfg().map_openai_params( - response_api_optional_params={"reasoning": {"effort": "minimal", "summary": "auto"}, "max_output_tokens": 64}, - model=model, - drop_params=True, - ) + def test_effort_level_disabled_in_model_map_is_dropped_with_a_warning( + self, model: str, local_model_cost_map: None, caplog: pytest.LogCaptureFixture + ) -> None: + with caplog.at_level(logging.WARNING, logger="LiteLLM"): + params: Final = _cfg().map_openai_params( + response_api_optional_params={ + "reasoning": {"effort": "minimal", "summary": "auto"}, + "max_output_tokens": 64, + }, + model=model, + drop_params=True, + ) assert params == {"reasoning": {"summary": "auto"}, "max_output_tokens": 64} + dropped: Final = [ + r.getMessage() for r in caplog.records if "Dropping reasoning.effort='minimal'" in r.getMessage() + ] + assert len(dropped) == 1 and model in dropped[0] + assert "model_info" not in dropped[0] + + def test_bedrock_disabled_effort_drop_warns_on_its_own( + self, local_model_cost_map: None, caplog: pytest.LogCaptureFixture + ) -> None: + with caplog.at_level(logging.WARNING, logger="LiteLLM"): + params: Final = _without_disabled_reasoning_effort( + {"reasoning": {"effort": "minimal"}, "max_output_tokens": 64}, MODEL, True + ) + assert params == {"max_output_tokens": 64} + dropped: Final = [ + r.getMessage() for r in caplog.records if "Dropping reasoning.effort='minimal'" in r.getMessage() + ] + assert len(dropped) == 1 and MODEL in dropped[0] def test_effort_only_reasoning_is_removed_when_dropped(self, local_model_cost_map): params = _cfg().map_openai_params( diff --git a/tests/unit/llms/bedrock/test_claude_platform_provider.py b/tests/unit/llms/bedrock/test_claude_platform_provider.py index 2353a45139b..9142fe45752 100644 --- a/tests/unit/llms/bedrock/test_claude_platform_provider.py +++ b/tests/unit/llms/bedrock/test_claude_platform_provider.py @@ -207,6 +207,26 @@ def test_claude_platform_api_key_auth_sets_workspace_and_key_headers(): assert headers["anthropic-beta"] == "skills-2025-10-02" +def test_claude_platform_chat_sends_the_binding_beta_when_thinking_carries_block_binding(): + from litellm.llms.bedrock.claude_platform.transformation import ( + BedrockClaudePlatformConfig, + ) + + headers: Final = BedrockClaudePlatformConfig().validate_environment( + api_key="fake-platform-key", + headers={}, + model="claude-opus-5-5", + messages=[{"role": "user", "content": "hello"}], + optional_params={ + "workspace_id": "wrkspc_test", + "thinking": {"type": "adaptive", "block_binding": {"prefix_mismatch_behavior": "error"}}, + }, + litellm_params={}, + ) + + assert "thinking-binding-controls-2026-08-01" in headers["anthropic-beta"].split(",") + + def test_claude_platform_does_not_use_standard_anthropic_api_key(monkeypatch): from litellm.llms.bedrock.claude_platform.transformation import ( BedrockClaudePlatformConfig, diff --git a/tests/unit/llms/bedrock_mantle/test_bedrock_mantle_responses_transformation.py b/tests/unit/llms/bedrock_mantle/test_bedrock_mantle_responses_transformation.py index a2289167ad4..0ae6a73968d 100644 --- a/tests/unit/llms/bedrock_mantle/test_bedrock_mantle_responses_transformation.py +++ b/tests/unit/llms/bedrock_mantle/test_bedrock_mantle_responses_transformation.py @@ -9,6 +9,7 @@ gate, the URL construction for both paths, and the shared Bearer auth. import copy import logging +from typing import Final import pytest from botocore.exceptions import ( @@ -572,6 +573,32 @@ class TestBedrockMantleServiceTier: assert "priority" in str(mock_warning.call_args) +class TestBedrockMantleRefusedEffort: + def test_dropping_a_refused_effort_does_not_suggest_declaring_levels_mantle_ignores( + self, local_cost_map: None, caplog: pytest.LogCaptureFixture + ) -> None: + refused: Final = next( + level + for level in ("none", "minimal", "low") + if litellm.model_cost["gpt-6.1-sol"].get(f"supports_{level}_reasoning_effort") is False + ) + with caplog.at_level(logging.WARNING, logger="LiteLLM"): + params: Final = BedrockMantleResponsesAPIConfig().map_openai_params( + response_api_optional_params={"reasoning": {"effort": refused}}, + model="openai.gpt-6.1-sol", + drop_params=True, + ) + + assert "reasoning" not in params + drop_warnings: Final = [ + record.getMessage() + for record in caplog.records + if f"Dropping reasoning.effort={refused!r}" in record.getMessage() + ] + assert len(drop_warnings) == 1 + assert "model_info" not in drop_warnings[0] + + class TestBedrockMantleReasoningSummary: @pytest.mark.parametrize("summary", ["concise", "detailed"]) def test_unsupported_reasoning_summary_dropped_when_drop_params_true(self, summary): diff --git a/tests/unit/llms/cohere/embed/test_cohere_embedding_transformation.py b/tests/unit/llms/cohere/embed/test_cohere_embedding_transformation.py index 516ea1f5221..245d30a6283 100644 --- a/tests/unit/llms/cohere/embed/test_cohere_embedding_transformation.py +++ b/tests/unit/llms/cohere/embed/test_cohere_embedding_transformation.py @@ -7,7 +7,12 @@ import respx import litellm from litellm.litellm_core_utils.litellm_logging import Logging -from litellm.llms.cohere.embed.transformation import CohereEmbeddingConfig +from litellm.llms.cohere.embed.transformation import ( + CohereEmbeddingConfig, + CohereEmbeddingRowShape, + CohereImageTokenBilling, + cohere_embedding_response, +) from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler from litellm.types.utils import CallTypes, EmbeddingResponse @@ -108,18 +113,50 @@ def test_cohere_embedding_response_counts_tokens_when_provider_usage_is_missing( assert response.usage.total_tokens == response.usage.prompt_tokens -def test_cohere_embedding_usage_includes_text_and_image_token_details(): - config: Final = CohereEmbeddingConfig() - usage: Final = config._calculate_usage( - input=["hello"], - encoding=litellm.encoding, - meta={"billed_units": {"input_tokens": 5, "images": 2}}, +def test_cohere_embedding_fallback_counts_special_token_text_as_plain_text() -> None: + text: Final = "before <|endoftext|> after" + response: Final = CohereEmbeddingConfig().transform_embedding_response( + model="embed-english-v3.0", + raw_response=httpx.Response(200, json={"embeddings": {"float": [[0.1]]}}), + model_response=EmbeddingResponse(), + logging_obj=_logging_obj([text]), + api_key="test-key", + request_data={"texts": [text]}, + optional_params={}, + litellm_params={}, ) + assert response.usage.prompt_tokens == len(litellm.encoding.encode(text, disallowed_special=())) + + +@pytest.mark.parametrize("image_key", ["images", "image_tokens"]) +def test_cohere_embedding_usage_includes_text_and_image_token_details(image_key: str) -> None: + usage: Final = cohere_embedding_response( + {"embeddings": {"float": [[0.1]]}, "meta": {"billed_units": {"input_tokens": 5, image_key: 2}}}, + "embed-english-v3.0", + litellm.encoding, + ["hello"], + CohereImageTokenBilling.SEPARATE_FROM_INPUT_TOKENS, + CohereEmbeddingRowShape.OPENAI, + ).usage + assert usage.prompt_tokens == 7 assert (usage.prompt_tokens_details.text_tokens, usage.prompt_tokens_details.image_tokens) == (5, 2) +def test_cohere_embedding_usage_folded_image_tokens_count_input_tokens_only() -> None: + usage: Final = cohere_embedding_response( + {"embeddings": {"float": [[0.1]]}, "meta": {"billed_units": {"input_tokens": 24, "image_tokens": 6}}}, + "embed-v-4-0", + litellm.encoding, + [], + CohereImageTokenBilling.FOLDED_INTO_INPUT_TOKENS, + CohereEmbeddingRowShape.OPENAI, + ).usage + + assert (usage.prompt_tokens, usage.total_tokens, usage.prompt_tokens_details) == (24, 24, None) + + def test_cohere_embedding_error_class_preserves_status_and_message(): error: Final = CohereEmbeddingConfig().get_error_class( error_message="invalid embedding request", diff --git a/tests/unit/llms/cohere/embed/test_transformation.py b/tests/unit/llms/cohere/embed/test_transformation.py index c0f355cfb57..abae30013fe 100644 --- a/tests/unit/llms/cohere/embed/test_transformation.py +++ b/tests/unit/llms/cohere/embed/test_transformation.py @@ -1,6 +1,6 @@ import datetime import json -from collections.abc import Iterator +from collections.abc import Iterator, Mapping from typing import Final import httpx @@ -70,8 +70,11 @@ def test_cohere_embedding_response_lists_every_embedding_type_in_provider_order( ("meta", "expected_counts", "expected_details"), [ ({"billed_units": {"input_tokens": 5}}, (5, 0, 5), (5, None)), + ({"billed_units": {"input_tokens": 5, "image_tokens": 0}}, (5, 0, 5), (5, None)), ({"billed_units": {"images": 3}}, (3, 0, 3), (None, 3)), ({"billed_units": {"input_tokens": 5, "images": 3}}, (8, 0, 8), (5, 3)), + ({"billed_units": {"input_tokens": 0, "image_tokens": 6}}, (6, 0, 6), (0, 6)), + ({"billed_units": {"input_tokens": 3, "image_tokens": 6}}, (9, 0, 9), (3, 6)), ({"billed_units": {"input_tokens": 0}}, (0, 0, 0), (0, None)), ( {"api_version": {"version": "2"}, "billed_units": {"input_tokens": 2, "output_tokens": 9}}, @@ -80,11 +83,15 @@ def test_cohere_embedding_response_lists_every_embedding_type_in_provider_order( ), ], ) -def test_cohere_embedding_usage_comes_from_billed_units(meta, expected_counts, expected_details): - response = _transform({"embeddings": {"float": FLOAT_VECTORS}, "meta": meta}) +def test_cohere_embedding_usage_comes_from_billed_units( + meta: Mapping[str, object], + expected_counts: tuple[int, int, int], + expected_details: tuple[int | None, int | None], +) -> None: + response: Final = _transform({"embeddings": {"float": FLOAT_VECTORS}, "meta": meta}) assert response.usage is not None - details = response.usage.prompt_tokens_details + details: Final = response.usage.prompt_tokens_details assert details is not None assert _token_counts(response) == expected_counts assert (details.text_tokens, details.image_tokens) == expected_details @@ -123,8 +130,20 @@ def test_cohere_embedding_rows_pass_through_whatever_each_type_lists(embeddings, assert [row["embedding"] for row in response.data] == expected_vectors -@pytest.mark.parametrize("embeddings", [FLOAT_VECTORS, [], None, "vectors", 3]) -def test_cohere_embeddings_that_are_not_keyed_by_type_are_rejected(embeddings): +@pytest.mark.parametrize("vectors", [FLOAT_VECTORS, []]) +def test_cohere_v1_float_embeddings_list_one_row_per_vector(vectors: list[list[float]]) -> None: + response: Final = _transform( + {"embeddings": vectors, "meta": {"billed_units": {"input_tokens": 1}}, "response_type": "embeddings_floats"} + ) + + assert response.data == [ + {"object": "embedding", "index": index, "embedding": vector} for index, vector in enumerate(vectors) + ] + assert _token_counts(response) == (1, 0, 1) + + +@pytest.mark.parametrize("embeddings", [None, "vectors", 3]) +def test_cohere_embeddings_that_are_neither_keyed_by_type_nor_a_list_are_rejected(embeddings: object) -> None: with pytest.raises(ValidationError): _transform({"embeddings": embeddings, "meta": {"billed_units": {"input_tokens": 1}}}) @@ -147,12 +166,15 @@ def test_cohere_embedding_body_that_is_not_an_object_is_rejected(payload): _transform(payload) -def test_cohere_embedding_body_without_embeddings_is_rejected(): - with pytest.raises(KeyError): +def test_cohere_embedding_body_without_embeddings_is_rejected() -> None: + with pytest.raises(ValidationError): _transform({"meta": {"billed_units": {"input_tokens": 1}}}) COHERE_V2_EMBED_URL: Final = "https://api.cohere.ai/v2/embed" +ONE_PIXEL_PNG: Final = ( + "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGP4z8AAAAMBAQDJ/pLvAAAAAElFTkSuQmCC" +) @pytest.fixture @@ -362,13 +384,43 @@ async def test_cohere_embed_v4_with_optional_params(respx_mock: respx.MockRouter assert response.data == [{"object": "embedding", "index": 0, "embedding": vector}] +@pytest.mark.usefixtures("_cohere_httpx_transport") +@pytest.mark.respx(assert_all_called=True) +@pytest.mark.parametrize(("input_tokens", "image_tokens"), [(0, 6), (3, 6)]) +async def test_cohere_embed_v4_image_tokens_are_billed_apart_from_input_tokens( + respx_mock: respx.MockRouter, input_tokens: int, image_tokens: int +) -> None: + respx_mock.post(COHERE_V2_EMBED_URL).mock( + return_value=httpx.Response( + 200, + json={ + "id": "5807ee2e-0cda-445a-9ec8-864c60a06606", + "embeddings": {"float": [[0.125, -0.375]]}, + "texts": [], + "images": [{"width": 1, "height": 1, "format": "image/png", "bit_depth": 8}], + "meta": { + "api_version": {"version": "2"}, + "billed_units": {"input_tokens": input_tokens, "image_tokens": image_tokens}, + }, + "response_type": "embeddings_by_type", + }, + ) + ) + model_info: Final = litellm.get_model_info("cohere/embed-v4.0") + + response: Final = await _embed(True, input=[ONE_PIXEL_PNG], input_type="image") + + assert model_info["input_cost_per_image_token"] > 0 + assert response._hidden_params["response_cost"] == pytest.approx( + input_tokens * model_info["input_cost_per_token"] + image_tokens * model_info["input_cost_per_image_token"] + ) + + @pytest.mark.usefixtures("_cohere_httpx_transport") @pytest.mark.respx(assert_all_called=True) @pytest.mark.parametrize("sync_mode", [True, False]) async def test_cohere_embed_v4_image_embedding(respx_mock: respx.MockRouter, sync_mode: bool) -> None: - image: Final = ( - "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGP4z8AAAAMBAQDJ/pLvAAAAAElFTkSuQmCC" - ) + image: Final = ONE_PIXEL_PNG route: Final = respx_mock.post(COHERE_V2_EMBED_URL).mock( return_value=httpx.Response( 200, diff --git a/tests/unit/llms/custom_httpx/test_llm_http_handler.py b/tests/unit/llms/custom_httpx/test_llm_http_handler.py index 90d1f6772a0..dca21db5b6f 100644 --- a/tests/unit/llms/custom_httpx/test_llm_http_handler.py +++ b/tests/unit/llms/custom_httpx/test_llm_http_handler.py @@ -4911,6 +4911,160 @@ async def test_async_realtime_bridges_a_transcription_session_through_the_provid assert [bytes(request.audio) for request in speech_client.requests[1:]] == [b"\x00\x01" * 800, b"\x00\x01" * 800] +class _FakeSonioxSocket: + def __init__(self) -> None: + self.sent: Final[list[str | bytes]] = [] + self._responses: Final[asyncio.Queue[str]] = asyncio.Queue() + + async def __aenter__(self) -> "_FakeSonioxSocket": + return self + + async def __aexit__(self, exc_type, exc_value, traceback) -> None: + return None + + async def send(self, message: str | bytes) -> None: + self.sent.append(message) + if message == '{"type":"finalize"}': + tokens = [{"text": "Hello", "is_final": True}, {"text": "", "is_final": True}] + await self._responses.put(json.dumps({"tokens": tokens, "total_audio_proc_ms": 1200})) + + async def recv(self, decode: bool | None = None) -> str: + return await self._responses.get() + + async def close(self) -> None: + return None + + +@pytest.mark.asyncio +async def test_async_realtime_bridges_a_soniox_transcription_through_the_wrapped_websocket(): + import websockets.exceptions # noqa: F401 # binds the submodule so async_realtime's except clause resolves, as in the proxy process + + from litellm.llms.soniox.realtime.transformation import SonioxRealtimeBackend, SonioxRealtimeConfig + + upstream = _FakeSonioxSocket() + wrapped: list[object] = [] + provider_config = SonioxRealtimeConfig( + wrap=lambda backend: wrapped.append(backend) or SonioxRealtimeBackend(backend) + ) + audio = b"\x00\x01" * 800 + client_ws = _ScriptedClientWebSocket( + [ + json.dumps( + { + "type": "session.update", + "session": { + "type": "transcription", + "audio": { + "input": { + "format": {"type": "audio/pcm", "rate": 16000}, + "transcription": {"model": "stt-rt-v5", "language": "en"}, + "turn_detection": None, + } + }, + }, + } + ), + json.dumps({"type": "input_audio_buffer.append", "audio": base64.b64encode(audio).decode()}), + json.dumps({"type": "input_audio_buffer.commit"}), + ], + last_event_type="conversation.item.input_audio_transcription.completed", + ) + logging_obj = Mock() + logging_obj.litellm_trace_id = "trace_1" + logging_obj.model_call_details = {} + logging_obj.dispatch_success_handlers = AsyncMock() + logging_obj.dispatch_failure_handlers = AsyncMock() + handler = BaseLLMHTTPHandler() + + with patch.object(handler, "_open_realtime_backend_ws", AsyncMock(return_value=upstream)) as dial: + await handler.async_realtime( + model="stt-rt-v5", + websocket=client_ws, + logging_obj=logging_obj, + provider_config=provider_config, + headers={}, + api_key="soniox-key", + query_params={"model": "stt-rt-v5", "intent": "transcription"}, + ) + + assert dial.await_args.args[1:3] == ( + "wss://stt-rt.soniox.com/transcribe-websocket", + {"Authorization": "Bearer soniox-key"}, + ) + assert wrapped == [upstream] + assert json.loads(upstream.sent[0]) == { + "model": "stt-rt-v5", + "audio_format": "pcm_s16le", + "sample_rate": 16000, + "num_channels": 1, + "enable_endpoint_detection": False, + "language_hints": ["en"], + } + assert upstream.sent[1:] == [audio, '{"type":"finalize"}'] + events = client_ws.sent_events() + assert [event["type"] for event in events] == [ + "session.created", + "session.updated", + "input_audio_buffer.speech_started", + "conversation.item.input_audio_transcription.delta", + "conversation.item.input_audio_transcription.completed", + ] + assert events[-1]["transcript"] == "Hello" + assert events[-1]["usage"] == {"type": "duration", "seconds": 1.2} + + +@pytest.mark.asyncio +async def test_async_realtime_soniox_rejects_a_bad_session_update_and_still_transcribes_audio_sent_without_one(): + import websockets.exceptions # noqa: F401 # binds the submodule so async_realtime's except clause resolves, as in the proxy process + + from litellm.llms.soniox.realtime.transformation import SonioxRealtimeConfig + + upstream = _FakeSonioxSocket() + audio = b"\x00\x01" * 800 + client_ws = _ScriptedClientWebSocket( + [ + json.dumps( + { + "type": "session.update", + "session": {"type": "transcription", "audio": {"input": {"format": {"type": "audio/pcmu"}}}}, + } + ), + json.dumps({"type": "input_audio_buffer.append", "audio": base64.b64encode(audio).decode()}), + json.dumps({"type": "input_audio_buffer.commit"}), + ], + last_event_type="conversation.item.input_audio_transcription.completed", + ) + logging_obj = Mock() + logging_obj.litellm_trace_id = "trace_1" + logging_obj.model_call_details = {} + logging_obj.dispatch_success_handlers = AsyncMock() + logging_obj.dispatch_failure_handlers = AsyncMock() + handler = BaseLLMHTTPHandler() + + with patch.object(handler, "_open_realtime_backend_ws", AsyncMock(return_value=upstream)): + await handler.async_realtime( + model="stt-rt-v5", + websocket=client_ws, + logging_obj=logging_obj, + provider_config=SonioxRealtimeConfig(), + headers={}, + api_key="soniox-key", + query_params={"model": "stt-rt-v5", "intent": "transcription"}, + ) + + assert json.loads(upstream.sent[0])["sample_rate"] == 24_000 + assert upstream.sent[1:] == [audio, '{"type":"finalize"}'] + events = client_ws.sent_events() + errors = [event for event in events if event["type"] == "error"] + assert errors == [ + { + "type": "error", + "error": {"type": "invalid_request_error", "message": "Soniox realtime requires pcm16 input audio"}, + } + ] + assert events[-1]["transcript"] == "Hello" + + @pytest.mark.asyncio async def test_responses_agentic_followup_does_not_repeat_request_params_from_plan_kwargs(monkeypatch): """A plan whose kwargs repeat a request param must not crash the Responses follow-up with a duplicate keyword""" @@ -5634,6 +5788,63 @@ async def test_anthropic_messages_dropped_safeguards_send_neither_the_field_nor_ assert "dangerous-tool-use-2026-09-03" not in sent.headers.get("anthropic-beta", ""), dict(sent.headers) +class TestAgenticFollowUpKeepsTheProviderPrefix: + @staticmethod + async def _run_followup( + model: str, custom_llm_provider: str, patched_model: str | None = None + ) -> ModelResponse: + from litellm.llms.custom_httpx.llm_http_handler import BaseLLMHTTPHandler + from litellm.types.integrations.custom_logger import ( + AgenticLoopPlan, + AgenticLoopRequestPatch, + ) + + response: Final = await BaseLLMHTTPHandler()._execute_chat_completion_agentic_plan( + plan=AgenticLoopPlan( + run_agentic_loop=True, + request_patch=AgenticLoopRequestPatch( + messages=[{"role": "user", "content": "hi"}], model=patched_model + ), + ), + model=model, + messages=[{"role": "user", "content": "hi"}], + optional_params={"mock_response": "ok from followup"}, + kwargs={}, + custom_llm_provider=custom_llm_provider, + depth=0, + max_loops=2, + fingerprints=[], + fingerprint="fp", + ) + assert isinstance(response, ModelResponse) + return response + + @pytest.mark.asyncio + async def test_a_sub_path_model_still_resolves_a_provider(self) -> None: + response: Final = await self._run_followup("mantle/anthropic.claude-sonnet-5", "bedrock") + + assert response.choices[0].message.content == "ok from followup" + + @pytest.mark.asyncio + async def test_an_ordinary_model_still_resolves_a_provider(self) -> None: + response: Final = await self._run_followup("gpt-4o-mini", "openai") + + assert response.choices[0].message.content == "ok from followup" + + @pytest.mark.asyncio + async def test_a_hook_that_patches_another_providers_model_reaches_that_provider(self) -> None: + response: Final = await self._run_followup("gpt-4o", "openai", patched_model="anthropic/claude-sonnet-4-5") + + assert response.model == "claude-sonnet-4-5" + assert response._hidden_params["custom_llm_provider"] == "anthropic" + + @pytest.mark.asyncio + async def test_a_hook_echoing_an_org_style_model_stays_on_the_request_provider(self) -> None: + response: Final = await self._run_followup("openai/gpt-4o", "openrouter", patched_model="openai/gpt-4o") + + assert response._hidden_params["custom_llm_provider"] == "openrouter" + + @pytest.mark.asyncio @pytest.mark.parametrize("stream", [False, True]) @pytest.mark.parametrize("environment_base", [None, "ANTHROPIC_API_BASE", "ANTHROPIC_BASE_URL"]) diff --git a/tests/unit/llms/dashscope/test_dashscope_chat_transformation.py b/tests/unit/llms/dashscope/test_dashscope_chat_transformation.py index a3fd5933a83..99a8004b0a1 100644 --- a/tests/unit/llms/dashscope/test_dashscope_chat_transformation.py +++ b/tests/unit/llms/dashscope/test_dashscope_chat_transformation.py @@ -5,15 +5,50 @@ These tests validate the DashScopeConfig class which extends OpenAIGPTConfig. DashScope is an OpenAI-compatible provider with minor customizations. """ +from typing import Final +import pytest from litellm.types.llms.openai import AllMessageValues -import pytest import litellm from litellm import completion from litellm.llms.dashscope.chat.transformation import DashScopeChatConfig +WORKSPACE: Final = "https://llm-abc123.cn-beijing.maas.aliyuncs.com" + + +def test_get_complete_url_derives_the_compatible_base_from_any_standard_base(): + config: Final = DashScopeChatConfig() + + for api_base in ( + WORKSPACE, + f"{WORKSPACE}/compatible-mode/v1", + f"{WORKSPACE}/apps/anthropic", + f"{WORKSPACE}/api/v1", + ): + url: Final = config.get_complete_url( + api_base=api_base, + api_key="sk-test", + model="qwen3.7-plus", + optional_params={}, + litellm_params={}, + ) + + assert url == f"{WORKSPACE}/compatible-mode/v1/chat/completions" + + +def test_get_complete_url_keeps_unrecognized_hosts(): + url: Final = DashScopeChatConfig().get_complete_url( + api_base="https://gateway.example.com/v1", + api_key="sk-test", + model="qwen3.7-plus", + optional_params={}, + litellm_params={}, + ) + + assert url == "https://gateway.example.com/v1/chat/completions" + class TestDashScopeConfig: """Test class for DashScope functionality""" diff --git a/tests/unit/llms/dashscope/test_dashscope_common_utils.py b/tests/unit/llms/dashscope/test_dashscope_common_utils.py new file mode 100644 index 00000000000..2dc4d5e5a85 --- /dev/null +++ b/tests/unit/llms/dashscope/test_dashscope_common_utils.py @@ -0,0 +1,171 @@ +""" +Unit tests for DashScope's protocol-specific base URL derivation. +""" + +from typing import Final + +import pytest + +from litellm.llms.dashscope.common_utils import ( + DASHSCOPE_DEFAULT_ORIGIN, + resolve_dashscope_anthropic_api_base, + resolve_dashscope_endpoints, + resolve_dashscope_family_rerank_api_base, + resolve_dashscope_openai_api_base, +) + +WORKSPACE: Final = "https://llm-abc123.cn-beijing.maas.aliyuncs.com" + + +@pytest.mark.parametrize( + "api_base", + [ + WORKSPACE, + f"{WORKSPACE}/", + f"{WORKSPACE}/compatible-mode/v1", + f"{WORKSPACE}/compatible-mode/v1/chat/completions", + f"{WORKSPACE}/apps/anthropic", + f"{WORKSPACE}/apps/anthropic/v1/messages", + f"{WORKSPACE}/api/v1", + f"{WORKSPACE}/api/v1/services/aigc/video-generation/video-synthesis", + ], +) +def test_workspace_base_derives_every_protocol(api_base: str): + endpoints: Final = resolve_dashscope_endpoints(api_base) + + assert endpoints is not None + assert endpoints.origin == WORKSPACE + assert endpoints.openai == f"{WORKSPACE}/compatible-mode/v1" + assert endpoints.anthropic == f"{WORKSPACE}/apps/anthropic" + assert endpoints.native == f"{WORKSPACE}/api/v1" + + +@pytest.mark.parametrize( + "origin", + [ + "https://dashscope.aliyuncs.com", + "https://dashscope-intl.aliyuncs.com", + "https://dashscope-us.aliyuncs.com", + ], +) +@pytest.mark.parametrize("path", ["", "/", "/compatible-mode/v1"]) +def test_standard_hosts_derive_every_protocol(origin: str, path: str): + endpoints: Final = resolve_dashscope_endpoints(f"{origin}{path}") + + assert endpoints is not None + assert endpoints.openai == f"{origin}/compatible-mode/v1" + assert endpoints.anthropic == f"{origin}/apps/anthropic" + assert endpoints.native == f"{origin}/api/v1" + + +@pytest.mark.parametrize( + "api_base", + [ + None, + "", + "not-a-url", + "https://gateway.example.com", + "https://gateway.example.com/compatible-mode/v1", + f"{WORKSPACE}/custom-path", + f"{WORKSPACE}/compatible-mode/v1?api-version=1", + "https://trial.cn-beijing.maas.aliyuncs.com", + "https://token-plan.cn-beijing.maas.aliyuncs.com", + ], +) +def test_unrecognized_bases_are_not_rewritten(api_base: str | None): + assert resolve_dashscope_endpoints(api_base) is None + + +def test_openai_base_derives_from_the_domain(monkeypatch: pytest.MonkeyPatch): + monkeypatch.delenv("DASHSCOPE_API_BASE", raising=False) + + assert resolve_dashscope_openai_api_base(WORKSPACE) == f"{WORKSPACE}/compatible-mode/v1" + assert resolve_dashscope_openai_api_base(f"{WORKSPACE}/apps/anthropic") == f"{WORKSPACE}/compatible-mode/v1" + assert ( + resolve_dashscope_openai_api_base(f"{WORKSPACE}/compatible-api/v1/reranks") == f"{WORKSPACE}/compatible-mode/v1" + ) + + +@pytest.mark.parametrize( + "endpoint_url", + [ + "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation", + f"{WORKSPACE}/api/v1/services/aigc/video-generation/video-synthesis", + ], +) +def test_openai_base_keeps_a_native_endpoint_url(monkeypatch: pytest.MonkeyPatch, endpoint_url: str): + monkeypatch.setenv("DASHSCOPE_API_BASE", endpoint_url) + + assert resolve_dashscope_openai_api_base(None) == endpoint_url + assert resolve_dashscope_openai_api_base(endpoint_url) == endpoint_url + assert resolve_dashscope_openai_api_base(f"{WORKSPACE}/api/v1") == f"{WORKSPACE}/compatible-mode/v1" + + +def test_openai_base_keeps_unrecognized_hosts(monkeypatch: pytest.MonkeyPatch): + monkeypatch.delenv("DASHSCOPE_API_BASE", raising=False) + + assert resolve_dashscope_openai_api_base("https://gateway.example.com/v1") == "https://gateway.example.com/v1" + + +def test_openai_base_defaults_to_dashscope(monkeypatch: pytest.MonkeyPatch): + monkeypatch.delenv("DASHSCOPE_API_BASE", raising=False) + + assert resolve_dashscope_openai_api_base(None) == f"{DASHSCOPE_DEFAULT_ORIGIN}/compatible-mode/v1" + + +def test_anthropic_base_derives_from_the_domain(monkeypatch: pytest.MonkeyPatch): + monkeypatch.delenv("DASHSCOPE_API_BASE", raising=False) + + assert resolve_dashscope_anthropic_api_base(WORKSPACE) == f"{WORKSPACE}/apps/anthropic" + assert resolve_dashscope_anthropic_api_base(f"{WORKSPACE}/compatible-mode/v1") == f"{WORKSPACE}/apps/anthropic" + + +def test_anthropic_base_keeps_unrecognized_hosts(monkeypatch: pytest.MonkeyPatch): + monkeypatch.delenv("DASHSCOPE_API_BASE", raising=False) + + assert ( + resolve_dashscope_anthropic_api_base("https://gateway.example.com/anthropic") + == "https://gateway.example.com/anthropic" + ) + + +def test_rerank_base_derives_from_the_domain(monkeypatch: pytest.MonkeyPatch): + monkeypatch.delenv("DASHSCOPE_API_BASE_RERANK", raising=False) + + assert ( + resolve_dashscope_family_rerank_api_base(WORKSPACE, "DASHSCOPE_API_BASE_RERANK", "https://default/reranks") + == f"{WORKSPACE}/compatible-api/v1/reranks" + ) + + +def test_rerank_base_keeps_unrecognized_hosts(): + gateway: Final = "https://gateway.example.com/v1" + + assert ( + resolve_dashscope_family_rerank_api_base(gateway, "DASHSCOPE_API_BASE_RERANK", "https://default/reranks") + == gateway + ) + + +def test_rerank_base_uses_the_configured_default(monkeypatch: pytest.MonkeyPatch): + monkeypatch.delenv("DASHSCOPE_API_BASE_RERANK", raising=False) + + assert ( + resolve_dashscope_family_rerank_api_base(None, "DASHSCOPE_API_BASE_RERANK", "https://default/reranks") + == "https://default/reranks" + ) + + +@pytest.mark.parametrize( + "origin", + ["https://cn-hongkong.dashscope.aliyuncs.com", "https://token-plan.cn-beijing.maas.aliyuncs.com"], +) +def test_rerank_keeps_remapping_chat_bases_on_other_aliyun_hosts(monkeypatch: pytest.MonkeyPatch, origin: str): + monkeypatch.delenv("DASHSCOPE_API_BASE_RERANK", raising=False) + + assert ( + resolve_dashscope_family_rerank_api_base( + f"{origin}/compatible-mode/v1", "DASHSCOPE_API_BASE_RERANK", "https://default/reranks" + ) + == f"{origin}/compatible-api/v1/reranks" + ) diff --git a/tests/unit/llms/dashscope/test_dashscope_embedding_transformation.py b/tests/unit/llms/dashscope/test_dashscope_embedding_transformation.py index 1b6eea0e4c8..27afbee3173 100644 --- a/tests/unit/llms/dashscope/test_dashscope_embedding_transformation.py +++ b/tests/unit/llms/dashscope/test_dashscope_embedding_transformation.py @@ -3,6 +3,7 @@ Unit tests for DashScope embedding transformation. """ import json +from typing import Final from unittest.mock import MagicMock import httpx @@ -16,6 +17,40 @@ from litellm.llms.dashscope.embed.transformation import ( ) from litellm.types.utils import EmbeddingResponse +WORKSPACE: Final = "https://llm-abc123.cn-beijing.maas.aliyuncs.com" + + +def test_get_complete_url_derives_the_compatible_base_from_any_standard_base(): + config: Final = DashScopeEmbeddingConfig() + + for api_base in ( + WORKSPACE, + f"{WORKSPACE}/compatible-mode/v1", + f"{WORKSPACE}/apps/anthropic", + f"{WORKSPACE}/api/v1", + ): + url: Final = config.get_complete_url( + api_base=api_base, + api_key="sk-test", + model="text-embedding-v4", + optional_params={}, + litellm_params={}, + ) + + assert url == f"{WORKSPACE}/compatible-mode/v1/embeddings" + + +def test_get_complete_url_keeps_unrecognized_hosts(): + url: Final = DashScopeEmbeddingConfig().get_complete_url( + api_base="https://gateway.example.com/v1", + api_key="sk-test", + model="text-embedding-v4", + optional_params={}, + litellm_params={}, + ) + + assert url == "https://gateway.example.com/v1/embeddings" + def test_validate_environment_and_url(): config = DashScopeEmbeddingConfig() diff --git a/tests/unit/llms/dashscope/test_dashscope_messages_transformation.py b/tests/unit/llms/dashscope/test_dashscope_messages_transformation.py new file mode 100644 index 00000000000..decc12afd87 --- /dev/null +++ b/tests/unit/llms/dashscope/test_dashscope_messages_transformation.py @@ -0,0 +1,215 @@ +""" +Unit tests for DashScope's Anthropic-compatible Messages endpoint. +""" + +from typing import Final + +import pytest +import respx + +import litellm +from litellm.exceptions import AuthenticationError +from litellm.llms.dashscope.messages.transformation import ( + DashScopeAnthropicMessagesConfig, +) + +WORKSPACE: Final = "https://llm-abc123.cn-beijing.maas.aliyuncs.com" +INTL: Final = "https://dashscope-intl.aliyuncs.com" +CODING_PLAN: Final = "https://coding-intl.dashscope.aliyuncs.com" +CHAT_COMPLETION: Final = { + "id": "chatcmpl-1", + "object": "chat.completion", + "created": 1792227137, + "model": "qwen3.7-plus", + "choices": [{"index": 0, "message": {"role": "assistant", "content": "ok"}, "finish_reason": "stop"}], + "usage": {"prompt_tokens": 9, "completion_tokens": 1, "total_tokens": 10}, +} +MESSAGE: Final = { + "id": "msg_1", + "type": "message", + "role": "assistant", + "model": "qwen3.7-plus", + "content": [{"type": "text", "text": "ok"}], + "stop_reason": "end_turn", + "stop_sequence": None, + "usage": {"input_tokens": 9, "output_tokens": 1}, +} + + +@pytest.mark.parametrize( + ("api_base", "expected"), + [ + ( + None, + "https://dashscope.aliyuncs.com/apps/anthropic/v1/messages", + ), + ( + "https://dashscope.aliyuncs.com", + "https://dashscope.aliyuncs.com/apps/anthropic/v1/messages", + ), + ( + "https://dashscope.aliyuncs.com/compatible-mode/v1", + "https://dashscope.aliyuncs.com/apps/anthropic/v1/messages", + ), + ( + "https://dashscope.aliyuncs.com/apps/anthropic", + "https://dashscope.aliyuncs.com/apps/anthropic/v1/messages", + ), + ( + "https://dashscope.aliyuncs.com/apps/anthropic/v1/messages", + "https://dashscope.aliyuncs.com/apps/anthropic/v1/messages", + ), + ( + WORKSPACE, + f"{WORKSPACE}/apps/anthropic/v1/messages", + ), + ( + f"{WORKSPACE}/compatible-mode/v1", + f"{WORKSPACE}/apps/anthropic/v1/messages", + ), + ( + "https://gateway.example.com/anthropic", + "https://gateway.example.com/anthropic/v1/messages", + ), + ], +) +def test_get_complete_url(api_base: str | None, expected: str): + config: Final = DashScopeAnthropicMessagesConfig() + + url: Final = config.get_complete_url( + api_base=api_base, + api_key="sk-test", + model="qwen3.7-plus", + optional_params={}, + litellm_params={}, + ) + + assert url == expected + + +@pytest.mark.parametrize("dashscope_api_key", [None, " "]) +def test_missing_dashscope_key_does_not_fall_back_to_anthropic( + monkeypatch: pytest.MonkeyPatch, dashscope_api_key: str | None +): + monkeypatch.delenv("DASHSCOPE_API_KEY", raising=False) + if dashscope_api_key is not None: + monkeypatch.setenv("DASHSCOPE_API_KEY", dashscope_api_key) + monkeypatch.delenv("ANTHROPIC_AUTH_TOKEN", raising=False) + monkeypatch.setenv("ANTHROPIC_API_KEY", "sk-ant-secret") + monkeypatch.setattr(litellm, "api_key", None) + headers: dict[str, str] = {} + + with pytest.raises(AuthenticationError) as error: + DashScopeAnthropicMessagesConfig().validate_anthropic_messages_environment( + headers=headers, + model="qwen3.7-plus", + messages=[], + optional_params={}, + litellm_params={}, + api_key=None, + api_base=WORKSPACE, + ) + + assert "DashScope" in str(error.value) + assert "sk-ant-secret" not in str(error.value) + assert {name.lower() for name in headers}.isdisjoint({"x-api-key", "authorization"}) + + +def test_caller_auth_header_is_honored_without_a_dashscope_key(monkeypatch: pytest.MonkeyPatch): + monkeypatch.delenv("DASHSCOPE_API_KEY", raising=False) + monkeypatch.setattr(litellm, "api_key", None) + headers: Final = {"x-api-key": "caller-key"} + + resolved_headers, _ = DashScopeAnthropicMessagesConfig().validate_anthropic_messages_environment( + headers=headers, + model="qwen3.7-plus", + messages=[], + optional_params={}, + litellm_params={}, + api_key=None, + api_base=WORKSPACE, + ) + + assert resolved_headers["x-api-key"] == "caller-key" + + +def test_caller_authorization_header_is_honored_without_a_dashscope_key(monkeypatch: pytest.MonkeyPatch): + monkeypatch.delenv("DASHSCOPE_API_KEY", raising=False) + monkeypatch.setattr(litellm, "api_key", None) + headers: Final = {"Authorization": "Bearer caller-token"} + + resolved_headers, _ = DashScopeAnthropicMessagesConfig().validate_anthropic_messages_environment( + headers=headers, + model="qwen3.7-plus", + messages=[], + optional_params={}, + litellm_params={}, + api_key=None, + api_base=WORKSPACE, + ) + + assert resolved_headers["Authorization"] == "Bearer caller-token" + + +def test_dashscope_key_wins_over_an_anthropic_environment_key(monkeypatch: pytest.MonkeyPatch): + monkeypatch.setenv("DASHSCOPE_API_KEY", "sk-dashscope") + monkeypatch.setenv("ANTHROPIC_API_KEY", "sk-ant-secret") + + resolved_headers, _ = DashScopeAnthropicMessagesConfig().validate_anthropic_messages_environment( + headers={}, + model="qwen3.7-plus", + messages=[], + optional_params={}, + litellm_params={}, + api_key=None, + api_base=WORKSPACE, + ) + + header_values: Final = " ".join(resolved_headers.values()) + assert "sk-dashscope" in header_values + assert "sk-ant-secret" not in header_values + + +def test_dashscope_prefix_reaches_the_provider(): + model, provider, _, _ = litellm.get_llm_provider( + "dashscope/qwen3.7-plus", + api_base=WORKSPACE, + ) + + assert provider == "dashscope" + assert model == "qwen3.7-plus" + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + ("api_base", "expected_url"), + [ + (INTL, f"{INTL}/apps/anthropic/v1/messages"), + (f"{INTL}/apps/anthropic", f"{INTL}/apps/anthropic/v1/messages"), + (WORKSPACE, f"{WORKSPACE}/apps/anthropic/v1/messages"), + (f"{CODING_PLAN}/apps/anthropic", f"{CODING_PLAN}/apps/anthropic/v1/messages"), + (None, "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions"), + (f"{INTL}/compatible-mode/v1", f"{INTL}/compatible-mode/v1/chat/completions"), + (f"{CODING_PLAN}/v1", f"{CODING_PLAN}/v1/chat/completions"), + ("https://gateway.example.com/anthropic", "https://gateway.example.com/anthropic/chat/completions"), + ], +) +async def test_only_a_domain_root_or_anthropic_base_leaves_the_chat_bridge( + monkeypatch: pytest.MonkeyPatch, api_base: str | None, expected_url: str +): + monkeypatch.delenv("DASHSCOPE_API_BASE", raising=False) + monkeypatch.setattr(litellm, "disable_aiohttp_transport", True) + + with respx.mock(assert_all_called=False) as upstream: + upstream.route(path__regex=r".*/chat/completions$").respond(200, json=CHAT_COMPLETION) + upstream.route(path__regex=r".*/v1/messages$").respond(200, json=MESSAGE) + + await litellm.anthropic.messages.acreate( + model="dashscope/qwen3.7-plus", + messages=[{"role": "user", "content": "say ok"}], + max_tokens=16, + api_key="sk-dashscope", + api_base=api_base, + ) + + assert [str(call.request.url) for call in upstream.calls] == [expected_url] diff --git a/tests/unit/llms/dashscope/test_dashscope_rerank_transformation.py b/tests/unit/llms/dashscope/test_dashscope_rerank_transformation.py index a913c79af31..f0dbdb74b68 100644 --- a/tests/unit/llms/dashscope/test_dashscope_rerank_transformation.py +++ b/tests/unit/llms/dashscope/test_dashscope_rerank_transformation.py @@ -3,12 +3,15 @@ Unit tests for DashScope rerank transformation. """ import json +from typing import Final from unittest.mock import MagicMock import httpx import pytest +import respx from pydantic import ValidationError +import litellm from litellm.llms.dashscope.common_utils import DashScopeError from litellm.llms.dashscope.rerank.transformation import ( @@ -26,6 +29,21 @@ class TestDashScopeRerankURL: url = self.config.get_complete_url(api_base=None, model="qwen3-rerank") assert url == DEFAULT_RERANK_URL + def test_domain_root_remaps_to_rerank_route(self): + url: Final = self.config.get_complete_url( + api_base="https://llm-abc123.cn-beijing.maas.aliyuncs.com", + model="qwen3-rerank", + ) + assert url == "https://llm-abc123.cn-beijing.maas.aliyuncs.com/compatible-api/v1/reranks" + + def test_explicit_rerank_url_wins_over_the_rerank_env(self, monkeypatch: pytest.MonkeyPatch): + monkeypatch.setenv("DASHSCOPE_API_BASE_RERANK", DEFAULT_RERANK_URL) + explicit_url: Final = "https://dashscope-intl.aliyuncs.com/compatible-api/v1/reranks" + + url: Final = self.config.get_complete_url(api_base=explicit_url, model="qwen3-rerank") + + assert url == explicit_url + def test_chat_shaped_base_remaps_to_rerank_route(self): url = self.config.get_complete_url( api_base="https://dashscope.aliyuncs.com/compatible-mode/v1", @@ -434,3 +452,37 @@ def test_transform_rerank_response_shape_errors_do_not_echo_the_payload(): _transform({"results": [["leaked document text"]]}) assert "leaked document text" not in str(exc_info.value) + + +@pytest.mark.parametrize( + ("provider", "rerank_env"), + [ + ("dashscope", "DASHSCOPE_API_BASE_RERANK"), + ("qwencloud", "QWENCLOUD_API_BASE_RERANK"), + ("qwen_ai_platform", "QWEN_AI_PLATFORM_API_BASE_RERANK"), + ], +) +@pytest.mark.parametrize( + "api_base", + [ + "https://dashscope-intl.aliyuncs.com/compatible-api/v1/reranks", + "https://dashscope-intl.aliyuncs.com/compatible-api/v1", + "https://dashscope-intl.aliyuncs.com", + "https://dashscope-intl.aliyuncs.com/api/v1", + ], +) +def test_rerank_sends_an_explicit_base_over_the_rerank_env( + monkeypatch: pytest.MonkeyPatch, provider: str, rerank_env: str, api_base: str +): + monkeypatch.setenv("DASHSCOPE_API_BASE", "https://dashscope-intl.aliyuncs.com") + monkeypatch.setenv(rerank_env, DEFAULT_RERANK_URL) + + with respx.mock(assert_all_called=True) as upstream: + upstream.post("https://dashscope-intl.aliyuncs.com/compatible-api/v1/reranks").respond( + 200, json={"id": "r1", "results": [{"index": 0, "relevance_score": 0.9}], "usage": {"total_tokens": 5}} + ) + response: Final = litellm.rerank( + model=f"{provider}/qwen3-rerank", query="q", documents=["a"], api_base=api_base, api_key="sk-test" + ) + + assert response.results == [{"index": 0, "relevance_score": 0.9}] diff --git a/tests/unit/llms/dashscope/test_dashscope_video_transformation.py b/tests/unit/llms/dashscope/test_dashscope_video_transformation.py index 15b77aaee3d..c1b4157c8ab 100644 --- a/tests/unit/llms/dashscope/test_dashscope_video_transformation.py +++ b/tests/unit/llms/dashscope/test_dashscope_video_transformation.py @@ -1119,6 +1119,20 @@ class TestDashScopeVideoEnvironment: assert url == "https://ws-123.cn-beijing.maas.aliyuncs.com" + def test_every_protocol_base_normalizes_to_the_host(self): + workspace: Final = "https://ws-123.cn-beijing.maas.aliyuncs.com" + + for api_base in ( + f"{workspace}/compatible-mode/v1", + f"{workspace}/apps/anthropic", + f"{workspace}/api/v1", + ): + url: Final = DashScopeVideoConfig().get_complete_url( + model="wan3.0-video", api_base=api_base, litellm_params={} + ) + + assert url == workspace + def test_default_host_is_beijing(self, monkeypatch): monkeypatch.delenv("DASHSCOPE_API_BASE_VIDEO", raising=False) monkeypatch.delenv("DASHSCOPE_API_BASE", raising=False) diff --git a/tests/unit/llms/dashscope/test_qwen_brand_aliases.py b/tests/unit/llms/dashscope/test_qwen_brand_aliases.py index 69ae66d3818..43ef8f90b39 100644 --- a/tests/unit/llms/dashscope/test_qwen_brand_aliases.py +++ b/tests/unit/llms/dashscope/test_qwen_brand_aliases.py @@ -2,8 +2,10 @@ import json import math import re from pathlib import Path +from typing import Final, Literal import pytest +from typing_extensions import ReadOnly, TypedDict import litellm from litellm import completion, get_llm_provider @@ -53,6 +55,23 @@ DASHSCOPE_FAMILY_ENV_VARS = [ "QWEN_AI_PLATFORM_API_BASE_IMAGE", ] + + +class BrandCase(TypedDict): + provider: ReadOnly[str] + display_name: ReadOnly[str] + enum: ReadOnly[LlmProviders] + key_env: ReadOnly[str] + base_env: ReadOnly[str] + default_base: ReadOnly[str] + default_rerank_base: ReadOnly[str] + default_image_base: ReadOnly[str] + chat_config: ReadOnly[type[DashScopeChatConfig]] + embedding_config: ReadOnly[type[DashScopeEmbeddingConfig]] + rerank_config: ReadOnly[type[DashScopeRerankConfig]] + image_config: ReadOnly[type[DashScopeImageGenerationConfig]] + + BRAND_CASES = [ pytest.param( { @@ -211,6 +230,20 @@ class TestQwenBrandDefaultUrls: ) assert url == f"{brand['default_base']}/embeddings" + @pytest.mark.parametrize("brand", BRAND_CASES) + @pytest.mark.parametrize(("config_key", "route"), [("chat_config", "chat/completions"), ("embedding_config", "embeddings")]) + def test_openai_routes_derive_from_a_domain_root( + self, brand: BrandCase, config_key: Literal["chat_config", "embedding_config"], route: str + ) -> None: + url: Final = brand[config_key]().get_complete_url( + api_base="https://dashscope-intl.aliyuncs.com", + api_key="sk-test", + model="qwen-max", + optional_params={}, + litellm_params={}, + ) + assert url == f"https://dashscope-intl.aliyuncs.com/compatible-mode/v1/{route}" + @pytest.mark.parametrize("brand", BRAND_CASES) def test_rerank_complete_url(self, brand): url = brand["rerank_config"]().get_complete_url(api_base=None, model="gte-rerank-v2") @@ -249,6 +282,19 @@ class TestQwenBrandDefaultUrls: ) assert url == brand["default_image_base"] + @pytest.mark.parametrize("brand", BRAND_CASES) + def test_image_generation_uses_its_own_configured_base(self, monkeypatch: pytest.MonkeyPatch, brand: BrandCase) -> None: + monkeypatch.setenv("DASHSCOPE_API_BASE", "https://legacy.example.com/images") + monkeypatch.setenv(brand["base_env"], "https://gateway.example.com/qwen/images") + url: Final = brand["image_config"]().get_complete_url( + api_base=None, + api_key="sk-test", + model="qwen-image", + optional_params={}, + litellm_params={}, + ) + assert url == "https://gateway.example.com/qwen/images" + @pytest.mark.parametrize("brand", BRAND_CASES) def test_validate_environment_requires_key(self, brand): with pytest.raises(ValueError, match="DASHSCOPE_API_KEY"): diff --git a/tests/unit/llms/databricks/chat/test_databricks_chat_transformation.py b/tests/unit/llms/databricks/chat/test_databricks_chat_transformation.py index eb2e1d8e347..19234767b19 100644 --- a/tests/unit/llms/databricks/chat/test_databricks_chat_transformation.py +++ b/tests/unit/llms/databricks/chat/test_databricks_chat_transformation.py @@ -2465,6 +2465,41 @@ def test_databricks_claude_drops_the_openai_tool_search_tool_under_drop_params(m ] +@pytest.mark.parametrize("drop_params", [True, False]) +def test_databricks_chat_drops_beta_gated_thinking_fields_only_under_drop_params( + drop_params: bool, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setattr(litellm, "drop_params", False) + thinking: Final = { + "type": "adaptive", + "block_binding": {"prefix_mismatch_behavior": "error"}, + "display": "updates", + } + mapped: Final = DatabricksConfig().map_openai_params( + non_default_params={"thinking": thinking}, + optional_params={}, + model="databricks-claude-opus-5-5", + drop_params=drop_params, + ) + assert mapped["thinking"] == ({"type": "adaptive"} if drop_params else thinking) + assert thinking == { + "type": "adaptive", + "block_binding": {"prefix_mismatch_behavior": "error"}, + "display": "updates", + } + + +def test_databricks_chat_keeps_ungated_thinking_display_under_drop_params(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(litellm, "drop_params", False) + mapped: Final = DatabricksConfig().map_openai_params( + non_default_params={"thinking": {"type": "adaptive", "display": "summarized"}}, + optional_params={}, + model="databricks-claude-opus-5-5", + drop_params=True, + ) + assert mapped["thinking"] == {"type": "adaptive", "display": "summarized"} + + _JSON_TOOL_ARGUMENT_FRAGMENTS: Final = ("", "", '{"name": ', '"Ada"', ', "met', 'a": ', "{}", ', "tags": []}') _RECORD_RESPONSE_FORMAT: Final = { "type": "json_schema", diff --git a/tests/unit/llms/databricks/messages/test_databricks_messages_transformation.py b/tests/unit/llms/databricks/messages/test_databricks_messages_transformation.py index 3c5f4fd72f2..ae54d0e7437 100644 --- a/tests/unit/llms/databricks/messages/test_databricks_messages_transformation.py +++ b/tests/unit/llms/databricks/messages/test_databricks_messages_transformation.py @@ -281,7 +281,7 @@ SERVER_TOOLS: Final = [ ] -async def _workspace_request_body(**request_params: object) -> dict[str, object]: +async def _workspace_request(max_tokens: int = 64, **request_params: object) -> httpx.Request: received: Final[list[httpx.Request]] = [] # mutable-ok: the fake workspace records what litellm sent def workspace_answers(request: httpx.Request) -> httpx.Response: @@ -303,7 +303,7 @@ async def _workspace_request_body(**request_params: object) -> dict[str, object] upstream: Final = AsyncHTTPHandler(transport=httpx.MockTransport(workspace_answers)) try: await litellm.anthropic.messages.acreate( - max_tokens=64, + max_tokens=max_tokens, messages=USER_TURN, model=f"databricks/{CLAUDE_MODEL}", api_key=DATABRICKS_API_KEY, @@ -314,7 +314,30 @@ async def _workspace_request_body(**request_params: object) -> dict[str, object] finally: await upstream.close() assert len(received) == 1 - return json.loads(received[0].content) + return received[0] + + +async def _workspace_request_body(**request_params: object) -> dict[str, object]: + return json.loads((await _workspace_request(**request_params)).content) + + +@pytest.mark.asyncio +@pytest.mark.parametrize("drop_params", [True, False]) +async def test_bound_thinking_with_display_updates_reaches_the_workspace_with_both_betas( + drop_params: bool, monkeypatch: pytest.MonkeyPatch, local_beta_headers_config: None +) -> None: + monkeypatch.setattr(litellm, "drop_params", False) + thinking: Final = { + "type": "enabled", + "budget_tokens": 1024, + "block_binding": {"prefix_mismatch_behavior": "error"}, + "display": "updates", + } + request: Final = await _workspace_request(max_tokens=4096, thinking=thinking, drop_params=drop_params) + assert json.loads(request.content)["thinking"] == thinking + assert {"thinking-binding-controls-2026-08-01", "thinking-display-updates-2026-08-18"} <= set( + request.headers["anthropic-beta"].split(",") + ) @pytest.mark.asyncio diff --git a/tests/unit/llms/edenai/conftest.py b/tests/unit/llms/edenai/conftest.py index 5ca5728354c..e11a515e953 100644 --- a/tests/unit/llms/edenai/conftest.py +++ b/tests/unit/llms/edenai/conftest.py @@ -2,7 +2,6 @@ import asyncio import uuid import pytest -import pytest_asyncio import litellm from litellm.integrations.custom_logger import CustomLogger @@ -40,9 +39,8 @@ class SpendCapture(CustomLogger): await asyncio.wait_for(GLOBAL_LOGGING_WORKER.flush(), timeout=10.0) -@pytest_asyncio.fixture -async def spend_capture(monkeypatch) -> SpendCapture: - GLOBAL_LOGGING_WORKER.start() # rebinds the worker's queue to this test's event loop +@pytest.fixture +def spend_capture(monkeypatch) -> SpendCapture: capture = SpendCapture(call_id=f"eden-{uuid.uuid4()}") monkeypatch.setattr(litellm, "callbacks", [capture]) return capture diff --git a/tests/unit/llms/gigachat/chat/test_gigachat_chat_transformation.py b/tests/unit/llms/gigachat/chat/test_gigachat_chat_transformation.py index f2224e84ff3..115d81bfb90 100644 --- a/tests/unit/llms/gigachat/chat/test_gigachat_chat_transformation.py +++ b/tests/unit/llms/gigachat/chat/test_gigachat_chat_transformation.py @@ -8,6 +8,7 @@ get_model_response_iterator, and get_error_class. """ import json +from typing import Final from unittest.mock import MagicMock, patch import httpx @@ -512,6 +513,20 @@ class TestTransformMessages: assert result[0]["content"] == "look" assert "file-456" in result[0]["attachments"] + def test_list_content_with_pdf_file_part_uploads_it_as_an_attachment(self) -> None: + pdf_data_url: Final = "data:application/pdf;base64,JVBERi0xLjQK" + content: Final = [ + {"type": "text", "text": "summarize"}, + {"type": "file", "file": {"filename": "report.pdf", "file_data": pdf_data_url}}, + ] + with patch.object(self.config, "_upload_image", return_value="file-789") as upload: + result: Final = self.config._transform_messages([ + {"role": "user", "content": content} + ]) + upload.assert_called_once_with(pdf_data_url) + assert result[0]["content"] == "summarize" + assert result[0]["attachments"] == ["file-789"] + class TestTransformRequest: def setup_method(self): diff --git a/tests/unit/llms/oci/chat/test_oci_chat_transformation.py b/tests/unit/llms/oci/chat/test_oci_chat_transformation.py index 8c96bd91049..0535279d177 100644 --- a/tests/unit/llms/oci/chat/test_oci_chat_transformation.py +++ b/tests/unit/llms/oci/chat/test_oci_chat_transformation.py @@ -1,4 +1,5 @@ import datetime +from typing import Final import httpx import pytest import json @@ -2164,3 +2165,32 @@ class TestOpenAIPrefixMaxTokensKey: chat_request = _oci_chat_request("openai.gpt-5", {"max_tokens": 64}) assert chat_request["maxCompletionTokens"] == 64 assert "maxTokens" not in chat_request + + +def test_transform_request_sends_a_pdf_file_part_as_an_image_data_url() -> None: + pdf_data_url: Final = "data:application/pdf;base64,JVBERi0xLjQK" + transformed_request: Final = OCIChatConfig().transform_request( + model="google.gemini-2.5-flash", + messages=[ + { + "role": "user", + "content": [ + {"type": "text", "text": "Summarize this."}, + {"type": "file", "file": {"filename": "report.pdf", "file_data": pdf_data_url}}, + ], + } + ], + optional_params={"oci_compartment_id": TEST_COMPARTMENT_ID}, + litellm_params={}, + headers={}, + ) + + assert transformed_request["chatRequest"]["messages"] == [ + { + "role": "USER", + "content": [ + {"type": "TEXT", "text": "Summarize this."}, + {"type": "IMAGE", "imageUrl": {"url": pdf_data_url}}, + ], + } + ] diff --git a/tests/unit/llms/openai/completion/test_text_completion_guardrail_handler.py b/tests/unit/llms/openai/completion/test_text_completion_guardrail_handler.py index c96fbf34fe1..9beb1faf7f4 100644 --- a/tests/unit/llms/openai/completion/test_text_completion_guardrail_handler.py +++ b/tests/unit/llms/openai/completion/test_text_completion_guardrail_handler.py @@ -2,18 +2,25 @@ Unit tests for OpenAI Text Completion Guardrail Translation Handler """ -from typing import List, Optional, Tuple +import json +from typing import List, Literal, Optional, Tuple from unittest.mock import MagicMock import pytest +from fastapi import HTTPException from litellm.integrations.custom_guardrail import CustomGuardrail +from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj from litellm.llms import get_guardrail_translation_mapping from litellm.llms.openai.completion.guardrail_translation.handler import ( OpenAITextCompletionHandler, ) -from litellm.types.utils import CallTypes, TextChoices, TextCompletionResponse +from litellm.proxy._types import UserAPIKeyAuth +from litellm.proxy.guardrails.guardrail_hooks.litellm_content_filter.content_filter import ContentFilterGuardrail +from litellm.proxy.guardrails.guardrail_hooks.unified_guardrail.unified_guardrail import UnifiedLLMGuardrails +from litellm.types.guardrails import BlockedWord, ContentFilterAction, GuardrailEventHooks +from litellm.types.utils import CallTypes, GenericGuardrailAPIInputs, TextChoices, TextCompletionResponse, Usage class MockGuardrail(CustomGuardrail): @@ -341,3 +348,215 @@ class TestPIIMaskingScenario: assert "[EMAIL_REDACTED]" in result["prompt"][1] assert "alice@example.com" not in result["prompt"][0] assert "bob@test.org" not in result["prompt"][1] + + +class _RecordingKeywordFilter(ContentFilterGuardrail): + def __init__(self): + super().__init__( + guardrail_name="keyword-filter", + blocked_words=[ + BlockedWord(keyword="persimmon", action=ContentFilterAction.MASK), + BlockedWord(keyword="kumquat", action=ContentFilterAction.BLOCK), + ], + event_hook=GuardrailEventHooks.post_call, + default_on=True, + ) + self.streaming_sampling_rate = 1 + self.scanned_texts: tuple[tuple[str, ...], ...] = () + + async def apply_guardrail( + self, + inputs: GenericGuardrailAPIInputs, + request_data: dict, + input_type: Literal["request", "response"], + logging_obj: LiteLLMLoggingObj | None = None, + ) -> GenericGuardrailAPIInputs: + self.scanned_texts = (*self.scanned_texts, tuple(inputs.get("texts") or ())) + return await super().apply_guardrail(inputs, request_data, input_type, logging_obj) + + +class _PassthroughReplaceGuardrail(CustomGuardrail): + async def apply_guardrail( + self, + inputs: GenericGuardrailAPIInputs, + request_data: dict, + input_type: Literal["request", "response"], + logging_obj: LiteLLMLoggingObj | None = None, + ) -> GenericGuardrailAPIInputs: + if any("kumquat" in text for text in inputs.get("texts") or []): + self.raise_passthrough_exception(violation_message="replaced kumquat", request_data=request_data) + return inputs + + +def _text_chunk(index: int | None, text: str | None, finish_reason: str | None = None) -> TextCompletionResponse: + return TextCompletionResponse( + id="cmpl-stream", + created=1589478378, + model="gpt-3.5-turbo-instruct", + object="text_completion", + choices=[TextChoices(index=index, text=text, finish_reason=finish_reason)], + ) + + +def _usage_chunk() -> TextCompletionResponse: + return TextCompletionResponse( + id="cmpl-stream", + created=1589478378, + model="gpt-3.5-turbo-instruct", + object="text_completion", + choices=[], + usage=Usage(prompt_tokens=3, completion_tokens=5, total_tokens=8), + ) + + +def _delivered(chunks: list[TextCompletionResponse]) -> list[tuple[int, str | None, str | None]]: + return [(choice.index, choice.text, choice.finish_reason) for chunk in chunks for choice in chunk.choices] + + +async def _drive_flat_stream(guardrail: CustomGuardrail, chunks: list[TextCompletionResponse]) -> list[object]: + async def upstream(): + for chunk in chunks: + yield chunk + + return [ + item + async for item in UnifiedLLMGuardrails().async_post_call_streaming_iterator_hook( + user_api_key_dict=UserAPIKeyAuth(api_key="test-key", request_route="/v1/completions"), + response=upstream(), + request_data={"guardrail_to_apply": guardrail, "model": "gpt-3.5-turbo-instruct"}, + ) + ] + + +class TestStreamingOutputProcessing: + @pytest.mark.asyncio + async def test_ended_stream_delivers_each_choice_masked_text(self): + guardrail = _RecordingKeywordFilter() + chunks = [ + _text_chunk(0, "A pers"), + _text_chunk(1, "kiwi "), + _text_chunk(0, "im"), + _text_chunk(1, "pie", "stop"), + _text_chunk(0, "mon tart"), + _text_chunk(0, None, "length"), + _usage_chunk(), + ] + + await OpenAITextCompletionHandler().process_output_streaming_response( + responses_so_far=chunks, + guardrail_to_apply=guardrail, + request_data={}, + deliver_ended_stream_rewrites=True, + ) + + assert guardrail.scanned_texts == (("A persimmon tart", "kiwi pie"),) + assert _delivered(chunks) == [ + (0, "A [KEYWORD_REDACTED] tart", None), + (1, "kiwi ", None), + (0, "", None), + (1, "pie", "stop"), + (0, "", None), + (0, None, "length"), + ] + assert chunks[-1].usage.total_tokens == 8 + + @pytest.mark.asyncio + async def test_ended_stream_scanned_without_delivery_keeps_chunks_and_stores_the_assembled_response(self): + chunks = [_text_chunk(0, "A pers"), _text_chunk(0, "immon tart"), _text_chunk(0, "", "stop"), _usage_chunk()] + request_data: dict[str, object] = {} + + await OpenAITextCompletionHandler().process_output_streaming_response( + responses_so_far=chunks, guardrail_to_apply=_RecordingKeywordFilter(), request_data=request_data + ) + + assert _delivered(chunks) == [(0, "A pers", None), (0, "immon tart", None), (0, "", "stop")] + assembled = request_data["response"] + assert isinstance(assembled, TextCompletionResponse) + assert [(c.text, c.finish_reason) for c in assembled.choices] == [("A [KEYWORD_REDACTED] tart", "stop")] + assert assembled.usage.total_tokens == 8 + + @pytest.mark.asyncio + async def test_ended_stream_block_raises_the_guardrail_error(self): + chunks = [_text_chunk(0, "A kum"), _text_chunk(0, "quat tart"), _text_chunk(0, "", "stop")] + + with pytest.raises(HTTPException) as exc_info: + await OpenAITextCompletionHandler().process_output_streaming_response( + responses_so_far=chunks, guardrail_to_apply=_RecordingKeywordFilter(), deliver_ended_stream_rewrites=True + ) + + assert exc_info.value.status_code == 400 + assert "kumquat" in str(exc_info.value.detail) + + @pytest.mark.asyncio + async def test_ended_stream_treats_a_null_choice_index_as_choice_zero(self): + guardrail = _RecordingKeywordFilter() + chunks = [_text_chunk(None, "A pers"), _text_chunk(0, "immon tart"), _text_chunk(0, "", "stop")] + + await OpenAITextCompletionHandler().process_output_streaming_response( + responses_so_far=chunks, guardrail_to_apply=guardrail, request_data={}, deliver_ended_stream_rewrites=True + ) + + assert guardrail.scanned_texts == (("A persimmon tart",),) + assert [choice.text for chunk in chunks for choice in chunk.choices] == ["A [KEYWORD_REDACTED] tart", "", ""] + + @pytest.mark.asyncio + async def test_ended_stream_blocks_a_keyword_split_across_a_null_and_a_zero_choice_index(self): + chunks = [_text_chunk(None, "A kum"), _text_chunk(0, "quat tart"), _text_chunk(0, "", "stop")] + + with pytest.raises(HTTPException) as exc_info: + await OpenAITextCompletionHandler().process_output_streaming_response( + responses_so_far=chunks, guardrail_to_apply=_RecordingKeywordFilter(), deliver_ended_stream_rewrites=True + ) + + assert exc_info.value.status_code == 400 + assert "kumquat" in str(exc_info.value.detail) + + @pytest.mark.asyncio + async def test_flat_stream_is_scanned_once_after_it_ends(self): + guardrail = _RecordingKeywordFilter() + chunks = [_text_chunk(0, "A pers"), _text_chunk(0, "immon tart"), _text_chunk(0, "", "stop"), _usage_chunk()] + + delivered = await _drive_flat_stream(guardrail, chunks) + + assert guardrail.scanned_texts == (("A persimmon tart",),) + assert len(delivered) == len(chunks) + + @pytest.mark.asyncio + async def test_flat_stream_block_ends_with_an_in_band_error_frame(self): + chunks = [_text_chunk(0, "A kum"), _text_chunk(0, "quat tart"), _text_chunk(0, "", "stop"), _usage_chunk()] + + delivered = await _drive_flat_stream(_RecordingKeywordFilter(), chunks) + + error_frame = delivered[-1] + assert isinstance(error_frame, bytes) + assert error_frame.startswith(b'data: {"error": ') + assert b"kumquat" in error_frame + + @pytest.mark.asyncio + async def test_flat_stream_passthrough_block_appends_the_replacement_and_ends_with_content_filter(self): + guardrail = _PassthroughReplaceGuardrail( + guardrail_name="passthrough", event_hook=GuardrailEventHooks.post_call, default_on=True + ) + chunks = [_text_chunk(0, "A kum"), _text_chunk(0, "quat tart"), _text_chunk(0, "", "stop"), _usage_chunk()] + + delivered = await _drive_flat_stream(guardrail, chunks) + + frames = [json.loads(item.decode().removeprefix("data: ")) for item in delivered if isinstance(item, bytes)] + assert [(frame["choices"][0]["text"], frame["choices"][0].get("finish_reason")) for frame in frames] == [ + ("replaced kumquat", None), + ("", "content_filter"), + ] + assert frames[-1]["usage"]["total_tokens"] == 8 + + @pytest.mark.asyncio + async def test_flat_stream_passthrough_block_reports_no_usage_the_upstream_never_sent(self): + guardrail = _PassthroughReplaceGuardrail( + guardrail_name="passthrough", event_hook=GuardrailEventHooks.post_call, default_on=True + ) + chunks = [_text_chunk(0, "A kum"), _text_chunk(0, "quat tart"), _text_chunk(0, "", "stop")] + + delivered = await _drive_flat_stream(guardrail, [chunk.model_copy(update={"usage": None}) for chunk in chunks]) + + frames = [json.loads(item.decode().removeprefix("data: ")) for item in delivered if isinstance(item, bytes)] + assert frames[-1]["choices"][0]["finish_reason"] == "content_filter" + assert [frame for frame in frames if "usage" in frame] == [] diff --git a/tests/unit/llms/openai/responses/test_openai_responses_transformation.py b/tests/unit/llms/openai/responses/test_openai_responses_transformation.py index a185cdaf718..f5a33f87fd0 100644 --- a/tests/unit/llms/openai/responses/test_openai_responses_transformation.py +++ b/tests/unit/llms/openai/responses/test_openai_responses_transformation.py @@ -1,4 +1,5 @@ import json +import logging from collections.abc import Callable, Mapping from types import SimpleNamespace from typing import Final, cast @@ -19,6 +20,7 @@ from litellm.llms.hosted_vllm.responses.transformation import HostedVLLMResponse from litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfig from litellm.llms.xai.responses.transformation import XAIResponsesAPIConfig from litellm.responses.litellm_completion_transformation.transformation import LiteLLMCompletionResponsesConfig +from litellm.responses.utils import ResponsesAPIRequestUtils from litellm.types.llms.openai import ( ImageGenerationPartialImageEvent, IncompleteDetails, @@ -2479,6 +2481,105 @@ class TestResponsesSurfaceSharesTheEffortRule: ) assert mapped["reasoning"] == {"summary": "auto"} + @pytest.mark.parametrize( + ("declared_reasoning_efforts", "suggests_declared_levels"), + [((), True), (None, False)], + ids=["responses route reads model_info levels", "compact route reads none"], + ) + def test_dropping_a_refused_effort_logs_one_warning_naming_only_the_fix_its_route_honors( + self, + local_model_cost_map: None, + caplog: pytest.LogCaptureFixture, + declared_reasoning_efforts: tuple[str, ...] | None, + suggests_declared_levels: bool, + ) -> None: + refused: Final = next( + level + for level in ("none", "minimal", "low") + if litellm.model_cost["gpt-6.1-sol"].get(f"supports_{level}_reasoning_effort") is False + ) + with caplog.at_level(logging.WARNING, logger="LiteLLM"): + mapped: Final = ResponsesAPIRequestUtils.get_optional_params_responses_api( + model="gpt-6.1-sol", + responses_api_provider_config=OpenAIResponsesAPIConfig(), + response_api_optional_params={"reasoning": {"effort": refused}}, + drop_params=True, + declared_reasoning_efforts=declared_reasoning_efforts, + ) + + assert "reasoning" not in mapped + drop_warnings: Final = [record.getMessage() for record in caplog.records if "Dropping" in record.getMessage()] + assert len(drop_warnings) == 1 + assert repr(refused) in drop_warnings[0] and "gpt-6.1-sol" in drop_warnings[0] + assert ("model_info.reasoning_effort_levels" in drop_warnings[0]) is suggests_declared_levels + assert "model_info.base_model" not in drop_warnings[0] + + def test_dropping_reasoning_for_a_model_without_supports_reasoning_logs_one_warning( + self, local_model_cost_map: None, caplog: pytest.LogCaptureFixture + ) -> None: + with caplog.at_level(logging.WARNING, logger="LiteLLM"): + mapped: Final = OpenAIResponsesAPIConfig().map_openai_params( + response_api_optional_params={"reasoning": {"effort": "medium"}}, + model="gpt-4o", + drop_params=True, + ) + + assert "reasoning" not in mapped + drop_warnings: Final = [record.getMessage() for record in caplog.records if "Dropping" in record.getMessage()] + assert len(drop_warnings) == 1 + assert all(fragment in drop_warnings[0] for fragment in ("'medium'", "gpt-4o", "supports_reasoning")) + + def test_turning_reasoning_off_for_a_model_without_supports_reasoning_drops_it_silently( + self, local_model_cost_map: None, caplog: pytest.LogCaptureFixture + ) -> None: + with caplog.at_level(logging.WARNING, logger="LiteLLM"): + mapped: Final = OpenAIResponsesAPIConfig().map_openai_params( + response_api_optional_params={"reasoning": {"effort": "none", "summary": "auto"}}, + model="gpt-4o", + drop_params=True, + ) + + assert "reasoning" not in mapped + assert not [record for record in caplog.records if "Dropping" in record.getMessage()] + + def test_turning_reasoning_off_warns_when_only_custom_pricing_describes_the_model( + self, local_model_cost_map: None, caplog: pytest.LogCaptureFixture + ) -> None: + litellm.register_model( + { + "pricing-only-test-model": { + "litellm_provider": "openai", + "mode": "responses", + "input_cost_per_token": 0.000001, + "output_cost_per_token": 0.000002, + } + } + ) + with caplog.at_level(logging.WARNING, logger="LiteLLM"): + mapped: Final = OpenAIResponsesAPIConfig().map_openai_params( + response_api_optional_params={"reasoning": {"effort": "none"}}, + model="pricing-only-test-model", + drop_params=True, + ) + + assert "reasoning" not in mapped + drop_warnings: Final = [record.getMessage() for record in caplog.records if "Dropping" in record.getMessage()] + assert len(drop_warnings) == 1 + assert all(fragment in drop_warnings[0] for fragment in ("'none'", "pricing-only-test-model")) + + def test_a_forwarded_effort_logs_no_drop_warning( + self, local_model_cost_map: None, caplog: pytest.LogCaptureFixture + ) -> None: + with caplog.at_level(logging.WARNING, logger="LiteLLM"): + mapped: Final = OpenAIResponsesAPIConfig().map_openai_params( + response_api_optional_params={"reasoning": {"effort": "medium"}}, + model="gpt-6.1-sol", + drop_params=True, + ) + + assert mapped["reasoning"] == {"effort": "medium"} + assert not [record for record in caplog.records if "Dropping" in record.getMessage()] + @pytest.mark.parametrize("level", ["none", "minimal", "low", "medium", "high", "xhigh", "max"]) def test_gpt5_3_codex_forwards_exactly_the_effort_levels_its_map_row_allows(self, local_model_cost_map, level): """The gate turns a stale false into a local refusal, so the Responses-only codex row has to diff --git a/tests/unit/llms/openai/test_gpt_reasoning_transformation.py b/tests/unit/llms/openai/test_gpt_reasoning_transformation.py index 4cdc674c774..807a43c5d9d 100644 --- a/tests/unit/llms/openai/test_gpt_reasoning_transformation.py +++ b/tests/unit/llms/openai/test_gpt_reasoning_transformation.py @@ -1,3 +1,4 @@ +import logging import re from collections.abc import Mapping from typing import Final @@ -1589,3 +1590,55 @@ def test_gpt_6_astra_takes_the_reasoning_series_request_shape(): assert "max_tokens" not in params assert params["reasoning_effort"] == "max" assert params["verbosity"] == "low" + + +@pytest.mark.parametrize( + ("custom_llm_provider", "level", "advice"), + [ + ("azure", "none", frozenset(("model_info.reasoning_effort_levels", "model_info.base_model"))), + ("azure", "xhigh", frozenset(("model_info.reasoning_effort_levels", "model_info.base_model"))), + ("openai", "xhigh", frozenset(("model_info.reasoning_effort_levels",))), + ("azure_ai", "xhigh", frozenset(("model_info.reasoning_effort_levels",))), + ("github_copilot", "xhigh", frozenset()), + ], +) +def test_a_reasoning_effort_the_level_gate_drops_logs_a_warning_naming_only_the_fixes_its_route_honors( + caplog: pytest.LogCaptureFixture, custom_llm_provider: str, level: str, advice: frozenset[str] +) -> None: + with caplog.at_level(logging.WARNING, logger="LiteLLM"): + params: Final = litellm.get_optional_params( + model="gpt-5.6-terra-ptu", + custom_llm_provider=custom_llm_provider, + reasoning_effort=level, + drop_params=True, + ) + + assert "reasoning_effort" not in params + drop_warnings: Final = [ + record.getMessage() + for record in caplog.records + if record.levelno == logging.WARNING and "reasoning_effort" in record.getMessage() + ] + assert len(drop_warnings) == 1 + assert repr(level) in drop_warnings[0] and "gpt-5.6-terra-ptu" in drop_warnings[0] + assert ( + frozenset( + fragment + for fragment in ("model_info.reasoning_effort_levels", "model_info.base_model") + if fragment in drop_warnings[0] + ) + == advice + ) + + +def test_a_reasoning_effort_the_level_gate_forwards_logs_no_drop_warning(caplog: pytest.LogCaptureFixture) -> None: + with caplog.at_level(logging.WARNING, logger="LiteLLM"): + params: Final = litellm.get_optional_params( + model="gpt-5.6-terra-ptu", + custom_llm_provider="azure", + reasoning_effort="medium", + drop_params=True, + ) + + assert params["reasoning_effort"] == "medium" + assert not [record for record in caplog.records if "reasoning_effort" in record.getMessage()] diff --git a/tests/unit/llms/openai_like/test_kyma_provider.py b/tests/unit/llms/openai_like/test_kyma_provider.py new file mode 100644 index 00000000000..2b3740ca676 --- /dev/null +++ b/tests/unit/llms/openai_like/test_kyma_provider.py @@ -0,0 +1,465 @@ +import json +import re +from collections.abc import Callable +from datetime import datetime +from itertools import chain +from pathlib import Path +from typing import Final + +import pytest +import respx + +import litellm +from litellm.litellm_core_utils.get_llm_provider_logic import get_llm_provider +from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLogging +from litellm.proxy.auth.model_checks import get_known_models_from_wildcard +from litellm.proxy.common_request_processing import cost_headers +from litellm.types.utils import Delta, ModelResponseStream, StreamingChoices, Usage + +_REPO_ROOT: Final = Path(__file__).resolve().parents[4] +_PRIMARY_PRICES: Final = json.loads((_REPO_ROOT / "model_prices_and_context_window.json").read_text()) +_BACKUP_PRICES: Final = json.loads((_REPO_ROOT / "litellm" / "model_prices_and_context_window_backup.json").read_text()) +_CATALOG_MODELS: Final = tuple( + key for key, row in _PRIMARY_PRICES.items() if isinstance(row, dict) and row.get("litellm_provider") == "kyma" +) +_TIER_KEY: Final = re.compile(r"^input_cost_per_token_above_(\d+)k_tokens$") + + +def _long_prompt_tiers(model: str) -> tuple[tuple[str, int], ...]: + matches: Final = map(_TIER_KEY.match, _PRIMARY_PRICES[model]) + return tuple((model, int(match.group(1))) for match in matches if match is not None) + + +_LONG_PROMPT_TIERS: Final = tuple(chain.from_iterable(map(_long_prompt_tiers, _CATALOG_MODELS))) +_CHAT_URL: Final = "https://kymaapi.com/v1/chat/completions" +_UNLISTED_MODEL: Final = "kyma/model-added-after-this-release" + + +def _kyma_chat_body(usage: dict[str, object]) -> dict[str, object]: + return { + "id": "chatcmpl_kyma", + "object": "chat.completion", + "created": 1_792_000_000, + "model": "sonar", + "choices": [{"index": 0, "message": {"role": "assistant", "content": "pong"}, "finish_reason": "stop"}], + "usage": usage, + } + + +def test_kyma_provider_resolution(monkeypatch: pytest.MonkeyPatch): + monkeypatch.setenv("KYMA_API_KEY", "kyma-test-key") + + model, provider, api_key, api_base = get_llm_provider( + model="kyma/qwen3.7-flash", + custom_llm_provider=None, + api_base=None, + api_key=None, + ) + + assert model == "qwen3.7-flash" + assert provider == "kyma" + assert api_key == "kyma-test-key" + assert api_base == "https://kymaapi.com/v1" + + +def test_kyma_provider_keeps_explicit_credentials(monkeypatch: pytest.MonkeyPatch): + monkeypatch.setenv("KYMA_API_KEY", "kyma-env-key") + + _, provider, api_key, api_base = get_llm_provider( + model="kyma/qwen3.7-flash", + custom_llm_provider=None, + api_base="https://kyma.internal.example/v1", + api_key="kyma-explicit-key", + ) + + assert provider == "kyma" + assert api_key == "kyma-explicit-key" + assert api_base == "https://kyma.internal.example/v1" + + +def test_bare_model_on_kyma_base_url_keeps_openai_routing(monkeypatch: pytest.MonkeyPatch): + monkeypatch.setenv("OPENAI_API_KEY", "openai-env-key") + + model, provider, api_key, api_base = get_llm_provider( + model="gpt-6-luna", + custom_llm_provider=None, + api_base="https://kymaapi.com/v1", + api_key=None, + ) + + assert (model, provider, api_base) == ("gpt-6-luna", "openai", "https://kymaapi.com/v1") + assert api_key is None + + +def test_kyma_is_available_in_add_model_form(): + fields_path: Final = Path(litellm.__file__).parent / "proxy" / "public_endpoints" / "provider_create_fields.json" + providers: Final = json.loads(fields_path.read_text()) + kyma: Final = next(provider for provider in providers if provider["litellm_provider"] == "kyma") + + assert kyma["default_model_placeholder"] in _CATALOG_MODELS + assert {field["key"]: field["required"] for field in kyma["credential_fields"]} == { + "api_base": False, + "api_key": True, + } + + +def test_kyma_chat_completion_request(): + with respx.mock() as upstream: + route: Final = upstream.post(_CHAT_URL).respond( + 200, + json={ + "id": "chatcmpl_kyma", + "object": "chat.completion", + "created": 1_792_000_000, + "model": "qwen3.7-flash", + "choices": [ + {"index": 0, "message": {"role": "assistant", "content": "Hello from Kyma"}, "finish_reason": "stop"} + ], + "usage": {"prompt_tokens": 4, "completion_tokens": 3, "total_tokens": 7}, + }, + ) + response: Final = litellm.completion( + model="kyma/qwen3.7-flash", + messages=[{"role": "user", "content": "Say hello"}], + api_key="kyma-test-key", + ) + + request: Final = route.calls.last.request + body: Final = json.loads(request.content) + assert route.call_count == 1 + assert str(request.url) == _CHAT_URL + assert request.headers["authorization"] == "Bearer kyma-test-key" + assert body["model"] == "qwen3.7-flash" + assert body["messages"] == [{"role": "user", "content": "Say hello"}] + assert response.choices[0].message.content == "Hello from Kyma" + + +@pytest.mark.parametrize("model", _CATALOG_MODELS) +def test_catalog_row_matches_its_backup_and_fits_its_context(model: str): + row: Final = _PRIMARY_PRICES[model] + + assert row == _BACKUP_PRICES[model] + assert row["mode"] == "chat" + assert 0 < row["max_output_tokens"] <= row["max_input_tokens"] + assert row["input_cost_per_token"] > 0 + assert row["output_cost_per_token"] > 0 + + +@pytest.mark.usefixtures("local_model_cost_map") +@pytest.mark.parametrize("model", _CATALOG_MODELS) +def test_chat_response_is_priced_at_its_catalog_row(model: str): + row: Final = _PRIMARY_PRICES[model] + response: Final = litellm.ModelResponse( + model=model.removeprefix("kyma/"), + usage=litellm.Usage(prompt_tokens=1000, completion_tokens=200, total_tokens=1200), + ) + + cost: Final = litellm.completion_cost(completion_response=response, model=model) + + assert cost == pytest.approx(1000 * row["input_cost_per_token"] + 200 * row["output_cost_per_token"]) + + +@pytest.mark.usefixtures("local_model_cost_map") +@pytest.mark.parametrize(("model", "threshold_k"), _LONG_PROMPT_TIERS) +def test_long_prompt_is_priced_at_its_tier(model: str, threshold_k: int): + row: Final = _PRIMARY_PRICES[model] + prompt_tokens: Final = threshold_k * 1000 + 1000 + response: Final = litellm.ModelResponse( + model=model.removeprefix("kyma/"), + usage=litellm.Usage(prompt_tokens=prompt_tokens, completion_tokens=200, total_tokens=prompt_tokens + 200), + ) + + cost: Final = litellm.completion_cost(completion_response=response, model=model) + + assert cost == pytest.approx( + prompt_tokens * row[f"input_cost_per_token_above_{threshold_k}k_tokens"] + + 200 * row[f"output_cost_per_token_above_{threshold_k}k_tokens"] + ) + + +@pytest.mark.parametrize("model", ("kyma/sonar", _UNLISTED_MODEL)) +def test_kyma_response_is_billed_at_the_cost_kyma_reports(model: str): + reported_cost: Final = 0.00721 + with respx.mock() as upstream: + upstream.post(_CHAT_URL).respond( + 200, json=_kyma_chat_body({"prompt_tokens": 4, "completion_tokens": 3, "total_tokens": 7, "cost": reported_cost}) + ) + response: Final = litellm.completion(model=model, messages=[{"role": "user", "content": "ping"}], api_key="k") + + assert response._hidden_params["response_cost"] == pytest.approx(reported_cost) + + +def test_deployment_pricing_overrides_the_cost_kyma_reports(): + with respx.mock() as upstream: + upstream.post(_CHAT_URL).respond( + 200, json=_kyma_chat_body({"prompt_tokens": 4, "completion_tokens": 3, "total_tokens": 7, "cost": 0.00721}) + ) + response: Final = litellm.completion( + model="kyma/sonar", + messages=[{"role": "user", "content": "ping"}], + api_key="k", + input_cost_per_token=0.001, + output_cost_per_token=0.002, + ) + + assert response._hidden_params["response_cost"] == pytest.approx(4 * 0.001 + 3 * 0.002) + + + +def _catalog_price(model: str, prompt_tokens: int, completion_tokens: int) -> float: + row: Final = _PRIMARY_PRICES[model] + return prompt_tokens * row["input_cost_per_token"] + completion_tokens * row["output_cost_per_token"] + + +@pytest.mark.usefixtures("local_model_cost_map") +@pytest.mark.parametrize("reported_cost", ("-0.5", "1e999")) +def test_unusable_cost_from_kyma_falls_back_to_the_catalog_price(reported_cost: str): + usage: Final = {"prompt_tokens": 4, "completion_tokens": 3, "total_tokens": 7, "cost": "REPORTED_COST"} + with respx.mock() as upstream: + upstream.post(_CHAT_URL).respond( + 200, + content=json.dumps(_kyma_chat_body(usage)).replace('"REPORTED_COST"', reported_cost), + headers={"content-type": "application/json"}, + ) + response: Final = litellm.completion(model="kyma/sonar", messages=[{"role": "user", "content": "ping"}], api_key="k") + + assert response._hidden_params["response_cost"] == pytest.approx(_catalog_price("kyma/sonar", 4, 3)) + + +@pytest.mark.usefixtures("local_model_cost_map") +def test_deployment_base_model_overrides_the_cost_kyma_reports(): + with respx.mock() as upstream: + upstream.post(_CHAT_URL).respond( + 200, json=_kyma_chat_body({"prompt_tokens": 4, "completion_tokens": 3, "total_tokens": 7, "cost": 0.00721}) + ) + response: Final = litellm.completion( + model="kyma/sonar", + messages=[{"role": "user", "content": "ping"}], + api_key="k", + base_model="kyma/qwen3.7-flash", + ) + + assert response._hidden_params["response_cost"] == pytest.approx(_catalog_price("kyma/qwen3.7-flash", 4, 3)) + + +def test_interaction_on_kyma_is_billed_at_the_cost_kyma_reports(): + reported_cost: Final = 0.00721 + with respx.mock() as upstream: + upstream.post(_CHAT_URL).respond( + 200, json=_kyma_chat_body({"prompt_tokens": 4, "completion_tokens": 3, "total_tokens": 7, "cost": reported_cost}) + ) + interaction: Final = litellm.interactions.create(model="kyma/qwen3.7-flash", input="ping", api_key="k") + + assert interaction._hidden_params["response_cost"] == pytest.approx(reported_cost) + + +@pytest.mark.parametrize("model", ("openrouter/openai/gpt-4o-mini", "deepseek/deepseek-chat")) +def test_interaction_on_another_provider_keeps_its_upstream_cost_out_of_the_body(model: str): + chat_usage: Final = {"prompt_tokens": 4, "completion_tokens": 3, "total_tokens": 7, "cost": 0.00721} + responses_body: Final = { + "id": "resp_upstream", + "object": "response", + "created_at": 1_792_000_000, + "status": "completed", + "model": "x", + "output": [ + { + "type": "message", + "id": "msg_upstream", + "status": "completed", + "role": "assistant", + "content": [{"type": "output_text", "text": "pong", "annotations": []}], + } + ], + "usage": {"input_tokens": 4, "output_tokens": 3, "total_tokens": 7, "cost": 0.00721}, + } + with respx.mock(assert_all_called=False) as upstream: + upstream.route(path__regex=r".*/chat/completions$").respond(200, json=_kyma_chat_body(chat_usage)) + upstream.route(path__regex=r".*/responses$").respond(200, json=responses_body) + interaction: Final = litellm.interactions.create(model=model, input="ping", api_key="k") + + assert "cost" not in interaction.model_dump(exclude_none=True)["usage"] + + +def test_sdk_completion_cost_prices_kyma_at_the_cost_kyma_reports(): + reported_cost: Final = 0.00721 + with respx.mock() as upstream: + upstream.post(_CHAT_URL).respond( + 200, json=_kyma_chat_body({"prompt_tokens": 4, "completion_tokens": 3, "total_tokens": 7, "cost": reported_cost}) + ) + response: Final = litellm.completion(model="kyma/sonar", messages=[{"role": "user", "content": "ping"}], api_key="k") + + assert litellm.completion_cost(completion_response=response) == pytest.approx(reported_cost) + + +def _kyma_sdk_response(usage: dict[str, float]) -> litellm.ModelResponse: + with respx.mock() as upstream: + upstream.post(_CHAT_URL).respond(200, json=_kyma_chat_body(usage)) + response: Final = litellm.completion(model="kyma/sonar", messages=[{"role": "user", "content": "ping"}], api_key="k") + assert isinstance(response, litellm.ModelResponse) + return response + + +_KYMA_TOKEN_USAGE: Final = {"prompt_tokens": 4, "completion_tokens": 3, "total_tokens": 7} +_KYMA_REPORTED_COST: Final = 0.00721 + + +def test_sdk_completion_cost_uses_the_callers_token_prices_over_the_cost_kyma_reports(): + caller_prices: Final = {"input_cost_per_token": 0.5, "output_cost_per_token": 2.0} + with_reported_cost: Final = _kyma_sdk_response({**_KYMA_TOKEN_USAGE, "cost": _KYMA_REPORTED_COST}) + without_reported_cost: Final = _kyma_sdk_response(_KYMA_TOKEN_USAGE) + + assert litellm.completion_cost( + completion_response=with_reported_cost, custom_cost_per_token=caller_prices + ) == pytest.approx(litellm.completion_cost(completion_response=without_reported_cost, custom_cost_per_token=caller_prices)) + assert litellm.completion_cost( + completion_response=with_reported_cost, custom_cost_per_token=caller_prices + ) != pytest.approx(_KYMA_REPORTED_COST) + + +def test_sdk_completion_cost_uses_the_callers_per_second_price_over_the_cost_kyma_reports(): + response: Final = _kyma_sdk_response({**_KYMA_TOKEN_USAGE, "cost": _KYMA_REPORTED_COST}) + + assert litellm.completion_cost(completion_response=response, custom_cost_per_second=50.0) == pytest.approx( + 50.0 * response._response_ms / 1000 + ) + + +def _with_kyma_discount_and_margin(monkeypatch: pytest.MonkeyPatch) -> float: + monkeypatch.setattr(litellm, "cost_discount_config", {"kyma": 0.5}) + monkeypatch.setattr(litellm, "cost_margin_config", {"kyma": 0.1}) + return 0.5 * 1.1 + + +def test_cost_kyma_reports_takes_the_configured_discount_and_margin(monkeypatch: pytest.MonkeyPatch): + adjustment: Final = _with_kyma_discount_and_margin(monkeypatch) + with respx.mock() as upstream: + upstream.post(_CHAT_URL).respond( + 200, json=_kyma_chat_body({"prompt_tokens": 4, "completion_tokens": 3, "total_tokens": 7, "cost": 0.008}) + ) + response: Final = litellm.completion(model="kyma/sonar", messages=[{"role": "user", "content": "ping"}], api_key="k") + + assert response._hidden_params["response_cost"] == pytest.approx(0.008 * adjustment) + + +def _kyma_stream_chunk(content: str, finish_reason: str | None = None) -> ModelResponseStream: + return ModelResponseStream( + id="chatcmpl_kyma", + created=1_792_000_000, + model="sonar", + object="chat.completion.chunk", + choices=[StreamingChoices(finish_reason=finish_reason, index=0, delta=Delta(content=content, role="assistant"))], + ) + + +def test_streamed_cost_kyma_reports_takes_the_configured_discount_and_margin(monkeypatch: pytest.MonkeyPatch): + adjustment: Final = _with_kyma_discount_and_margin(monkeypatch) + usage_chunk: Final = _kyma_stream_chunk("") + usage_chunk.usage = Usage(prompt_tokens=4, completion_tokens=3, total_tokens=7, cost=0.008) + logging_obj: Final = LiteLLMLogging( + model="sonar", + messages=[{"role": "user", "content": "ping"}], + stream=True, + call_type="completion", + start_time=datetime.now(), + litellm_call_id="kyma-call", + function_id="kyma-function", + ) + logging_obj.update_environment_variables( + model="sonar", + user=None, + optional_params={}, + litellm_params={"custom_llm_provider": "kyma"}, + custom_llm_provider="kyma", + ) + + response: Final = litellm.stream_chunk_builder( + chunks=[_kyma_stream_chunk("pong", finish_reason="stop"), usage_chunk], + messages=[{"role": "user", "content": "ping"}], + logging_obj=logging_obj, + ) + + assert logging_obj.response_cost_calculator(result=response) == pytest.approx(0.008 * adjustment) + + +def test_cost_headers_show_the_discount_and_margin_taken_from_the_cost_kyma_reports(monkeypatch: pytest.MonkeyPatch): + _with_kyma_discount_and_margin(monkeypatch) + logging_obj: Final = LiteLLMLogging( + model="sonar", + messages=[{"role": "user", "content": "ping"}], + stream=False, + call_type="completion", + start_time=datetime.now(), + litellm_call_id="kyma-call", + function_id="kyma-function", + ) + logging_obj.update_environment_variables( + model="sonar", + user=None, + optional_params={}, + litellm_params={"custom_llm_provider": "kyma"}, + custom_llm_provider="kyma", + ) + response: Final = litellm.ModelResponse( + model="sonar", usage=Usage(prompt_tokens=4, completion_tokens=3, total_tokens=7, cost=0.008) + ) + + headers: Final = cost_headers(logging_obj.response_cost_calculator(result=response), logging_obj) + + assert float(headers["x-litellm-response-cost-original"] or "nan") == pytest.approx(0.008) + assert float(headers["x-litellm-response-cost-discount-amount"] or "nan") == pytest.approx(0.004) + assert float(headers["x-litellm-response-cost-margin-amount"] or "nan") == pytest.approx(0.0004) + assert float(headers["x-litellm-response-cost"] or "nan") == pytest.approx(0.0044) + assert headers["x-litellm-response-cost-input"] is None + assert headers["x-litellm-response-cost-output"] is None + + +@pytest.mark.parametrize("thinking", ({"type": "enabled", "budget_tokens": 1024}, {"type": "adaptive"})) +def test_messages_thinking_on_a_kyma_claude_model_is_sent_as_reasoning_effort(thinking: dict[str, object]): + with respx.mock() as upstream: + route: Final = upstream.post(_CHAT_URL).respond( + 200, json=_kyma_chat_body({"prompt_tokens": 4, "completion_tokens": 3, "total_tokens": 7}) + ) + litellm.anthropic.messages.create( + model="kyma/claude-haiku-4-5", + max_tokens=2048, + messages=[{"role": "user", "content": "ping"}], + thinking=thinking, + api_key="k", + ) + + sent: Final = json.loads(route.calls.last.request.content) + assert "thinking" not in sent + assert sent["reasoning_effort"] in ("low", "medium", "high") + +def test_kyma_model_missing_from_the_catalog_keeps_its_tools(): + tools: Final = [{"type": "function", "function": {"name": "lookup", "parameters": {"type": "object"}}}] + with respx.mock() as upstream: + route: Final = upstream.post(_CHAT_URL).respond( + 200, json=_kyma_chat_body({"prompt_tokens": 4, "completion_tokens": 3, "total_tokens": 7}) + ) + litellm.completion(model=_UNLISTED_MODEL, messages=[{"role": "user", "content": "ping"}], api_key="k", tools=tools) + + assert json.loads(route.calls.last.request.content)["tools"] == tools + + +def test_kyma_wildcard_lists_every_catalog_model(): + assert sorted(get_known_models_from_wildcard("kyma/*")) == sorted(_CATALOG_MODELS) + + +def _generate_image() -> object: + return litellm.image_generation(model="kyma/flux-2", prompt="a cat", api_key="k") + + +def _transcribe() -> object: + return litellm.transcription(model="kyma/whisper-1", file=("a.wav", b"RIFF0000WAVE", "audio/wav"), api_key="k") + + +@pytest.mark.parametrize("call", (_generate_image, _transcribe)) +def test_unpriced_kyma_media_calls_are_refused_without_calling_kyma(call: Callable[[], object]): + with respx.mock(assert_all_called=False) as upstream: + kyma: Final = upstream.route(host="kymaapi.com").respond(202, json={"id": "job_kyma"}) + with pytest.raises(litellm.BadRequestError): + call() + + assert kyma.call_count == 0 diff --git a/tests/unit/llms/opensearch/__init__.py b/tests/unit/llms/opensearch/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/unit/llms/opensearch/vector_stores/__init__.py b/tests/unit/llms/opensearch/vector_stores/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/unit/llms/opensearch/vector_stores/test_opensearch_transformation.py b/tests/unit/llms/opensearch/vector_stores/test_opensearch_transformation.py new file mode 100644 index 00000000000..07836d57af0 --- /dev/null +++ b/tests/unit/llms/opensearch/vector_stores/test_opensearch_transformation.py @@ -0,0 +1,482 @@ +import hashlib +import json +import re +from collections.abc import Callable, Mapping +from datetime import datetime, timezone +from pathlib import Path +from typing import Final +from unittest.mock import MagicMock + +import httpx +import openai +import pytest +from botocore.auth import SigV4Auth +from botocore.awsrequest import AWSRequest +from botocore.credentials import Credentials +from botocore.exceptions import CredentialRetrievalError + +import litellm +from litellm.litellm_core_utils.litellm_logging import Logging +from litellm.llms.base_llm.chat.transformation import BaseLLMException +from litellm.llms.bedrock.base_aws_llm import BaseAWSLLM +from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler +from litellm.llms.opensearch.vector_stores.transformation import OpenSearchVectorStoreConfig +from litellm.types.llms.bedrock import AwsAuthParams +from litellm.types.router import GenericLiteLLMParams +from litellm.types.utils import EmbeddingResponse +from litellm.types.vector_stores import VectorStoreSearchResponse +from litellm.vector_stores import main as vector_stores + +ACCESS_KEY: Final = "AKIDOPENSEARCHTEST" +SECRET_KEY: Final = "opensearch-test-secret" +QUERY: Final = "How many days a year can I work abroad?" +QUERY_VECTOR: Final = [0.1, 0.2, 0.3] +CREDENTIAL_SOURCE_OUTPUT: Final = "credential-process-stderr-detail" +SERVERLESS: Final = "https://abc123.us-east-1.aoss.amazonaws.com" +BASE_PARAMS: Final = { + "api_base": SERVERLESS, + "aws_access_key_id": ACCESS_KEY, + "aws_secret_access_key": SECRET_KEY, + "litellm_embedding_model": "embedding-alias", +} +_AUTHORIZATION: Final = re.compile( + r"\AAWS4-HMAC-SHA256 Credential=(?P[^/]+)/(?P\d{8})/(?P[^/]+)/(?P[^/]+)/aws4_request, " + r"SignedHeaders=(?P[^,]+), Signature=(?P[0-9a-f]{64})\Z" +) + + +class RecordingEmbeddingExecutor: + def __init__(self) -> None: + self.call: Final = MagicMock(return_value=EmbeddingResponse(data=[{"embedding": QUERY_VECTOR}])) + + def embed(self, model: str, query: str, configuration: Mapping[str, object]) -> EmbeddingResponse: + return self.call(model, query, configuration) + + async def aembed(self, model: str, query: str, configuration: Mapping[str, object]) -> EmbeddingResponse: + return self.call(model, query, configuration) + + +def _signature_scope(request: httpx.Request) -> tuple[str, str]: + authorization: Final = _AUTHORIZATION.match(request.headers["authorization"]) + assert authorization is not None, request.headers["authorization"] + signed_names: Final = authorization["signed"].split(";") + assert {"host", "x-amz-content-sha256", "x-amz-date"} <= set(signed_names), signed_names + assert request.headers["x-amz-content-sha256"] == hashlib.sha256(request.content).hexdigest() + signed_request: Final = AWSRequest( + method=request.method, + url=str(request.url), + data=request.content, + headers={name: request.headers[name] for name in signed_names}, + ) + signed_request.context["timestamp"] = request.headers["x-amz-date"] + signer: Final = SigV4Auth(Credentials(ACCESS_KEY, SECRET_KEY), authorization["service"], authorization["region"]) + expected: Final = signer.signature( + signer.string_to_sign(signed_request, signer.canonical_request(signed_request)), signed_request + ) + assert authorization["key"] == ACCESS_KEY + assert authorization["signature"] == expected, "SigV4 signature does not cover the request that was sent" + return authorization["service"], authorization["region"] + + +def _sent(respond: MagicMock) -> tuple[httpx.Request, ...]: + return tuple(call.args[0] for call in respond.call_args_list) + + +def _opensearch_hits(text_field: str, embedding_field: str) -> Mapping[str, object]: + return { + "took": 4, + "timed_out": False, + "hits": { + "total": {"value": 2, "relation": "eq"}, + "max_score": 0.581, + "hits": [ + { + "_index": "litellm-qa-docs", + "_id": "1%3A0%3Aw6ftW6EB", + "_score": 0.581, + "_source": { + text_field: "Employees may work abroad for at most 37 calendar days per year.", + embedding_field: [0.5, 0.5, 0.5], + "embedding_ada002": [0.25, 0.75], + "filename": "hr-remote-work-policy.md", + "category": "hr", + "revision": 7, + "published": True, + "tags": ["hr", "travel"], + "owner": {"team": "people-ops"}, + "reviewed_by": None, + }, + }, + {"_index": "litellm-qa-docs", "_id": "doc-2", "_score": 0.42, "_source": {"category": "facilities"}}, + ], + }, + } + + +EXPECTED_RESULTS: Final = [ + { + "score": 0.581, + "content": [{"type": "text", "text": "Employees may work abroad for at most 37 calendar days per year."}], + "file_id": "1%3A0%3Aw6ftW6EB", + "filename": "hr-remote-work-policy.md", + "attributes": {"filename": "hr-remote-work-policy.md", "category": "hr", "revision": 7, "published": True}, + }, + {"score": 0.42, "content": [], "file_id": "doc-2", "filename": None, "attributes": {"category": "facilities"}}, +] + + +async def _search( + asynchronous: bool, + respond: Callable[[httpx.Request], httpx.Response], + executor: RecordingEmbeddingExecutor, + kwargs: Mapping[str, object], +) -> VectorStoreSearchResponse: + request_kwargs: Final = { + **BASE_PARAMS, + "vector_store_id": "litellm-qa-docs", + "query": QUERY, + "custom_llm_provider": "opensearch", + "_direct_vector_store_embedding_executor": executor, + **kwargs, + } + if asynchronous: + return await litellm.vector_stores.asearch( + **request_kwargs, client=AsyncHTTPHandler(transport=httpx.MockTransport(respond)) + ) + with httpx.Client(transport=httpx.MockTransport(respond)) as transport: + return litellm.vector_stores.search(**request_kwargs, client=HTTPHandler(client=transport)) + + +@pytest.mark.parametrize("asynchronous", [False, True]) +@pytest.mark.parametrize( + "fields", + [{}, {"opensearch_embedding_field": "passage_vector", "opensearch_text_field": "passage"}], +) +@pytest.mark.parametrize( + "api_base,region_params,service,region", + [ + (SERVERLESS, {}, "aoss", "us-east-1"), + (SERVERLESS, {"aws_region_name": "us-west-2"}, "aoss", "us-east-1"), + ("https://search-docs-xyz.eu-west-1.es.amazonaws.com/", {}, "es", "eu-west-1"), + ("https://vpc-docs-xyz.cn-north-1.es.amazonaws.com.cn", {}, "es", "cn-north-1"), + ("https://abc123.us-gov-west-1.aoss-fips.amazonaws.com", {}, "aoss", "us-gov-west-1"), + ("https://abc123.aoss.eu-central-1.on.aws", {}, "aoss", "eu-central-1"), + ("https://search-docs-xyz.aos.ap-southeast-2.on.aws", {}, "es", "ap-southeast-2"), + ("https://search.docs.example", {"aws_region_name": "ap-south-1"}, "es", "ap-south-1"), + ], +) +@pytest.mark.asyncio +async def test_search_sends_a_signed_knn_query_and_returns_an_openai_results_page( + asynchronous: bool, + fields: Mapping[str, str], + api_base: str, + region_params: Mapping[str, str], + service: str, + region: str, +) -> None: + executor: Final = RecordingEmbeddingExecutor() + embedding_field: Final = fields.get("opensearch_embedding_field", "embedding") + text_field: Final = fields.get("opensearch_text_field", "text") + respond: Final = MagicMock(return_value=httpx.Response(200, json=_opensearch_hits(text_field, embedding_field))) + + response: Final = await _search( + asynchronous, + respond, + executor, + {**fields, **region_params, "api_base": api_base, "max_num_results": 3}, + ) + + assert response == {"object": "vector_store.search_results.page", "search_query": QUERY, "data": EXPECTED_RESULTS} + sent: Final = _sent(respond) + assert len(sent) == 1 + assert str(sent[0].url) == f"{api_base.rstrip('/')}/litellm-qa-docs/_search" + assert json.loads(sent[0].content) == { + "size": 3, + "query": {"knn": {embedding_field: {"vector": QUERY_VECTOR, "k": 3}}}, + "_source": {"excludes": [embedding_field]}, + } + assert _signature_scope(sent[0]) == (service, region) + executor.call.assert_called_once_with("embedding-alias", QUERY, {}) + + +@pytest.mark.asyncio +async def test_search_defaults_to_ten_results_like_openai() -> None: + respond: Final = MagicMock(return_value=httpx.Response(200, json={"hits": {"hits": []}})) + + response: Final = await _search(True, respond, RecordingEmbeddingExecutor(), {}) + + assert response == {"object": "vector_store.search_results.page", "search_query": QUERY, "data": []} + body: Final = json.loads(_sent(respond)[0].content) + assert (body["size"], body["query"]["knn"]["embedding"]["k"]) == (10, 10) + + +@pytest.mark.parametrize("asynchronous", [False, True]) +@pytest.mark.asyncio +async def test_a_list_query_is_embedded_and_echoed_as_one_string(asynchronous: bool) -> None: + executor: Final = RecordingEmbeddingExecutor() + respond: Final = MagicMock(return_value=httpx.Response(200, json={"hits": {"hits": []}})) + + response: Final = await _search(asynchronous, respond, executor, {"query": ["remote work", "abroad"]}) + + assert response["search_query"] == "remote work abroad" + executor.call.assert_called_once_with("embedding-alias", "remote work abroad", {}) + + +@pytest.mark.parametrize("asynchronous", [False, True]) +@pytest.mark.parametrize("status", [403, 404, 429, 500]) +@pytest.mark.asyncio +async def test_opensearch_error_keeps_its_status(asynchronous: bool, status: int) -> None: + def respond(request: httpx.Request) -> httpx.Response: + return httpx.Response(status, json={"status": status, "error": {"reason": "upstream refused", "type": "x"}}) + + with pytest.raises(openai.APIStatusError) as error: + await _search(asynchronous, respond, RecordingEmbeddingExecutor(), {}) + + assert error.value.status_code == status + + +@pytest.mark.parametrize( + "overrides,message", + [ + ({"filters": {"type": "eq", "key": "category", "value": "hr"}}, "filters"), + ({"ranking_options": {"score_threshold": 0.5}}, "ranking_options"), + ({"rewrite_query": True}, "rewrite_query"), + ({"max_num_results": 0}, "max_num_results"), + ({"max_num_results": 51}, "max_num_results"), + ({"max_num_results": "ten"}, "Invalid vector store search parameters"), + ({"litellm_embedding_model": None}, "litellm_embedding_model"), + ({"opensearch_text_feild": "passage"}, "opensearch_text_feild"), + ({"opensearch_text_field": 7}, "opensearch_text_field"), + ({"opensearch_index_name": ["docs"]}, "opensearch_index_name"), + ({"extra_body": {"min_score": 0.5}}, "extra_body"), + ({"api_base": None}, "api_base"), + ({"api_base": "abc123.us-east-1.aoss.amazonaws.com"}, "api_base"), + ({"api_base": "https://123456789012.aoss.us-east-1.on.aws"}, "per-account endpoint"), + ({"api_key": "opensearch-basic-auth"}, "api_key"), + ], +) +@pytest.mark.asyncio +async def test_unsupported_search_is_rejected_before_embedding_or_any_request( + overrides: Mapping[str, object], message: str +) -> None: + executor: Final = RecordingEmbeddingExecutor() + respond: Final = MagicMock(return_value=httpx.Response(200, json={"hits": {"hits": []}})) + + with pytest.raises(litellm.BadRequestError, match=message) as error: + await _search(True, respond, executor, overrides) + + assert error.value.status_code == 400 + respond.assert_not_called() + executor.call.assert_not_called() + + +_AMBIENT_AWS_ENV: Final = ( + "AWS_ACCESS_KEY_ID", + "AWS_SECRET_ACCESS_KEY", + "AWS_SESSION_TOKEN", + "AWS_PROFILE", + "AWS_PROFILE_NAME", + "AWS_ROLE_NAME", + "AWS_ROLE_ARN", + "AWS_SESSION_NAME", + "AWS_WEB_IDENTITY_TOKEN", + "AWS_WEB_IDENTITY_TOKEN_FILE", + "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI", + "AWS_CONTAINER_CREDENTIALS_FULL_URI", +) + + +@pytest.mark.parametrize("asynchronous", [False, True]) +@pytest.mark.parametrize( + "credentials,env", + [ + pytest.param({"aws_session_name": "opensearch-no-credentials"}, {}, id="none-anywhere"), + pytest.param({"aws_profile_name": "opensearch-missing-profile"}, {}, id="missing-profile"), + pytest.param({}, {"AWS_ACCESS_KEY_ID": "AKIDPARTIAL"}, id="key-without-secret"), + pytest.param( + {"aws_access_key_id": "AKIDSTOREONLY"}, + {"AWS_ACCESS_KEY_ID": "AKIDAMBIENT", "AWS_SECRET_ACCESS_KEY": "ambient-secret"}, + id="store-key-without-secret-beside-ambient-credentials", + ), + pytest.param( + {"aws_secret_access_key": "store-secret-only"}, + {"AWS_ACCESS_KEY_ID": "AKIDAMBIENT", "AWS_SECRET_ACCESS_KEY": "ambient-secret"}, + id="store-secret-without-key-beside-ambient-credentials", + ), + ], +) +@pytest.mark.asyncio +async def test_missing_aws_credentials_are_a_401_before_embedding_or_any_request( + asynchronous: bool, + credentials: Mapping[str, str], + env: Mapping[str, str], + monkeypatch: pytest.MonkeyPatch, + tmp_path: Path, +) -> None: + for name in _AMBIENT_AWS_ENV: + monkeypatch.delenv(name, raising=False) + for name in ("AWS_CONFIG_FILE", "AWS_SHARED_CREDENTIALS_FILE", "BOTO_CONFIG"): + config_file: Path = tmp_path / name + config_file.write_text("") + monkeypatch.setenv(name, str(config_file)) + monkeypatch.setenv("AWS_EC2_METADATA_DISABLED", "true") + for name, value in env.items(): + monkeypatch.setenv(name, value) + executor: Final = RecordingEmbeddingExecutor() + respond: Final = MagicMock(return_value=httpx.Response(200, json={"hits": {"hits": []}})) + + with pytest.raises(litellm.AuthenticationError, match="No usable AWS credentials") as error: + await _search( + asynchronous, + respond, + executor, + {"aws_access_key_id": None, "aws_secret_access_key": None, **credentials}, + ) + + assert error.value.status_code == 401 + respond.assert_not_called() + executor.call.assert_not_called() + + +class _FailingCredentialSource(BaseAWSLLM): + def resolve_credentials(self, auth_params: AwsAuthParams, aws_region_name: str | None) -> Credentials: + raise CredentialRetrievalError(provider="container-role", error_msg=CREDENTIAL_SOURCE_OUTPUT) + + +def test_failing_credential_source_is_a_401_that_keeps_its_output_out_of_the_reply() -> None: + config: Final = OpenSearchVectorStoreConfig(aws=_FailingCredentialSource()) + + with pytest.raises(litellm.AuthenticationError, match="the AWS credential source failed") as error: + config.validate_environment({}, GenericLiteLLMParams(api_base=SERVERLESS)) + + assert error.value.status_code == 401 + assert CREDENTIAL_SOURCE_OUTPUT not in str(error.value) + + +@pytest.mark.parametrize("asynchronous", [False, True]) +@pytest.mark.asyncio +async def test_opensearch_index_name_picks_the_index_so_the_store_id_can_differ(asynchronous: bool) -> None: + respond: Final = MagicMock(return_value=httpx.Response(200, json=_opensearch_hits("text", "embedding"))) + + response: Final = await _search( + asynchronous, + respond, + RecordingEmbeddingExecutor(), + {"vector_store_id": "east-docs", "opensearch_index_name": "docs/v2"}, + ) + + assert response["data"] == EXPECTED_RESULTS + assert str(_sent(respond)[0].url) == f"{SERVERLESS}/docs%2Fv2/_search" + + +@pytest.mark.parametrize( + "unsupported", + [ + {"filters": {"type": "eq", "key": "category", "value": "hr"}}, + {"ranking_options": {"score_threshold": 0.5}}, + {"rewrite_query": True}, + ], +) +@pytest.mark.asyncio +async def test_unsupported_search_params_are_dropped_under_drop_params(unsupported: Mapping[str, object]) -> None: + respond: Final = MagicMock(return_value=httpx.Response(200, json=_opensearch_hits("text", "embedding"))) + + response: Final = await _search( + True, respond, RecordingEmbeddingExecutor(), {**unsupported, "drop_params": True, "max_num_results": 3} + ) + + assert response["data"] == EXPECTED_RESULTS + assert json.loads(_sent(respond)[0].content) == { + "size": 3, + "query": {"knn": {"embedding": {"vector": QUERY_VECTOR, "k": 3}}}, + "_source": {"excludes": ["embedding"]}, + } + + +@pytest.mark.asyncio +async def test_no_op_values_of_unsupported_params_are_accepted() -> None: + def respond(request: httpx.Request) -> httpx.Response: + return httpx.Response(200, json=_opensearch_hits("text", "embedding")) + + response: Final = await _search( + True, respond, RecordingEmbeddingExecutor(), {"filters": {}, "ranking_options": {}, "rewrite_query": False} + ) + + assert response["data"] == EXPECTED_RESULTS + + +def test_create_is_rejected_because_the_store_is_search_only() -> None: + with pytest.raises(litellm.BadRequestError, match="search-only"): + litellm.vector_stores.create(custom_llm_provider="opensearch") + + +async def _store_operation( + asynchronous: bool, operation: str, respond: Callable[[httpx.Request], httpx.Response] +) -> object: + request_kwargs: Final = {**BASE_PARAMS, "vector_store_id": "litellm-qa-docs", "custom_llm_provider": "opensearch"} + if asynchronous: + return await getattr(vector_stores, f"a{operation}")( + **request_kwargs, client=AsyncHTTPHandler(transport=httpx.MockTransport(respond)) + ) + with httpx.Client(transport=httpx.MockTransport(respond)) as transport: + return getattr(vector_stores, operation)(**request_kwargs, client=HTTPHandler(client=transport)) + + +@pytest.mark.parametrize("asynchronous", [False, True]) +@pytest.mark.parametrize("operation", ["retrieve", "update", "delete"]) +@pytest.mark.asyncio +async def test_store_operations_besides_search_send_nothing_to_the_index(asynchronous: bool, operation: str) -> None: + respond: Final = MagicMock(return_value=httpx.Response(200, json={"acknowledged": True})) + + with pytest.raises(litellm.BadRequestError, match="search-only") as error: + await _store_operation(asynchronous, operation, respond) + + assert error.value.status_code == 400 + respond.assert_not_called() + + +async def _list_stores(asynchronous: bool, respond: Callable[[httpx.Request], httpx.Response]) -> object: + request_kwargs: Final = {**BASE_PARAMS, "custom_llm_provider": "opensearch"} + if asynchronous: + return await vector_stores.alist( + **request_kwargs, client=AsyncHTTPHandler(transport=httpx.MockTransport(respond)) + ) + with httpx.Client(transport=httpx.MockTransport(respond)) as transport: + return vector_stores.list(**request_kwargs, client=HTTPHandler(client=transport)) + + +@pytest.mark.parametrize("asynchronous", [False, True]) +@pytest.mark.asyncio +async def test_listing_stores_sends_nothing_to_the_endpoint(asynchronous: bool) -> None: + respond: Final = MagicMock(return_value=httpx.Response(200, json={"cluster_name": "docs", "tagline": "OpenSearch"})) + + with pytest.raises(litellm.BadRequestError, match="search-only") as error: + await _list_stores(asynchronous, respond) + + assert error.value.status_code == 400 + respond.assert_not_called() + + +@pytest.mark.parametrize( + "response,status", + [ + (httpx.Response(403, json={"status": 403, "error": {"type": "Forbidden"}}), 403), + (httpx.Response(200, json={"took": 1}), 502), + (httpx.Response(200, text="gateway"), 502), + ], +) +def test_response_without_search_hits_is_an_error_not_an_empty_page(response: httpx.Response, status: int) -> None: + logging: Final = Logging( + model="", + messages=[], + stream=False, + call_type="vector_store_search", + start_time=datetime(2026, 10, 20, tzinfo=timezone.utc), + litellm_call_id="opensearch-test", + function_id="opensearch-test", + ) + + with pytest.raises(BaseLLMException) as error: + OpenSearchVectorStoreConfig().transform_search_vector_store_response(response, logging) + + assert error.value.status_code == status diff --git a/tests/unit/llms/sail/conftest.py b/tests/unit/llms/sail/conftest.py index 2b2a6e5cae0..c1156af5b12 100644 --- a/tests/unit/llms/sail/conftest.py +++ b/tests/unit/llms/sail/conftest.py @@ -4,11 +4,9 @@ from typing import Final import httpx import pytest -import pytest_asyncio import respx import litellm -from litellm.litellm_core_utils.logging_worker import GLOBAL_LOGGING_WORKER from tests.unit.llms.sail.helpers import SAIL_API_BASE, SpendCapture, chat_completion_body @@ -26,9 +24,8 @@ def sail_env(local_model_cost_map: None, monkeypatch: pytest.MonkeyPatch) -> Ite litellm.in_memory_llm_clients_cache.flush_cache() -@pytest_asyncio.fixture -async def spend_capture(monkeypatch: pytest.MonkeyPatch) -> SpendCapture: - GLOBAL_LOGGING_WORKER.start() +@pytest.fixture +def spend_capture(monkeypatch: pytest.MonkeyPatch) -> SpendCapture: capture: Final = SpendCapture(call_id=f"sail-{uuid.uuid4()}") monkeypatch.setattr(litellm, "callbacks", [capture]) return capture diff --git a/tests/unit/llms/sap/chat/test_sap_transformation.py b/tests/unit/llms/sap/chat/test_sap_transformation.py index 41e75b7bfc7..f95bf9b7845 100644 --- a/tests/unit/llms/sap/chat/test_sap_transformation.py +++ b/tests/unit/llms/sap/chat/test_sap_transformation.py @@ -1,4 +1,6 @@ import warnings +from typing import Final + import pytest from pydantic import ValidationError @@ -908,6 +910,22 @@ class TestCacheControl: assert "cache_control" not in content[0] assert content[1].get("cache_control") == {"type": "ephemeral"} + def test_pdf_file_part_reaches_sap_as_an_image_url_data_url(self) -> None: + pdf_data_url: Final = "data:application/pdf;base64,JVBERi0xLjQK" + messages: Final = [ + { + "role": "user", + "content": [ + {"type": "text", "text": "Summarize this."}, + {"type": "file", "file": {"filename": "report.pdf", "file_data": pdf_data_url}}, + ], + } + ] + body: Final = self._transform("anthropic--claude-3-5-sonnet", messages) + content: Final = self._template(body)[0]["content"] + assert content[1]["type"] == "image_url" + assert content[1]["image_url"]["url"] == pdf_data_url + def test_message_level_cache_control_folded_onto_trailing_image_block(self): messages = [ { diff --git a/tests/unit/llms/snowflake/chat/test_snowflake_chat_transformation.py b/tests/unit/llms/snowflake/chat/test_snowflake_chat_transformation.py index 36c319d9524..a31f15551e9 100644 --- a/tests/unit/llms/snowflake/chat/test_snowflake_chat_transformation.py +++ b/tests/unit/llms/snowflake/chat/test_snowflake_chat_transformation.py @@ -18,6 +18,7 @@ import pytest import litellm from litellm import completion, acompletion from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler +from litellm.llms.anthropic.messages.adapters.transformation import LiteLLMAnthropicMessagesAdapter from litellm.llms.snowflake.chat.transformation import SnowflakeConfig, SnowflakeStreamingHandler from litellm.types.utils import ModelResponse @@ -479,6 +480,35 @@ class TestSnowflakeCortexClaudeFixes: } ] + def test_messages_api_pdf_reaches_cortex_claude_as_a_document_block(self) -> None: + translated: Final = LiteLLMAnthropicMessagesAdapter().translate_anthropic_messages_to_openai( + messages=[ + { + "role": "user", + "content": [ + { + "type": "document", + "source": {"type": "base64", "media_type": "application/pdf", "data": "ZmFrZQ=="}, + "cache_control": {"type": "ephemeral"}, + }, + {"type": "text", "text": "What is the codeword?"}, + ], + } + ], + model="snowflake/claude-sonnet-4-6", + ) + + body: Final = self._transform(translated) + + assert body["messages"][0]["content"] == [ + { + "type": "document", + "source": {"type": "base64", "media_type": "application/pdf", "data": "ZmFrZQ=="}, + "cache_control": {"type": "ephemeral"}, + }, + {"type": "text", "text": "What is the codeword?"}, + ] + def test_multipart_tool_result_preserves_text_and_converts_image(self): body = self._transform( [ diff --git a/tests/unit/llms/soniox/realtime/__init__.py b/tests/unit/llms/soniox/realtime/__init__.py new file mode 100644 index 00000000000..b503dff241f --- /dev/null +++ b/tests/unit/llms/soniox/realtime/__init__.py @@ -0,0 +1 @@ +"""Soniox realtime tests.""" diff --git a/tests/unit/llms/soniox/realtime/test_soniox_realtime_transformation.py b/tests/unit/llms/soniox/realtime/test_soniox_realtime_transformation.py new file mode 100644 index 00000000000..653f73ce22c --- /dev/null +++ b/tests/unit/llms/soniox/realtime/test_soniox_realtime_transformation.py @@ -0,0 +1,807 @@ +import asyncio +import base64 +import json +from typing import Final +from unittest.mock import MagicMock + +import pytest + +from litellm.llms.soniox.common_utils import SonioxException +from litellm.llms.soniox.realtime.transformation import ( + DEFAULT_SONIOX_REALTIME_URL, + SESSION_STARTED_FRAME, + SonioxEventTransformer, + SonioxRealtimeBackend, + SonioxProtocolError, + SonioxRealtimeConfig, + SonioxRealtimeOptions, +) +from litellm.types.llms.openai import OpenAIRealtimeEvents +from litellm.types.realtime import RealtimeResponseTransformInput +from litellm.types.utils import LlmProviders +from litellm.utils import ProviderConfigManager + +MODEL: Final = "stt-rt-v5" +EMPTY_TRANSFORM_INPUT: Final[RealtimeResponseTransformInput] = { + "session_configuration_request": None, + "current_output_item_id": None, + "current_response_id": None, + "current_delta_chunks": None, + "current_item_chunks": None, + "current_conversation_id": None, + "current_delta_type": None, +} +PCM: Final = b"\x01\x02" * 160 + + +def _event(event_type: str, **fields: object) -> str: + return json.dumps({"type": event_type, **fields}) + + +def _session_update(audio_input: dict[str, object]) -> str: + return _event("session.update", session={"type": "transcription", "audio": {"input": audio_input}}) + + +def _append() -> str: + return _event("input_audio_buffer.append", audio=base64.b64encode(PCM).decode()) + + +def _start_request(frames: tuple[str | bytes, ...]) -> dict[str, object]: + assert isinstance(frames[0], str) + return json.loads(frames[0]) + + +def _response(tokens: list[dict[str, object]], total_audio_proc_ms: int | None = None) -> str: + return json.dumps({"tokens": tokens, "total_audio_proc_ms": total_audio_proc_ms}) + + +def _token(text: str, is_final: bool = True, **fields: object) -> dict[str, object]: + return {"text": text, "is_final": is_final, **fields} + + +def _types(events: tuple[OpenAIRealtimeEvents, ...]) -> list[object]: + return [event["type"] for event in events] + + +def test_session_update_starts_the_stream_with_client_audio_and_deployment_options(): + config = SonioxRealtimeConfig( + options=SonioxRealtimeOptions(language_hints=("en", "fr"), enable_speaker_diarization=True) + ) + + frames = config.transform_realtime_request( + _session_update( + { + "format": {"type": "audio/pcm", "rate": 16_000}, + "transcription": {"model": "client-chosen-model", "language": "fr"}, + "turn_detection": None, + } + ), + MODEL, + ) + + assert _start_request(frames) == { + "model": MODEL, + "audio_format": "pcm_s16le", + "sample_rate": 16_000, + "num_channels": 1, + "enable_endpoint_detection": False, + "language_hints": ["fr", "en"], + "enable_speaker_diarization": True, + } + + +def test_an_empty_transcription_language_keeps_the_deployment_language_hints(): + config = SonioxRealtimeConfig(options=SonioxRealtimeOptions(language_hints=("fa",))) + + frames = config.transform_realtime_request(_session_update({"transcription": {"language": ""}}), MODEL) + + assert _start_request(frames)["language_hints"] == ["fa"] + + +def test_a_client_language_cannot_widen_strict_deployment_language_hints(): + config = SonioxRealtimeConfig(options=SonioxRealtimeOptions(language_hints=("fa",), language_hints_strict=True)) + + frames = config.transform_realtime_request(_session_update({"transcription": {"language": "en"}}), MODEL) + + assert _start_request(frames)["language_hints"] == ["fa"] + assert config.transform_realtime_request(_session_update({"transcription": {"language": "en"}}), MODEL) == () + + +def test_a_soniox_native_config_frame_is_rejected_without_starting_the_stream(): + config = SonioxRealtimeConfig() + + with pytest.raises(SonioxProtocolError): + config.transform_realtime_request(json.dumps({"model": "stt-rt-v5", "audio_format": "auto"}), MODEL) + assert _start_request(config.transform_realtime_request(_append(), MODEL))["sample_rate"] == 24_000 + + +def test_audio_before_session_update_starts_a_default_stream_once_and_rejects_a_later_change(): + config = SonioxRealtimeConfig() + + first = config.transform_realtime_request(_append(), MODEL) + second = config.transform_realtime_request(_append(), MODEL) + with pytest.raises(SonioxProtocolError, match="once the stream has started"): + config.transform_realtime_request(_session_update({"turn_detection": None}), MODEL) + + assert _start_request(first) == { + "model": MODEL, + "audio_format": "pcm_s16le", + "sample_rate": 24_000, + "num_channels": 1, + "enable_endpoint_detection": True, + } + assert first[1:] == (PCM,) + assert second == (PCM,) + assert config.transform_realtime_request(_append(), MODEL) == (PCM,) + + +@pytest.mark.parametrize( + ("event_type", "control_frame"), + [("input_audio_buffer.commit", '{"type":"finalize"}'), ("input_audio_buffer.end", "")], +) +def test_buffer_control_events_map_to_soniox_control_frames(event_type: str, control_frame: str): + config = SonioxRealtimeConfig() + config.transform_realtime_request(_append(), MODEL) + + assert config.transform_realtime_request(_event(event_type), MODEL) == (control_frame,) + + +@pytest.mark.parametrize("event_type", ["response.create", "input_audio_buffer.clear"]) +def test_events_with_nothing_to_send_upstream_are_dropped_without_starting_the_stream(event_type: str): + config = SonioxRealtimeConfig() + + assert config.transform_realtime_request(_event(event_type), MODEL) == () + assert _start_request(config.transform_realtime_request(_append(), MODEL))["model"] == MODEL + + +def _events(config: SonioxRealtimeConfig, tokens: list[dict[str, object]]) -> tuple[OpenAIRealtimeEvents, ...]: + return tuple( + config.transform_realtime_response(_response(tokens), MODEL, MagicMock(), EMPTY_TRANSFORM_INPUT)["response"] + ) + + +def test_clear_discards_the_open_turn_and_everything_soniox_finalizes_for_it(): + config = SonioxRealtimeConfig() + config.transform_realtime_request(_append(), MODEL) + _events(config, [_token("Hel")]) + + assert config.transform_realtime_request(_event("input_audio_buffer.clear"), MODEL) == ('{"type":"finalize"}',) + cleared = _events(config, [_token("lo"), _token(""), _token("")]) + config.transform_realtime_request(_event("input_audio_buffer.commit"), MODEL) + kept = _events(config, [_token("Bye"), _token("")]) + + assert cleared == () + assert kept[-1]["transcript"] == "Bye" + + +def test_clear_after_a_commit_keeps_the_committed_turn_and_drops_only_audio_sent_after_it(): + config = SonioxRealtimeConfig() + config.transform_realtime_request(_append(), MODEL) + _events(config, [_token("One")]) + config.transform_realtime_request(_event("input_audio_buffer.commit"), MODEL) + config.transform_realtime_request(_event("input_audio_buffer.clear"), MODEL) + + committed = _events(config, [_token("")]) + cleared = _events(config, [_token(" two"), _token("")]) + later = _events(config, [_token("Three"), _token("")]) + + assert committed[-1]["transcript"] == "One" + assert cleared == () + assert later[-1]["transcript"] == "Three" + + +@pytest.mark.parametrize( + "audio_input", + [ + {"format": {"type": "audio/pcmu"}}, + {"format": {"type": "audio/pcm", "rate": 24_000, "channels": 2}}, + {"format": {"type": "audio/pcm", "rate": 0}}, + ], +) +def test_unsupported_audio_formats_are_rejected_without_starting_the_stream(audio_input: dict[str, object]): + config = SonioxRealtimeConfig() + + with pytest.raises(SonioxProtocolError): + config.transform_realtime_request(_session_update(audio_input), MODEL) + assert _start_request(config.transform_realtime_request(_append(), MODEL))["sample_rate"] == 24_000 + + +@pytest.mark.parametrize( + ("audio_input", "sample_rate"), + [({"format": {"type": "audio/pcm"}}, 24_000), ({"input_audio_format": "pcm16"}, 24_000)], +) +def test_pcm16_without_a_rate_streams_at_the_openai_default_rate(audio_input: dict[str, object], sample_rate: int): + config = SonioxRealtimeConfig() + + frames = config.transform_realtime_request(_session_update(audio_input), MODEL) + + assert _start_request(frames)["sample_rate"] == sample_rate + + +def test_validate_environment_sends_the_key_as_a_bearer_header(monkeypatch): + monkeypatch.setenv("SONIOX_API_KEY", "env-key") + config = SonioxRealtimeConfig() + + assert config.validate_environment({"x": "y"}, MODEL, "deployment-key") == { + "x": "y", + "Authorization": "Bearer deployment-key", + } + assert config.validate_environment({}, MODEL) == {"Authorization": "Bearer env-key"} + + +def test_validate_environment_rejects_a_missing_key(monkeypatch): + monkeypatch.delenv("SONIOX_API_KEY", raising=False) + + with pytest.raises(ValueError, match="SONIOX_API_KEY"): + SonioxRealtimeConfig().validate_environment({}, MODEL) + + +def test_get_complete_url_defaults_to_soniox_and_opens_a_websocket_for_http_bases(): + config = SonioxRealtimeConfig() + + assert config.get_complete_url(None, MODEL) == DEFAULT_SONIOX_REALTIME_URL + assert config.get_complete_url("wss://proxy.example/ws", MODEL) == "wss://proxy.example/ws" + assert config.get_complete_url("https://proxy.example/ws?region=eu", MODEL) == "wss://proxy.example/ws?region=eu" + assert config.get_complete_url("http://localhost:8080/ws", MODEL) == "ws://localhost:8080/ws" + with pytest.raises(ValueError, match="wss://"): + config.get_complete_url("ftp://proxy.example/ws", MODEL) + + +def test_endpoint_detection_emits_one_openai_turn_billed_by_processed_audio(): + transformer = SonioxEventTransformer(drops_originals=False) + + provisional = transformer.transform(_response([_token("Hel", is_final=False)], total_audio_proc_ms=500)) + finals = transformer.transform( + _response([_token("Hello"), _token(" world"), _token(" next", is_final=False)], total_audio_proc_ms=1500) + ) + endpoint = transformer.transform(_response([_token("")], total_audio_proc_ms=2000)) + + assert _types(provisional) == ["input_audio_buffer.speech_started"] + item_id = provisional[0]["item_id"] + assert [(event["type"], event.get("delta")) for event in finals] == [ + ("conversation.item.input_audio_transcription.delta", "Hello"), + ("conversation.item.input_audio_transcription.delta", " world"), + ] + assert _types(endpoint) == [ + "input_audio_buffer.speech_stopped", + "conversation.item.input_audio_transcription.completed", + ] + assert {event["item_id"] for event in (*finals, *endpoint)} == {item_id} + assert all("speaker" not in event and "language" not in event for event in finals) + assert endpoint[1]["transcript"] == "Hello world" + assert "segments" not in endpoint[1] + assert endpoint[1]["usage"] == {"type": "duration", "seconds": 2.0} + assert transformer.take_unbilled_usage() is None + + +def test_manual_finalization_completes_the_turn_without_speech_stopped_and_starts_a_new_item(): + transformer = SonioxEventTransformer(drops_originals=False) + + first = transformer.transform(_response([_token("One"), _token("")])) + second = transformer.transform(_response([_token("Two"), _token("")])) + + assert _types(first) == [ + "input_audio_buffer.speech_started", + "conversation.item.input_audio_transcription.delta", + "conversation.item.input_audio_transcription.completed", + ] + assert first[-1]["transcript"] == "One" + assert "usage" not in first[-1] + assert second[-1]["transcript"] == "Two" + assert second[0]["item_id"] != first[0]["item_id"] + + +def test_a_commit_without_speech_answers_with_an_empty_turn_billed_for_the_silence(): + config = SonioxRealtimeConfig() + config.transform_realtime_request(_append(), MODEL) + config.transform_realtime_request(_event("input_audio_buffer.commit"), MODEL) + + events = tuple( + config.transform_realtime_response( + _response([_token("")], total_audio_proc_ms=2000), MODEL, MagicMock(), EMPTY_TRANSFORM_INPUT + )["response"] + ) + + assert _types(events) == ["conversation.item.input_audio_transcription.completed"] + assert events[0]["transcript"] == "" + assert events[0]["usage"] == {"type": "duration", "seconds": 2.0} + assert _events(config, [_token("Next"), _token("")])[0]["item_id"] != events[0]["item_id"] + + +def test_a_commit_soniox_answers_after_its_endpoint_already_ended_the_turn_adds_no_second_turn(): + config = SonioxRealtimeConfig() + config.transform_realtime_request(_append(), MODEL) + _events(config, [_token("Hello")]) + config.transform_realtime_request(_event("input_audio_buffer.commit"), MODEL) + + events = _events(config, [_token(""), _token("")]) + + assert [event["transcript"] for event in events if "transcript" in event] == ["Hello"] + + +def test_a_server_vad_turn_answers_only_the_oldest_pending_commit(): + config = SonioxRealtimeConfig() + config.transform_realtime_request(_append(), MODEL) + _events(config, [_token("Hello")]) + config.transform_realtime_request(_event("input_audio_buffer.commit"), MODEL) + config.transform_realtime_request(_append(), MODEL) + config.transform_realtime_request(_event("input_audio_buffer.commit"), MODEL) + + events = _events(config, [_token(""), _token(""), _token("")]) + + assert [event["transcript"] for event in events if "transcript" in event] == ["Hello", ""] + + +def test_a_clear_without_speech_still_answers_nothing(): + config = SonioxRealtimeConfig() + config.transform_realtime_request(_append(), MODEL) + config.transform_realtime_request(_event("input_audio_buffer.clear"), MODEL) + + assert _events(config, [_token("")]) == () + + +def test_translation_sessions_transcribe_only_the_translated_tokens(): + transformer = SonioxEventTransformer(drops_originals=True) + + events = transformer.transform( + _response( + [ + _token("Hola", translation_status="original"), + _token("Hello", translation_status="translation"), + _token(""), + ] + ) + ) + + assert [event.get("delta") for event in events if "delta" in event] == ["Hello"] + assert events[-1]["transcript"] == "Hello" + + +def test_translation_sessions_keep_speech_soniox_leaves_untranslated(): + transformer = SonioxEventTransformer(drops_originals=True) + + events = transformer.transform( + _response( + [_token("Hola", translation_status="none"), _token(" amigos", translation_status="none"), _token("")] + ) + ) + + assert events[-1]["transcript"] == "Hola amigos" + + +def test_diarized_tokens_label_each_delta_and_split_the_completed_turn_into_speaker_segments(): + transformer = SonioxEventTransformer(drops_originals=False) + + events = transformer.transform( + _response( + [ + _token("Hi", speaker="1", language="en"), + _token(" doctor", speaker="1", language="en"), + _token(" Salam", speaker="2", language="fa"), + _token(" Hi", speaker="1", language="en"), + _token(""), + ] + ) + ) + + assert [(event["delta"], event["speaker"], event["language"]) for event in events if "delta" in event] == [ + ("Hi", "1", "en"), + (" doctor", "1", "en"), + (" Salam", "2", "fa"), + (" Hi", "1", "en"), + ] + assert events[-1]["transcript"] == "Hi doctor Salam Hi" + assert events[-1]["segments"] == ( + {"speaker": "1", "language": "en", "text": "Hi doctor"}, + {"speaker": "2", "language": "fa", "text": "Salam"}, + {"speaker": "1", "language": "en", "text": "Hi"}, + ) + + +def test_translation_sessions_announce_speech_on_the_first_spoken_token_before_any_translation(): + transformer = SonioxEventTransformer(drops_originals=True) + + events = transformer.transform(_response([_token("Hol", is_final=False, translation_status="original")])) + + assert _types(events) == ["input_audio_buffer.speech_started"] + + +def test_provider_errors_surface_as_openai_error_events(): + transformer = SonioxEventTransformer(drops_originals=False) + + events = transformer.transform(json.dumps({"error_code": 401, "error_message": "Invalid API key."})) + + assert events == ( + {"type": "error", "error": {"type": "server_error", "message": "Soniox realtime error 401: Invalid API key."}}, + ) + + +def test_malformed_provider_frames_are_rejected(): + with pytest.raises(SonioxProtocolError): + SonioxEventTransformer(drops_originals=False).transform('{"tokens": "nope"}') + + +def test_audio_processed_after_the_last_turn_is_billed_on_session_close(): + config = SonioxRealtimeConfig() + config.transform_realtime_response( + _response([_token("Hi"), _token("")], total_audio_proc_ms=1000), MODEL, MagicMock(), EMPTY_TRANSFORM_INPUT + ) + config.transform_realtime_response( + _response([], total_audio_proc_ms=3500), MODEL, MagicMock(), EMPTY_TRANSFORM_INPUT + ) + + assert config.unbilled_usage_on_session_close(MODEL) == {"type": "duration", "seconds": 2.5} + assert config.unbilled_usage_on_session_close(MODEL) is None + + +def test_translation_option_switches_the_config_to_translated_transcripts(): + config = SonioxRealtimeConfig( + options=SonioxRealtimeOptions(translation={"type": "one_way", "target_language": "en"}) + ) + + result = config.transform_realtime_response( + _response([_token("Hola", translation_status="original"), _token("Hello", translation_status="translation")]), + MODEL, + MagicMock(), + EMPTY_TRANSFORM_INPUT, + ) + + assert [event.get("delta") for event in result["response"] if "delta" in event] == ["Hello"] + + +def test_stream_time_since_the_start_request_is_billed_on_close_because_soniox_charges_the_whole_stream(): + now = [100.0] + config = SonioxRealtimeConfig(clock=lambda: now[0]) + config.wrap_backend(_RecordingBackend()) + now[0] = 110.0 + config.transform_realtime_request(_append(), MODEL) + config.transform_realtime_response( + _response([_token("Hi"), _token("")], total_audio_proc_ms=5000), MODEL, MagicMock(), EMPTY_TRANSFORM_INPUT + ) + now[0] = 130.5 + + assert config.unbilled_usage_on_session_close(MODEL) == {"type": "duration", "seconds": 15.5} + assert config.unbilled_usage_on_session_close(MODEL) is None + + +def _refusal_after(processed_ms: int, error_code: int = 401) -> SonioxRealtimeConfig: + now = [100.0] + config = SonioxRealtimeConfig(clock=lambda: now[0]) + config.transform_realtime_request(_append(), MODEL) + if processed_ms: + config.transform_realtime_response( + _response([_token("Hi")], total_audio_proc_ms=processed_ms), MODEL, MagicMock(), EMPTY_TRANSFORM_INPUT + ) + config.transform_realtime_response( + json.dumps({"error_code": error_code, "error_message": "Soniox error.", "total_audio_proc_ms": processed_ms}), + MODEL, + MagicMock(), + EMPTY_TRANSFORM_INPUT, + ) + now[0] = 103.0 + return config + + +@pytest.mark.parametrize("error_code", [400, 401, 402, 403, 408, 429, 500, 503]) +def test_an_error_before_any_audio_is_processed_is_a_refused_session_that_bills_nothing(error_code: int): + config = _refusal_after(processed_ms=0, error_code=error_code) + + refusal = config.session_refusal() + assert isinstance(refusal, SonioxException) + assert refusal.status_code == error_code + assert config.unbilled_usage_on_session_close(MODEL) is None + + +def test_an_error_after_audio_was_processed_keeps_the_session_a_billed_success(): + config = _refusal_after(processed_ms=1500) + + assert config.session_refusal() is None + assert config.unbilled_usage_on_session_close(MODEL) == {"type": "duration", "seconds": 3.0} + + +def test_a_connection_that_never_started_a_stream_bills_nothing(): + now = [100.0] + config = SonioxRealtimeConfig(clock=lambda: now[0]) + config.wrap_backend(_RecordingBackend()) + now[0] = 160.0 + + assert config.unbilled_usage_on_session_close(MODEL) is None + + +def test_soniox_opts_out_of_deferred_setup_so_client_frames_reach_it_in_order(): + assert SonioxRealtimeConfig().requires_session_configuration() + + +def test_a_session_update_matching_the_started_stream_is_a_no_op(): + config = SonioxRealtimeConfig() + config.transform_realtime_request(_append(), MODEL) + + assert ( + config.transform_realtime_request(_session_update({"format": {"type": "audio/pcm", "rate": 24_000}}), MODEL) + == () + ) + + +def _stream_started_at_16khz_in_french_without_vad() -> SonioxRealtimeConfig: + config = SonioxRealtimeConfig() + config.transform_realtime_request( + _session_update( + { + "format": {"type": "audio/pcm", "rate": 16_000}, + "transcription": {"language": "fr"}, + "turn_detection": None, + } + ), + MODEL, + ) + return config + + +@pytest.mark.parametrize( + "follow_up", + [ + {"transcription": {"language": "fr"}}, + {"format": {"type": "audio/pcm", "rate": 16_000}}, + {"format": {"type": "audio/pcm"}}, + {"turn_detection": None}, + ], +) +def test_a_partial_follow_up_update_that_restates_the_stream_is_a_no_op(follow_up: dict[str, object]): + config = _stream_started_at_16khz_in_french_without_vad() + + assert config.transform_realtime_request(_session_update(follow_up), MODEL) == () + + +def test_a_follow_up_update_with_an_unsupported_encoding_is_still_rejected(): + config = _stream_started_at_16khz_in_french_without_vad() + + with pytest.raises(SonioxProtocolError, match="requires pcm16"): + config.transform_realtime_request(_session_update({"format": {"type": "audio/pcmu"}}), MODEL) + + +@pytest.mark.parametrize( + "follow_up", + [ + {"transcription": {"language": "de"}}, + {"format": {"type": "audio/pcm", "rate": 24_000}}, + {"turn_detection": {"type": "server_vad"}}, + ], +) +def test_a_partial_follow_up_update_that_changes_a_sent_setting_is_rejected(follow_up: dict[str, object]): + config = _stream_started_at_16khz_in_french_without_vad() + + with pytest.raises(SonioxProtocolError, match="once the stream has started"): + config.transform_realtime_request(_session_update(follow_up), MODEL) + + +def test_session_created_event_advertises_the_deployment_model_for_cost_tracking(): + config = SonioxRealtimeConfig(options=SonioxRealtimeOptions(language_hints=("de",))) + + event = config.transform_session_created_event(MODEL, "session_1") + + assert event["session"]["id"] == "session_1" + assert event["session"]["audio"]["input"]["transcription"] == {"model": MODEL, "language": "de"} + + +def test_backend_session_start_is_announced_with_the_negotiated_session(): + config = SonioxRealtimeConfig() + config.transform_session_created_event(MODEL, "session_1") + config.transform_realtime_request( + _session_update( + { + "format": {"type": "audio/pcm", "rate": 16_000}, + "transcription": {"language": "it"}, + "turn_detection": None, + } + ), + MODEL, + ) + + result = config.transform_realtime_response(SESSION_STARTED_FRAME, MODEL, MagicMock(), EMPTY_TRANSFORM_INPUT) + + assert _types(tuple(result["response"])) == ["session.created"] + assert result["response"][0]["session"]["id"] == "session_1" + assert result["response"][0]["session"]["audio"]["input"] == { + "format": {"type": "audio/pcm", "rate": 16_000}, + "transcription": {"model": MODEL, "language": "it"}, + "turn_detection": None, + } + + +def test_provider_config_manager_passes_only_soniox_options_from_deployment_params(): + config = ProviderConfigManager.get_provider_realtime_config( + MODEL, + LlmProviders.SONIOX, + litellm_params={ + "api_key": "secret", + "model": "soniox/stt-rt-v5", + "audio_format": "mulaw", + "language_hints": ["pl"], + "context": {"terms": ["LiteLLM"]}, + "max_endpoint_delay_ms": 800, + }, + ) + + assert isinstance(config, SonioxRealtimeConfig) + assert _start_request(config.transform_realtime_request(_append(), MODEL)) == { + "model": MODEL, + "audio_format": "pcm_s16le", + "sample_rate": 24_000, + "num_channels": 1, + "enable_endpoint_detection": True, + "language_hints": ["pl"], + "context": {"terms": ["LiteLLM"]}, + "max_endpoint_delay_ms": 800, + } + + +SEMANTIC_VAD: Final = {"turn_detection": {"type": "semantic_vad", "eagerness": "high"}} + + +def _soniox_config(drop_params: bool) -> SonioxRealtimeConfig: + config = ProviderConfigManager.get_provider_realtime_config(MODEL, LlmProviders.SONIOX, drop_params=drop_params) + assert isinstance(config, SonioxRealtimeConfig) + return config + + +def test_unsupported_turn_detection_is_rejected_without_drop_params(): + config = _soniox_config(drop_params=False) + + with pytest.raises(SonioxProtocolError, match="Set drop_params to use server_vad instead"): + config.transform_realtime_request(_session_update(SEMANTIC_VAD), MODEL) + + +def test_unsupported_turn_detection_falls_back_to_endpoint_detection_with_drop_params(): + config = _soniox_config(drop_params=True) + + frames = config.transform_realtime_request(_session_update(SEMANTIC_VAD), MODEL) + + assert _start_request(frames)["enable_endpoint_detection"] is True + assert config.transform_realtime_request(_session_update(SEMANTIC_VAD), MODEL) == () + + +def test_a_follow_up_with_unsupported_turn_detection_is_rejected_without_drop_params(): + config = _soniox_config(drop_params=False) + config.transform_realtime_request(_append(), MODEL) + + with pytest.raises(SonioxProtocolError, match="Set drop_params to use server_vad instead"): + config.transform_realtime_request(_session_update(SEMANTIC_VAD), MODEL) + + +class _RecordingBackend: + def __init__(self) -> None: + self.sent: Final[list[str | bytes]] = [] + self.upstream: Final[asyncio.Queue[str | Exception]] = asyncio.Queue() + self.entered = False + self.exited = False + self.closed = False + + async def __aenter__(self) -> "_RecordingBackend": + self.entered = True + return self + + async def __aexit__(self, exc_type, exc_value, traceback) -> None: + self.exited = True + + async def send(self, message: str | bytes) -> None: + self.sent.append(message) + + async def recv(self, decode: bool | None = None) -> str | bytes: + frame = await self.upstream.get() + if isinstance(frame, Exception): + raise frame + return frame + + async def close(self) -> None: + self.closed = True + + +class _BrokenBackend(_RecordingBackend): + async def send(self, message: str | bytes) -> None: + raise ConnectionError("upstream gone") + + +@pytest.mark.asyncio +async def test_keepalive_is_sent_only_while_the_client_is_idle_and_stops_after_end_of_stream(): + now = [0.0] + ticks: asyncio.Queue[float] = asyncio.Queue() + + async def sleep(_: float) -> None: + now[0] = await ticks.get() + + async def tick(at: float) -> None: + await ticks.put(at) + for _ in range(5): + await asyncio.sleep(0) + + inner = _RecordingBackend() + backend = SonioxRealtimeBackend(inner, keepalive_interval=5.0, clock=lambda: now[0], sleep=sleep) + + async with backend: + await tick(5.0) + assert inner.sent == ['{"type":"keepalive"}'] + now[0] = 7.0 + await backend.send(PCM) + await tick(10.0) + assert inner.sent == ['{"type":"keepalive"}', PCM] + await backend.send("") + await tick(30.0) + assert inner.sent == ['{"type":"keepalive"}', PCM, ""] + + assert inner.entered and inner.exited + + +@pytest.mark.asyncio +async def test_backend_announces_the_session_once_the_start_request_is_sent_while_relaying_upstream_frames(): + inner = _RecordingBackend() + backend = SonioxRealtimeBackend(inner) + + async with backend: + pending = asyncio.create_task(backend.recv()) + await backend.send('{"type":"keepalive"}') + await asyncio.sleep(0) + assert not pending.done() + await backend.send('{"model":"stt-rt-v5"}') + assert await pending == SESSION_STARTED_FRAME + await backend.send('{"model":"ignored-second-config"}') + await inner.upstream.put('{"tokens":[]}') + assert await backend.recv() == '{"tokens":[]}' + + +@pytest.mark.asyncio +async def test_backend_relays_upstream_frames_that_arrive_before_the_session_starts(): + inner = _RecordingBackend() + + async with SonioxRealtimeBackend(inner) as backend: + await inner.upstream.put('{"error_code":401,"error_message":"Invalid API key."}') + + assert await backend.recv() == '{"error_code":401,"error_message":"Invalid API key."}' + + +@pytest.mark.asyncio +async def test_a_cancelled_read_loses_no_frames_and_upstream_errors_reach_the_reader(): + inner = _RecordingBackend() + + async with SonioxRealtimeBackend(inner) as backend: + cancelled = asyncio.create_task(backend.recv()) + await asyncio.sleep(0) + cancelled.cancel() + await asyncio.gather(cancelled, return_exceptions=True) + await inner.upstream.put('{"tokens":[]}') + await inner.upstream.put(ConnectionError("upstream closed")) + + assert await backend.recv() == '{"tokens":[]}' + with pytest.raises(ConnectionError, match="upstream closed"): + await backend.recv() + + +@pytest.mark.asyncio +async def test_closing_the_backend_cancels_pending_reads_and_closes_upstream(): + inner = _RecordingBackend() + backend = SonioxRealtimeBackend(inner) + await backend.__aenter__() + pending = asyncio.create_task(backend.recv()) + await backend.send('{"model":"stt-rt-v5"}') + assert await pending == SESSION_STARTED_FRAME + + await backend.close() + await inner.upstream.put('{"tokens":[]}') + await asyncio.sleep(0) + + assert inner.closed + assert inner.upstream.qsize() == 1 + + +@pytest.mark.asyncio +async def test_keepalive_stops_after_the_upstream_rejects_a_send(): + sleeps: list[float] = [] + + async def sleep(seconds: float) -> None: + sleeps.append(seconds) + await asyncio.sleep(0) + + async with SonioxRealtimeBackend(_BrokenBackend(), keepalive_interval=0.0, sleep=sleep): + for _ in range(5): + await asyncio.sleep(0) + + assert sleeps == [0.0] diff --git a/tests/unit/llms/test_get_complete_url_trailing_slash.py b/tests/unit/llms/test_get_complete_url_trailing_slash.py index cd778c20d9b..43a74e56028 100644 --- a/tests/unit/llms/test_get_complete_url_trailing_slash.py +++ b/tests/unit/llms/test_get_complete_url_trailing_slash.py @@ -143,6 +143,7 @@ API_BASE_IS_THE_FULL_URL: Final[Mapping[str, str]] = MappingProxyType( "duckduckgo.search.transformation.DuckDuckGoSearchConfig", "google_pse.search.transformation.GooglePSESearchConfig", "searchapi.search.transformation.SearchAPIConfig", + "soniox.realtime.transformation.SonioxRealtimeConfig", "tinyfish.search.transformation.TinyfishSearchConfig", "vertex_ai.text_to_speech.transformation.VertexAITextToSpeechConfig", "vertex_ai.vertex_ai_partner_models.anthropic.messages.transformation." diff --git a/tests/unit/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_messages_config.py b/tests/unit/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_messages_config.py index 5916ce9f7a1..1e7c3d52814 100644 --- a/tests/unit/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_messages_config.py +++ b/tests/unit/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_messages_config.py @@ -289,7 +289,7 @@ def test_no_safeguards_leaves_dangerous_tool_use_beta_header_out(): assert "dangerous-tool-use-2026-09-03" not in updated_headers.get("anthropic-beta", "") -def _validate_vertex_headers(client_headers, messages): +def _validate_vertex_headers(client_headers, messages, thinking: Mapping[str, object] | None = None): config = VertexAIPartnerModelsAnthropicMessagesConfig() litellm_params = { "vertex_ai_project": "test-project", @@ -305,7 +305,7 @@ def _validate_vertex_headers(client_headers, messages): headers=client_headers, model="claude-opus-5-5", messages=messages, - optional_params={"max_tokens": 64}, + optional_params={"max_tokens": 64, **({"thinking": thinking} if thinking is not None else {})}, litellm_params=litellm_params, api_base=None, ) @@ -337,6 +337,25 @@ def test_per_message_output_config_reaches_vertex_with_per_turn_control_beta(cli assert filtered["anthropic-beta"].split(",").count("per-turn-control-2026-07-01") == 1 +def test_block_binding_reaches_vertex_messages_with_its_beta_under_drop_params(local_beta_headers_config: None) -> None: + from litellm.anthropic_beta_headers_manager import update_headers_with_filtered_beta + + thinking: Final = {"type": "adaptive", "block_binding": {"prefix_mismatch_behavior": "error"}} + messages: Final = [{"role": "user", "content": "Say hi in French"}] + headers: Final = update_headers_with_filtered_beta( + headers=_validate_vertex_headers({}, messages, thinking=thinking), provider="vertex_ai" + ) + request: Final = VertexAIPartnerModelsAnthropicMessagesConfig().transform_anthropic_messages_request( + model="claude-opus-5-5", + messages=[dict(message) for message in messages], + anthropic_messages_optional_request_params={"max_tokens": 64, "thinking": dict(thinking)}, + litellm_params=GenericLiteLLMParams(drop_params=True), + headers=headers, + ) + assert "thinking-binding-controls-2026-08-01" in headers["anthropic-beta"].split(",") + assert request["thinking"] == thinking + + def test_no_per_message_output_config_leaves_per_turn_control_beta_out(): headers = _validate_vertex_headers({}, [{"role": "user", "content": "Hello"}]) @@ -913,6 +932,20 @@ class TestVertexAnthropicMidConversationSystem: {"type": "text", "text": "Cite sources."}, ] + @pytest.mark.parametrize("drop_params", (False, True)) + def test_unsupported_model_sends_a_non_object_message_on_for_the_provider_to_reject( + self, local_model_cost_map, local_beta_headers_config, drop_params + ): + messages: Final = [{"role": "user", "content": "hi"}, "oops"] + result: Final = VertexAIPartnerModelsAnthropicMessagesConfig().transform_anthropic_messages_request( + model="claude-sonnet-4-6", + messages=list(messages), + anthropic_messages_optional_request_params={"max_tokens": 64}, + litellm_params=GenericLiteLLMParams(drop_params=drop_params), + headers={}, + ) + assert result["messages"] == messages + def test_unsupported_model_converts_mid_conversation_system_in_place(self, local_model_cost_map): messages = [ {"role": "user", "content": "read the file"}, diff --git a/tests/unit/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_transformation.py b/tests/unit/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_transformation.py index 1c432ec79da..3ae208bb3a8 100644 --- a/tests/unit/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_transformation.py +++ b/tests/unit/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_transformation.py @@ -155,6 +155,25 @@ def test_vertex_ai_anthropic_web_search_header_in_completion(): ), "anthropic-beta with web-search should not be present for non-Vertex requests" +def test_vertex_ai_anthropic_chat_adds_the_betas_for_bound_thinking_with_display_updates() -> None: + thinking: Final = { + "type": "adaptive", + "block_binding": {"prefix_mismatch_behavior": "error"}, + "display": "updates", + } + result: Final = VertexAIAnthropicConfig().transform_request( + model="claude-opus-4-8", + messages=[{"role": "user", "content": "Hello"}], + optional_params={"thinking": thinking, "max_tokens": 100, "is_vertex_request": True}, + litellm_params={}, + headers={}, + ) + assert result["thinking"] == thinking + assert {"thinking-binding-controls-2026-08-01", "thinking-display-updates-2026-08-18"} <= set( + result["anthropic_beta"] + ) + + def test_vertex_ai_anthropic_context_management_compact_beta_header(): """Test that context_management with compact adds the correct beta header for Vertex AI""" config = VertexAIAnthropicConfig() diff --git a/tests/unit/llms/vertex_ai/vertex_ai_partner_models/gemma/test_vertex_ai_gemma_global_endpoint.py b/tests/unit/llms/vertex_ai/vertex_ai_partner_models/gemma/test_vertex_ai_gemma_global_endpoint.py index 85a2124ab02..98d0d314977 100644 --- a/tests/unit/llms/vertex_ai/vertex_ai_partner_models/gemma/test_vertex_ai_gemma_global_endpoint.py +++ b/tests/unit/llms/vertex_ai/vertex_ai_partner_models/gemma/test_vertex_ai_gemma_global_endpoint.py @@ -13,6 +13,7 @@ These tests verify that: import json import os +from typing import Final from unittest.mock import AsyncMock, MagicMock, patch import pytest @@ -422,3 +423,52 @@ async def test_vertex_ai_gemma_vision_passthrough(): assert isinstance(content, list), f"Expected list content, got: {content}" image_parts = [p for p in content if p.get("type") == "image_url"] assert image_parts, f"No image_url part in forwarded message content: {content}" + + +@pytest.mark.asyncio +async def test_vertex_ai_gemma_pdf_file_part_is_sent_as_image_url() -> None: + pdf_data_url: Final = "data:application/pdf;base64,JVBERi0xLjQKJcfsj6IKMSAwIG9iago8PD4+CmVuZG9iagp0cmFpbGVyCjw8Pj4KJSVFT0YK" + messages: Final = [ + { + "role": "user", + "content": [ + {"type": "text", "text": "What does the PDF say?"}, + {"type": "file", "file": {"file_data": pdf_data_url, "filename": "report.pdf"}}, + ], + } + ] + + mock_response: Final = MagicMock() + mock_response.status_code = 200 + mock_response.headers = {} + mock_response.json.return_value = _MOCK_RESPONSE_JSON + + mock_vertexai: Final = MagicMock() + mock_vertexai.preview = MagicMock() + + with ( + patch("litellm.llms.custom_httpx.http_handler.AsyncHTTPHandler") as mock_http_handler, + patch( + "litellm.llms.vertex_ai.vertex_ai_partner_models.main.VertexAIPartnerModels._ensure_access_token", + return_value=("fake-token", "test-project"), + ), + patch.dict( + "sys.modules", + {"vertexai": mock_vertexai, "vertexai.preview": mock_vertexai.preview}, + ), + patch.dict(litellm.model_cost, _GEMMA_MODEL_COST_ENTRY, clear=False), + ): + mock_http_handler.return_value.post = AsyncMock(return_value=mock_response) + + await litellm.acompletion( + model="vertex_ai/google/gemma-4-26b-a4b-it-maas", + messages=messages, + vertex_ai_project="test-project", + ) + + body: Final = json.loads(mock_http_handler.return_value.post.call_args.kwargs["data"]) + user_msg: Final = next(m for m in body["messages"] if m["role"] == "user") + assert user_msg["content"] == [ + {"type": "text", "text": "What does the PDF say?"}, + {"type": "image_url", "image_url": {"url": pdf_data_url}}, + ] diff --git a/tests/unit/proxy/anthropic_endpoints/test_endpoints.py b/tests/unit/proxy/anthropic_endpoints/test_endpoints.py index 037b0a6c29a..e3e625be975 100644 --- a/tests/unit/proxy/anthropic_endpoints/test_endpoints.py +++ b/tests/unit/proxy/anthropic_endpoints/test_endpoints.py @@ -5,18 +5,25 @@ Test for anthropic_endpoints/endpoints.py, focusing on handling dictionary objec import json import logging import unittest -from collections.abc import Callable, Mapping +from collections.abc import AsyncGenerator, AsyncIterator, Callable, Mapping from typing import Final from unittest.mock import AsyncMock, MagicMock, patch import httpx import pytest +from fastapi import Request, Response +from fastapi.responses import StreamingResponse from fastapi.testclient import TestClient +from starlette.types import Message from litellm import Router from litellm._logging import verbose_proxy_logger +from litellm.caching.dual_cache import DualCache +from litellm.integrations.custom_guardrail import CustomGuardrail +from litellm.proxy._types import UserAPIKeyAuth from litellm.proxy.common_request_processing import ProxyBaseLLMRequestProcessing -from litellm.types.utils import TokenCountResponse +from litellm.types.guardrails import GuardrailEventHooks +from litellm.types.utils import ModelResponseStream, TokenCountResponse class TestAnthropicEndpoints(unittest.TestCase): @@ -70,11 +77,76 @@ class TestAnthropicEndpoints(unittest.TestCase): assert mock_safe_dumps.call_count == 2 # Called twice, once for each dict object +_BLOCK_CALL_ID: Final = "messages-block" +_BLOCK_TEXT: Final = "blocked by guardrail" + + +class _BlockEveryRequest(CustomGuardrail): + async def async_pre_call_hook( + self, user_api_key_dict: UserAPIKeyAuth, cache: DualCache, data: dict[str, object], call_type: str + ) -> None: + self.raise_passthrough_exception(violation_message=_BLOCK_TEXT, request_data=data) + + +class _RewriteEveryStreamedReply(CustomGuardrail): + async def async_post_call_streaming_iterator_hook( + self, user_api_key_dict: UserAPIKeyAuth, response: AsyncIterator[object], request_data: dict[str, object] + ) -> AsyncGenerator[ModelResponseStream, None]: + async for _ in response: + yield ModelResponseStream(choices=[]) + + +def _messages_request(body: Mapping[str, object]) -> Request: + async def receive() -> Message: + return {"type": "http.request", "body": json.dumps(body).encode(), "more_body": False} + + return Request( + { + "type": "http", + "method": "POST", + "path": "/v1/messages", + "query_string": b"", + "headers": [(b"content-type", b"application/json"), (b"x-litellm-call-id", _BLOCK_CALL_ID.encode())], + }, + receive, + ) + + +async def _blocked_messages_reply( + body: Mapping[str, object], fastapi_response: Response, rails: tuple[CustomGuardrail, ...] +) -> tuple[object, str]: + import litellm + import litellm.proxy.anthropic_endpoints.endpoints as ep + import litellm.proxy.proxy_server as proxy_server + from litellm.proxy.utils import ProxyLogging + + blocker: Final = _BlockEveryRequest( + guardrail_name="deny-list", event_hook=GuardrailEventHooks.pre_call, default_on=True + ) + with ( + patch.object( + proxy_server, "proxy_logging_obj", new=ProxyLogging(user_api_key_cache=DualCache()) + ), # test-quality-ok: module global imported at call time; no injection seam + patch.object(litellm, "callbacks", new=[blocker, *rails]), # test-quality-ok: guardrails register through this global + ): + response = await ep.anthropic_response( + fastapi_response=fastapi_response, + request=_messages_request(body), + user_api_key_dict=UserAPIKeyAuth(), + ) + if not isinstance(response, StreamingResponse): + return response, "" + streamed: Final = b"".join( + [chunk if isinstance(chunk, bytes) else chunk.encode() async for chunk in response.body_iterator] + ).decode() + return response, streamed + + class TestBlockedResponseUsage: """Blocked responses report the blocked LLM response's real usage.""" def test_uses_original_response_usage(self): - from litellm.proxy.anthropic_endpoints.endpoints import _blocked_response_usage + from litellm.llms.base_llm.guardrail_translation.utils import blocked_response_usage as _blocked_response_usage # original_response is the AnthropicMessagesResponse the LLM produced # before the guardrail blocked it; its usage is real. @@ -85,7 +157,7 @@ class TestBlockedResponseUsage: } def test_zero_usage_when_no_original_response(self): - from litellm.proxy.anthropic_endpoints.endpoints import _blocked_response_usage + from litellm.llms.base_llm.guardrail_translation.utils import blocked_response_usage as _blocked_response_usage # Pre-call blocks never invoked the LLM -> nothing consumed. assert _blocked_response_usage(None) == { @@ -131,6 +203,49 @@ class TestBlockedResponseUsage: assert response["usage"] == {"input_tokens": 12, "output_tokens": 5} mock_logging.post_call_failure_hook.assert_awaited_once() + @pytest.mark.asyncio + @pytest.mark.parametrize( + "rails", + ((), (_RewriteEveryStreamedReply(guardrail_name="post-call-rewrite", default_on=True),)), + ids=("no_rails", "post_call_rail"), + ) + async def test_streaming_block_answers_anthropic_events_with_the_call_id( + self, rails: tuple[CustomGuardrail, ...] + ) -> None: + response, streamed = await _blocked_messages_reply( + {"model": "claude-haiku-5-5", "stream": True}, Response(), rails + ) + + frames: Final = tuple(frame for frame in streamed.split("\n\n") if frame.strip()) + events: Final = tuple( + (frame.split("\n")[0].removeprefix("event: "), json.loads(frame.split("\n")[1].removeprefix("data: "))) + for frame in frames + ) + assert isinstance(response, StreamingResponse) + assert response.media_type == "text/event-stream" + assert response.headers["x-litellm-call-id"] == _BLOCK_CALL_ID + assert [name for name, _ in events] == [ + "message_start", + "content_block_start", + "content_block_delta", + "content_block_stop", + "message_delta", + "message_stop", + ] + assert all(payload["type"] == name for name, payload in events) + assert events[2][1]["delta"] == {"type": "text_delta", "text": _BLOCK_TEXT} + assert events[4][1]["delta"]["stop_reason"] == "end_turn" + + @pytest.mark.asyncio + async def test_json_block_answers_with_the_call_id(self) -> None: + fastapi_response: Final = Response() + + response, _ = await _blocked_messages_reply({"model": "claude-haiku-5-5"}, fastapi_response, ()) + + assert fastapi_response.headers["x-litellm-call-id"] == _BLOCK_CALL_ID + assert isinstance(response, Mapping) + assert response["content"] == [{"type": "text", "text": _BLOCK_TEXT}] + class TestProxyExceptionAnthropicEnvelope: @pytest.mark.asyncio diff --git a/tests/unit/proxy/common_utils/test_callback_utils.py b/tests/unit/proxy/common_utils/test_callback_utils.py index ecb2375d495..c8d909ac2eb 100644 --- a/tests/unit/proxy/common_utils/test_callback_utils.py +++ b/tests/unit/proxy/common_utils/test_callback_utils.py @@ -49,6 +49,19 @@ def test_get_remaining_tokens_and_requests_from_request_data(): } +@pytest.mark.parametrize("client_model_group", [7, ["embed-only"], {"name": "embed-only"}]) +def test_remaining_limit_headers_ignore_a_model_group_that_is_not_a_string(client_model_group: object): + casedata: Final = { + "metadata": { + "model_group": client_model_group, + f"litellm-key-remaining-requests-{client_model_group}": 100, + f"litellm-key-remaining-tokens-{client_model_group}": 200, + } + } + + assert get_remaining_tokens_and_requests_from_request_data(casedata) == {} + + @patch( "litellm.proxy.common_utils.callback_utils.CustomLogger.get_callback_env_vars", return_value=["API_KEY", "MISSING_VAR"], diff --git a/tests/unit/proxy/db/test_prisma_client.py b/tests/unit/proxy/db/test_prisma_client.py index 4b776bfbb1a..6c82628576f 100644 --- a/tests/unit/proxy/db/test_prisma_client.py +++ b/tests/unit/proxy/db/test_prisma_client.py @@ -586,3 +586,19 @@ async def test_a_query_issued_while_the_last_one_finishes_keeps_the_engine_drain await second await asyncio.wait_for(drained, timeout=1) assert engine.queries_finished == 2 + + +def test_migration_dry_run_summary_without_migrations_has_nothing_to_dry_run(unset_database_url): + """`prisma db push` applies no migration files, so the dry run reports that instead of connecting.""" + from litellm.proxy.db.prisma_client import PrismaManager + + assert PrismaManager.migration_dry_run_summary(use_migrate=False) == ( + True, + "The schema is pushed rather than migrated, there is nothing to dry-run.", + ) + + +def test_migration_dry_run_summary_needs_database_url(unset_database_url): + from litellm.proxy.db.prisma_client import PrismaManager + + assert PrismaManager.migration_dry_run_summary(use_migrate=True) == (False, "DATABASE_URL is not set.") diff --git a/tests/unit/proxy/guardrails/guardrail_hooks/content_filter/test_content_filter.py b/tests/unit/proxy/guardrails/guardrail_hooks/content_filter/test_content_filter.py index f7fb7e1fed0..9a14222c684 100644 --- a/tests/unit/proxy/guardrails/guardrail_hooks/content_filter/test_content_filter.py +++ b/tests/unit/proxy/guardrails/guardrail_hooks/content_filter/test_content_filter.py @@ -4,6 +4,7 @@ Tests for the Content Filter Guardrail import json import os +from collections.abc import AsyncGenerator, AsyncIterator, Callable from typing import Final from unittest.mock import MagicMock @@ -30,7 +31,17 @@ from litellm.types.proxy.guardrails.guardrail_hooks.litellm_content_filter impor ContentFilterCategoryConfig, ContentFilterDetection, ) -from litellm.types.utils import StandardLoggingGuardrailInformation +from litellm.llms.base_llm.guardrail_translation.utils import stream_item_field +from litellm.types.llms.openai import OutputTextDeltaEvent, ResponsesAPIStreamEvents +from litellm.types.utils import ( + Delta, + ModelResponseStream, + StandardLoggingGuardrailInformation, + StreamingChoices, + TextChoices, + TextCompletionResponse, + Usage, +) class TestContentFilterGuardrail: @@ -3523,3 +3534,180 @@ async def test_image_whose_description_is_clean_passes_through_unchanged(): ) assert result == {"images": ["https://example.com/desk.png"], "texts": []} + + +def _fruit_word_filter() -> ContentFilterGuardrail: + return ContentFilterGuardrail( + guardrail_name="fruit-filter", + blocked_words=[ + BlockedWord(keyword="persimmon", action=ContentFilterAction.MASK), + BlockedWord(keyword="kumquat", action=ContentFilterAction.BLOCK), + ], + event_hook=GuardrailEventHooks.post_call, + ) + + +async def _text_completion_stream(*parts: tuple[str | None, str | None]): + for text, finish_reason in parts: + yield TextCompletionResponse( + id="cmpl-stream", + model="gpt-3.5-turbo-instruct", + object="text_completion", + choices=[TextChoices(index=0, text=text, finish_reason=finish_reason)], + ) + + +@pytest.mark.asyncio +async def test_streaming_hook_masks_a_word_split_across_text_completion_chunks(): + request_data: Final[dict] = {} + + delivered: Final = [ + chunk + async for chunk in _fruit_word_filter().async_post_call_streaming_iterator_hook( + user_api_key_dict=MagicMock(), + response=_text_completion_stream(("A pers", None), ("immon tart", None), (None, "stop")), + request_data=request_data, + ) + ] + + assert "".join(chunk.choices[0].text or "" for chunk in delivered) == "A [KEYWORD_REDACTED] tart" + assert request_data["metadata"]["standard_logging_guardrail_information"][0]["guardrail_status"] == "success" + + +_LONG_TAIL: Final = "the quick brown fox jumps over the lazy dog and then eats a ripe persimmon" + + +async def _text_completion_n2_stream_without_choice_1_finish() -> AsyncGenerator[TextCompletionResponse, None]: + for index, text, finish_reason in ( + (0, "A short answer", None), + (1, _LONG_TAIL[:30], None), + (1, _LONG_TAIL[30:], None), + (0, None, "stop"), + ): + yield TextCompletionResponse( + id="cmpl-stream", + model="gpt-3.5-turbo-instruct", + object="text_completion", + choices=[TextChoices(index=index, text=text, finish_reason=finish_reason)], + ).model_copy(update={"usage": None}) + + +async def _chat_n2_stream_without_choice_1_finish() -> AsyncGenerator[ModelResponseStream, None]: + for index, content, finish_reason in ( + (0, "A short answer", None), + (1, _LONG_TAIL[:30], None), + (1, _LONG_TAIL[30:], None), + (0, None, "stop"), + ): + yield ModelResponseStream( + id="chatcmpl-stream", + model="gpt-4.1-mini", + choices=[StreamingChoices(index=index, delta=Delta(content=content), finish_reason=finish_reason)], + ) + + +def _streamed_text(chunk: ModelResponseStream | TextCompletionResponse, index: int) -> str: + return "".join(_choice_text(choice) for choice in chunk.choices if choice.index == index) + + +def _choice_text(choice: TextChoices | StreamingChoices) -> str: + content: Final = stream_item_field(choice, "text") if isinstance(choice, TextChoices) else choice.delta.content + return content if isinstance(content, str) else "" + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "stream", [_text_completion_n2_stream_without_choice_1_finish, _chat_n2_stream_without_choice_1_finish] +) +async def test_streaming_hook_releases_the_held_tail_of_a_choice_the_stream_never_finished( + stream: Callable[[], AsyncIterator[ModelResponseStream | TextCompletionResponse]], +) -> None: + delivered: Final = [ + chunk + async for chunk in _fruit_word_filter().async_post_call_streaming_iterator_hook( + user_api_key_dict=MagicMock(), response=stream(), request_data={} + ) + ] + + assert "".join(_streamed_text(chunk, 0) for chunk in delivered) == "A short answer" + assert "".join(_streamed_text(chunk, 1) for chunk in delivered) == _LONG_TAIL.replace( + "persimmon", "[KEYWORD_REDACTED]" + ) + assert all(getattr(chunk, "usage", None) is None for chunk in delivered) + + +_STREAM_USAGE: Final = Usage(prompt_tokens=5, completion_tokens=20, total_tokens=25) + + +async def _then_usage_chunk( + stream: Callable[[], AsyncIterator[ModelResponseStream | TextCompletionResponse]], + usage_chunk: ModelResponseStream | TextCompletionResponse, +) -> AsyncGenerator[ModelResponseStream | TextCompletionResponse, None]: + async for chunk in stream(): + yield chunk + yield usage_chunk + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + ("stream", "usage_chunk"), + [ + ( + _text_completion_n2_stream_without_choice_1_finish, + TextCompletionResponse(id="cmpl-stream", object="text_completion", choices=[], usage=_STREAM_USAGE), + ), + (_chat_n2_stream_without_choice_1_finish, ModelResponseStream(id="chatcmpl-stream", choices=[], usage=_STREAM_USAGE)), + ], +) +async def test_streaming_hook_keeps_the_usage_chunk_last_after_releasing_a_held_tail( + stream: Callable[[], AsyncIterator[ModelResponseStream | TextCompletionResponse]], + usage_chunk: ModelResponseStream | TextCompletionResponse, +) -> None: + delivered: Final = [ + chunk + async for chunk in _fruit_word_filter().async_post_call_streaming_iterator_hook( + user_api_key_dict=MagicMock(), response=_then_usage_chunk(stream, usage_chunk), request_data={} + ) + ] + + assert delivered[-1] is usage_chunk + assert "".join(_streamed_text(chunk, 1) for chunk in delivered[:-1]) == _LONG_TAIL.replace( + "persimmon", "[KEYWORD_REDACTED]" + ) + + +@pytest.mark.asyncio +async def test_streaming_hook_relays_each_responses_api_event_as_it_arrives() -> None: + timeline: Final[list[str]] = [] + + async def responses_api_events() -> AsyncGenerator[OutputTextDeltaEvent, None]: + for delta in ("first", "second"): + timeline.append(f"upstream sent {delta}") + yield OutputTextDeltaEvent( + type=ResponsesAPIStreamEvents.OUTPUT_TEXT_DELTA, + item_id="msg_1", + output_index=0, + content_index=0, + delta=delta, + ) + + async for event in _fruit_word_filter().async_post_call_streaming_iterator_hook( + user_api_key_dict=MagicMock(), response=responses_api_events(), request_data={} + ): + timeline.append(f"client got {event.delta}") + + assert timeline == ["upstream sent first", "client got first", "upstream sent second", "client got second"] + + +@pytest.mark.asyncio +async def test_streaming_hook_blocks_a_word_split_across_text_completion_chunks(): + with pytest.raises(HTTPException) as exc_info: + async for _ in _fruit_word_filter().async_post_call_streaming_iterator_hook( + user_api_key_dict=MagicMock(), + response=_text_completion_stream(("A kum", None), ("quat tart", None), (None, "stop")), + request_data={}, + ): + pass + + assert exc_info.value.status_code == 400 + assert "kumquat" in str(exc_info.value.detail) diff --git a/tests/unit/proxy/guardrails/guardrail_hooks/test_compresr.py b/tests/unit/proxy/guardrails/guardrail_hooks/test_compresr.py index feb7090c2e3..cc5ad10b645 100644 --- a/tests/unit/proxy/guardrails/guardrail_hooks/test_compresr.py +++ b/tests/unit/proxy/guardrails/guardrail_hooks/test_compresr.py @@ -15,16 +15,20 @@ Tests cover: - fail_closed raises HTTPException; fail_open forwards uncompressed """ +import datetime import hashlib import json from types import SimpleNamespace +from typing import Final from unittest.mock import AsyncMock, MagicMock, create_autospec, patch import httpx import pytest from fastapi import HTTPException +import litellm from litellm.constants import PRE_CALL_EXECUTED_GUARDRAILS_KEY +from litellm.litellm_core_utils.litellm_logging import Logging from litellm.proxy.guardrails.guardrail_hooks.compresr.compresr import ( COMPRESR_RETRIEVE_TOOL_NAME, CompresrGuardrail, @@ -33,7 +37,7 @@ from litellm.proxy.guardrails.guardrail_hooks.compresr.compresr import ( _scoped_store_key, has_compresr_retrieve_tool, ) -from litellm.types.utils import GenericGuardrailAPIInputs +from litellm.types.utils import CallTypes, GenericGuardrailAPIInputs FAKE_API_BASE = "https://compresr.example.com" FAKE_API_KEY = "cmp_test-key" @@ -1435,6 +1439,57 @@ async def test_agentic_plan_builds_responses_followup_shape( assert output_item["output"] == TOOL_OUTPUT +@pytest.mark.asyncio +async def test_responses_retrieval_followup_reuses_the_model_the_caller_requested( + guardrail: CompresrGuardrail, +) -> None: + requested_model: Final = "bedrock/converse/us.anthropic.claude-sonnet-5-5" + hash_value: Final = "f" * 24 + logging_obj: Final = Logging( + model=requested_model, + messages=[], + stream=False, + call_type=CallTypes.responses.value, + start_time=datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc), + litellm_call_id="call-1", + function_id="fn-compresr-followup", + ) + litellm.responses( + model=requested_model, + input=USER_QUESTION, + mock_response="retrieving", + litellm_logging_obj=logging_obj, + ) + guardrail._store_originals(_scoped_store_key(logging_obj), {hash_value: TOOL_OUTPUT}) + response: Final = MagicMock() + response.output = [{"type": "function_call", "call_id": "fc_1"}] + response.content = None + + plan: Final = await guardrail.async_build_agentic_loop_plan( + tools={ + "tool_calls": [ + { + "id": "fc_1", + "type": "function", + "name": COMPRESR_RETRIEVE_TOOL_NAME, + "arguments": {"hash": hash_value}, + } + ] + }, + model="converse/us.anthropic.claude-sonnet-5-5", + messages=[{"role": "user", "content": USER_QUESTION}], + response=response, + anthropic_messages_provider_config=None, + anthropic_messages_optional_request_params={}, + logging_obj=logging_obj, + stream=False, + kwargs={}, + ) + + assert plan.request_patch is not None + assert plan.request_patch.model == requested_model + + @pytest.mark.asyncio async def test_agentic_plan_chat_parallel_tool_calls_echoes_only_retrieve( guardrail: CompresrGuardrail, diff --git a/tests/unit/proxy/guardrails/guardrail_hooks/test_grayswan.py b/tests/unit/proxy/guardrails/guardrail_hooks/test_grayswan.py index 954c57b5cb6..a5ba9dab0bd 100644 --- a/tests/unit/proxy/guardrails/guardrail_hooks/test_grayswan.py +++ b/tests/unit/proxy/guardrails/guardrail_hooks/test_grayswan.py @@ -5,6 +5,7 @@ import pytest from fastapi import HTTPException from litellm.integrations.custom_guardrail import ModifyResponseException +from litellm.llms.openai.completion.guardrail_translation.handler import OpenAITextCompletionHandler from litellm.proxy._types import UserAPIKeyAuth from litellm.proxy.guardrails.guardrail_hooks.grayswan import grayswan as grayswan_module from litellm.proxy.guardrails.guardrail_hooks.grayswan.grayswan import ( @@ -12,6 +13,7 @@ from litellm.proxy.guardrails.guardrail_hooks.grayswan.grayswan import ( GraySwanGuardrailAPIError, ) from litellm.types.guardrails import GuardrailEventHooks +from litellm.types.utils import TextChoices, TextCompletionResponse @pytest.fixture @@ -884,3 +886,29 @@ async def test_pre_call_payload_unchanged() -> None: {"role": "user", "content": "second"}, ] assert "tools" not in payload + + +@pytest.mark.asyncio +async def test_post_call_never_forwards_the_proxy_key_metadata_of_the_request_metadata() -> None: + guardrail = _post_call_guardrail() + client = _CapturingClient() + guardrail.async_handler = client + + await OpenAITextCompletionHandler().process_output_response( + response=TextCompletionResponse( + id="cmpl-1", model="gpt-4.1-mini", choices=[TextChoices(index=0, text="hello", finish_reason="stop")] + ), + guardrail_to_apply=guardrail, + user_api_key_dict=UserAPIKeyAuth(team_id="team-456", key_alias="ci-key", user_email="dev@example.com"), + request_data={ + "model": "gpt-4.1-mini", + "prompt": "say hello", + "metadata": { + "user_api_key_hash": "hashed-key", + "user_api_key_user_email": "dev@example.com", + "user_api_key_request_route": "/v1/completions", + }, + }, + ) + + assert "litellm_metadata" not in client.calls[0]["json"] diff --git a/tests/unit/proxy/guardrails/guardrail_hooks/test_headroom.py b/tests/unit/proxy/guardrails/guardrail_hooks/test_headroom.py index 3336b20f5eb..741fea5f2b9 100644 --- a/tests/unit/proxy/guardrails/guardrail_hooks/test_headroom.py +++ b/tests/unit/proxy/guardrails/guardrail_hooks/test_headroom.py @@ -21,6 +21,7 @@ Tests cover: loop resolves the retrieve tool call, then fake-streamed back to the client """ +import datetime import json import time from typing import Final, Optional @@ -33,6 +34,7 @@ from fastapi import HTTPException import litellm +from litellm.litellm_core_utils.litellm_logging import Logging from litellm.proxy.guardrails.guardrail_hooks.headroom.headroom import ( HeadroomGuardrail, has_headroom_retrieve_tool, @@ -771,6 +773,72 @@ async def test_async_build_agentic_loop_plan_builds_responses_api_function_call_ assert output_item["output"] == original_content +@pytest.mark.asyncio +async def test_responses_retrieval_followup_reuses_the_model_the_caller_requested( + guardrail: HeadroomGuardrail, +) -> None: + requested_model: Final = "bedrock/converse/us.anthropic.claude-sonnet-5-5" + retrieve_hash: Final = "b573993006976af767214fac" + logging_obj: Final = Logging( + model=requested_model, + messages=[], + stream=False, + call_type=CallTypes.responses.value, + start_time=datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc), + litellm_call_id="call-1", + function_id="fn-headroom-followup", + ) + litellm.responses( + model=requested_model, + input=f"What does it say? hash={retrieve_hash}", + mock_response="retrieving", + litellm_logging_obj=logging_obj, + ) + response: Final = MagicMock() + response.choices = None + response.content = None + response.output = [ + { + "type": "function_call", + "id": "fc_abc123", + "call_id": "call_abc123", + "name": HEADROOM_RETRIEVE_TOOL_NAME, + "arguments": json.dumps({"hash": retrieve_hash}), + } + ] + guardrail._issued_hashes_by_call_id["call-1"] = (frozenset({retrieve_hash}), time.monotonic() + 999) + + with patch.object( + guardrail.async_handler, + "get", + new_callable=AsyncMock, + return_value=_make_retrieve_response("The full compressed content."), + ): + plan: Final = await guardrail.async_build_agentic_loop_plan( + tools={ + "tool_calls": [ + { + "id": "call_abc123", + "type": "function", + "name": HEADROOM_RETRIEVE_TOOL_NAME, + "arguments": {"hash": retrieve_hash}, + } + ] + }, + model="converse/us.anthropic.claude-sonnet-5-5", + messages=[{"role": "user", "content": f"What does it say? hash={retrieve_hash}"}], + response=response, + anthropic_messages_provider_config=None, + anthropic_messages_optional_request_params={}, + logging_obj=logging_obj, + stream=False, + kwargs={"litellm_call_id": "call-1"}, + ) + + assert plan.request_patch is not None + assert plan.request_patch.model == requested_model + + @pytest.mark.asyncio async def test_async_build_agentic_loop_plan_builds_anthropic_tool_result_messages( guardrail: HeadroomGuardrail, diff --git a/tests/unit/proxy/guardrails/test_deferred_guardrail_logging.py b/tests/unit/proxy/guardrails/test_deferred_guardrail_logging.py index 7e5d0f6342e..6d1e5c72982 100644 --- a/tests/unit/proxy/guardrails/test_deferred_guardrail_logging.py +++ b/tests/unit/proxy/guardrails/test_deferred_guardrail_logging.py @@ -75,11 +75,13 @@ class _RecordingLogger(CustomLogger): def __init__(self) -> None: super().__init__() self.standard_logging_object: StandardLoggingPayload | None = None + self.logged: asyncio.Event = asyncio.Event() async def async_log_success_event( self, kwargs: Mapping[str, object], response_obj: object, start_time: datetime, end_time: datetime ) -> None: self.standard_logging_object = cast(StandardLoggingPayload, kwargs["standard_logging_object"]) + self.logged.set() class PostCallGuardrail(CustomGuardrail): @@ -239,52 +241,33 @@ class TestHasPostCallGuardrailsForPassthrough: @pytest.mark.asyncio -async def test_deferred_flag_stores_and_executes_closure(): - """ - When defer_async_logging is True on logging_obj: - 1. wrapper_async stores a callable closure instead of calling create_task - 2. Calling the closure fires create_task - 3. Sync callbacks fire immediately (not deferred) - """ - mock_logging_obj = MagicMock() - mock_logging_obj.defer_async_logging = True - mock_logging_obj.enqueue_deferred_logging = None - - await litellm.acompletion( - model="gpt-3.5-turbo", +@pytest.mark.parametrize("deferred", (False, True)) +async def test_success_logging_waits_for_deferred_closure_when_requested(deferred: bool) -> None: + recorder: Final = _RecordingLogger() + logging_obj: Final = Logging( + model="gpt-4o-mini", + messages=[{"role": "user", "content": "hi"}], + stream=False, + call_type="acompletion", + start_time=datetime(2026, 1, 1), + litellm_call_id="deferred-success", + function_id="deferred-success", + dynamic_async_success_callbacks=[recorder], + ) + logging_obj.defer_async_logging = deferred + response: Final = await litellm.acompletion( + model="gpt-4o-mini", messages=[{"role": "user", "content": "hi"}], mock_response="Hello!", - litellm_logging_obj=mock_logging_obj, + litellm_logging_obj=logging_obj, ) - - # Closure was stored - enqueue_fn = mock_logging_obj.enqueue_deferred_logging - assert callable(enqueue_fn), "Closure should be stored on logging_obj" - - # Sync callbacks fired immediately - mock_logging_obj.handle_sync_success_callbacks_for_async_calls.assert_called_once() - - # Calling the closure fires create_task - created_tasks = [] - real_create_task = asyncio.create_task - - def tracking_create_task(coro): - task = real_create_task(coro) - created_tasks.append(task) - return task - - with patch("asyncio.create_task", side_effect=tracking_create_task): - enqueue_fn() - - assert len(created_tasks) >= 1, "Closure should fire asyncio.create_task" - - for task in created_tasks: - if not task.done(): - task.cancel() - try: - await task - except (asyncio.CancelledError, Exception): - pass + if deferred: + assert recorder.standard_logging_object is None + assert logging_obj.enqueue_deferred_logging is not None + logging_obj.enqueue_deferred_logging() + await asyncio.wait_for(recorder.logged.wait(), timeout=5) + assert recorder.standard_logging_object is not None + assert recorder.standard_logging_object["id"] == response.id @pytest.mark.asyncio @@ -401,40 +384,6 @@ async def test_deferred_anthropic_messages_bridged_to_the_responses_api_logs_the assert recorder.standard_logging_object["response_cost"] == pytest.approx(3 * 2e-7 + 7333 * 2e-8 + 23 * 1.25e-6) -# --------------------------------------------------------------------------- -# 3. Non-streaming regression: without flag, create_task fires normally -# --------------------------------------------------------------------------- - - -@pytest.mark.asyncio -async def test_no_flag_fires_create_task_normally(): - """Without defer_async_logging, wrapper_async calls create_task as before.""" - created_tasks = [] - real_create_task = asyncio.create_task - - def tracking_create_task(coro): - task = real_create_task(coro) - created_tasks.append(task) - return task - - with patch("asyncio.create_task", side_effect=tracking_create_task): - await litellm.acompletion( - model="gpt-3.5-turbo", - messages=[{"role": "user", "content": "hi"}], - mock_response="Hello!", - ) - - assert len(created_tasks) >= 1 - - for task in created_tasks: - if not task.done(): - task.cancel() - try: - await task - except (asyncio.CancelledError, Exception): - pass - - # --------------------------------------------------------------------------- # 4. Non-streaming: deferred success log is suppressed when guardrail raises # --------------------------------------------------------------------------- @@ -1537,6 +1486,27 @@ class TestArmDeferredStreamDispatch: assert recorded["cache_hit"] is False assert recorded["prefer_async_handlers"] is True + @pytest.mark.asyncio + async def test_text_completion_stream_logs_after_the_guardrails_finish(self): + from litellm.text_completions.streaming_iterator import TextCompletionStreamWrapper + + logging_obj, recorded = self._dispatch_recording_logging_obj() + + self._processor()._arm_deferred_stream_dispatch( + response=TextCompletionStreamWrapper(completion_stream=MagicMock(), model="gpt-test"), + route_type="atext_completion", + user_api_key_dict=MagicMock(), + logging_obj=logging_obj, + ) + + assembled = object() + logging_obj._deferred_stream_complete_args = (assembled, False) + ProxyLogging._fire_deferred_stream_logging({"litellm_logging_obj": logging_obj}) + await asyncio.sleep(0) + + assert recorded["result"] is assembled + assert recorded["cache_hit"] is False + @pytest.mark.asyncio async def test_router_wrapped_bridged_iterator_gets_csw_arg_shape(self): """The router wraps iterators without _hidden_params in diff --git a/tests/unit/proxy/guardrails/test_prompt_security_guardrails.py b/tests/unit/proxy/guardrails/test_prompt_security_guardrails.py index e66e19dd1b4..b08646f1611 100644 --- a/tests/unit/proxy/guardrails/test_prompt_security_guardrails.py +++ b/tests/unit/proxy/guardrails/test_prompt_security_guardrails.py @@ -1,6 +1,7 @@ import asyncio import base64 from collections.abc import Mapping, Sequence +from typing import Final from unittest.mock import AsyncMock, patch import pytest @@ -8,6 +9,7 @@ from fastapi.exceptions import HTTPException from httpx import ReadTimeout, Request, Response import litellm +from litellm.llms.anthropic.chat.guardrail_translation.handler import AnthropicMessagesHandler from litellm.proxy._types import UserAPIKeyAuth from litellm.proxy.guardrails.guardrail_hooks.prompt_security.prompt_security import ( PromptSecurityGuardrail, @@ -1247,3 +1249,54 @@ async def test_check_tool_results_enabled(monkeypatch: pytest.MonkeyPatch): assert "indirect_prompt_injection" in str(excinfo.value.detail) + +@pytest.mark.asyncio +async def test_messages_api_pdf_reaches_file_sanitization_and_is_blocked() -> None: + sanitize_url: Final = "https://test.prompt.security/api/sanitizeFile" + upload: Final = AsyncMock( + return_value=Response(200, json={"jobId": "pdf-job"}, request=Request("POST", sanitize_url)) + ) + poll: Final = AsyncMock( + return_value=Response( + 200, + json={"status": "done", "content": "", "metadata": {"action": "block", "violations": ["Malicious File"]}}, + request=Request("GET", sanitize_url), + ) + ) + guardrail: Final = PromptSecurityGuardrail( + api_key="test-key", + api_base="https://test.prompt.security", + guardrail_name="test-guard", + event_hook="pre_call", + default_on=True, + ) + guardrail.poll_interval = 0 + pdf: Final = b"%PDF-1.4 pelican" + request: Final = { + "model": "gpt-5.6", + "max_tokens": 64, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "document", + "source": { + "type": "base64", + "media_type": "application/pdf", + "data": base64.b64encode(pdf).decode(), + }, + }, + {"type": "text", "text": "Summarize this PDF"}, + ], + } + ], + } + + with patch.object(guardrail.async_handler, "post", upload), patch.object(guardrail.async_handler, "get", poll): + with pytest.raises(HTTPException) as blocked: + await AnthropicMessagesHandler().process_input_messages(request, guardrail) + + assert blocked.value.status_code == 400 + assert "Malicious File" in str(blocked.value.detail) + assert upload.call_args.kwargs["files"]["file"][1] == pdf diff --git a/tests/unit/proxy/management_endpoints/policy_endpoints/test_ai_policy_suggester.py b/tests/unit/proxy/management_endpoints/policy_endpoints/test_ai_policy_suggester.py index 2041c622b3b..d287f5d00ad 100644 --- a/tests/unit/proxy/management_endpoints/policy_endpoints/test_ai_policy_suggester.py +++ b/tests/unit/proxy/management_endpoints/policy_endpoints/test_ai_policy_suggester.py @@ -3,6 +3,7 @@ Tests for AiPolicySuggester class. """ import json +from typing import Final from unittest.mock import AsyncMock, MagicMock, patch import httpx @@ -214,6 +215,26 @@ class TestAiPolicySuggester: assert result["selected_templates"] == [] assert "No templates" in result["explanation"] + @pytest.mark.asyncio + @pytest.mark.parametrize("selected_templates", [{}, ""]) + async def test_suggest_reads_an_empty_non_list_selection_as_no_templates(self, selected_templates: object) -> None: + mock_tool_call: Final = MagicMock() + mock_tool_call.function.arguments = json.dumps( + {"selected_templates": selected_templates, "explanation": "No template fits"} + ) + mock_response: Final = MagicMock() + mock_response.choices = [MagicMock()] + mock_response.choices[0].message.tool_calls = [mock_tool_call] + + with patch("litellm.acompletion", new_callable=AsyncMock, return_value=mock_response): + result: Final = await AiPolicySuggester().suggest( + templates=SAMPLE_TEMPLATES, + attack_examples=["Ignore all previous instructions"], + description="", + ) + + assert result == {"selected_templates": [], "explanation": "No template fits"} + @pytest.mark.asyncio async def test_suggest_calls_litellm_with_correct_params(self): suggester = AiPolicySuggester() diff --git a/tests/unit/proxy/management_endpoints/test_common_daily_activity.py b/tests/unit/proxy/management_endpoints/test_common_daily_activity.py index 01d2f7007a1..c80fa3572fd 100644 --- a/tests/unit/proxy/management_endpoints/test_common_daily_activity.py +++ b/tests/unit/proxy/management_endpoints/test_common_daily_activity.py @@ -2551,3 +2551,146 @@ async def test_get_daily_activity_rejects_non_canonical_dates_before_querying(st assert error.value.detail == {"error": "start_date and end_date must be valid YYYY-MM-DD dates"} mock_table.count.assert_not_awaited() mock_table.find_many.assert_not_awaited() + + +@pytest.mark.parametrize("missing", [None, ""]) +def test_missing_dimensions_preserve_all_paginated_metrics(missing: str | None) -> None: + records: Final = ( + _daily_user_spend_record(user_id="user", api_key="", spend=-0.25, model=missing, model_group=missing), + _daily_user_spend_record(user_id="user", api_key="", spend=0.0, model=missing, model_group=missing), + ) + result: Final = common_daily_activity_module._aggregate_spend_records_sync( + records=records, api_key_metadata={}, entity_id_field="user_id", entity_metadata_field=None + ) + day: Final = result["results"][0] + assert day.metrics == result["totals"] + for bucket in ( + day.breakdown.models["unknown-model"], + day.breakdown.model_groups["unknown-model-group"], + day.breakdown.providers["openai"], + day.breakdown.endpoints["/chat/completions"], + day.breakdown.entities["user"], + ): + assert bucket.metrics == day.metrics + assert bucket.api_key_breakdown == day.breakdown.api_keys + key: Final = day.breakdown.api_keys["unattributed"] + assert key.metrics == day.metrics + assert key.metadata.model_dump(exclude_none=True) == {"key_alias": "Unattributed", "key_exists": False} + + +def test_normalized_rollups_add_colliding_model_metrics_and_keep_key_identities() -> None: + records: Final = ( + _grouping_row(common_daily_activity_module._GROUP_DATE_MODEL, model=None, spend=1.0), + _grouping_row(common_daily_activity_module._GROUP_DATE_MODEL, model="", spend=2.0), + _grouping_row(common_daily_activity_module._GROUP_DATE_MODEL, model="unknown-model", spend=3.0), + _grouping_row(common_daily_activity_module._GROUP_DATE_MODEL_API_KEY, model=None, api_key="", spend=1.0), + _grouping_row(common_daily_activity_module._GROUP_DATE_MODEL_API_KEY, model="", api_key="", spend=2.0), + _grouping_row( + common_daily_activity_module._GROUP_DATE_MODEL_API_KEY, + model="unknown-model", + api_key="unattributed", + spend=3.0, + ), + _grouping_row(common_daily_activity_module._GROUP_DATE_API_KEY, api_key="", spend=3.0), + _grouping_row(common_daily_activity_module._GROUP_DATE_API_KEY, api_key="unattributed", spend=3.0), + _grouping_row(common_daily_activity_module._GROUP_DATE_API_KEY, api_key="raw:unattributed", spend=4.0), + ) + result: Final = common_daily_activity_module._aggregate_grouping_sets_records_sync( + records=records, api_key_metadata={} + ) + breakdown: Final = result["results"][0].breakdown + assert breakdown.models["unknown-model"].metrics.spend == 6.0 + assert {key: value.metrics.spend for key, value in breakdown.models["unknown-model"].api_key_breakdown.items()} == { + "unattributed": 3.0, + "raw:unattributed": 3.0, + } + assert {key: value.metrics.spend for key, value in breakdown.api_keys.items()} == { + "unattributed": 3.0, + "raw:unattributed": 3.0, + "raw:raw:unattributed": 4.0, + } + + +@pytest.mark.parametrize( + ("model", "group", "expected_group"), + [ + (None, None, "unknown-model-group"), + ("", "", "unknown-model-group"), + (None, "configured-group", "configured-group"), + ("provider-model", "", "provider-model"), + ("unknown-model", "", "unknown-model"), + (" ", "", " "), + ], +) +def test_model_group_uses_raw_model_before_unknown_group( + model: str | None, group: str | None, expected_group: str +) -> None: + result: Final = common_daily_activity_module._aggregate_spend_records_sync( + records=(_daily_user_spend_record(user_id="user", api_key="key", spend=1.0, model=model, model_group=group),), + api_key_metadata={}, + entity_id_field=None, + entity_metadata_field=None, + ) + assert tuple(result["results"][0].breakdown.model_groups) == (expected_group,) + + +@pytest.mark.parametrize( + ("raw_key", "bucket"), + [ + ("", "unattributed"), + ("real-hash", "real-hash"), + ("unattributed", "raw:unattributed"), + ("raw:unattributed", "raw:raw:unattributed"), + ("raw:real-hash", "raw:raw:real-hash"), + ], +) +def test_reporting_key_filters_round_trip_without_losing_scope(raw_key: str, bucket: str) -> None: + from litellm.repositories.daily_activity_buckets import api_key_bucket + + assert api_key_bucket(raw_key) == bucket + scope: Final = daily_activity_scope( + "litellm_dailyuserspend", "user_id", "user", None, bucket, "2024-01-01", "2024-01-01", None, None + ) + assert scope.api_keys == (raw_key,) + assert scope.entity_ids == ("user",) + + +@pytest.mark.parametrize(("raw_key", "bucket_key"), [("", "unattributed"), ("unattributed", "raw:unattributed")]) +def test_mcp_nested_key_metrics_use_reporting_identity(raw_key: str, bucket_key: str) -> None: + record: Final = SimpleNamespace( + **{ + **vars(_daily_user_spend_record(user_id="user", api_key=raw_key, spend=0.25, model=None, model_group=None)), + "mcp_namespaced_tool_name": "deleted-server/tool", + } + ) + result: Final = common_daily_activity_module._aggregate_spend_records_sync( + records=(record,), api_key_metadata={}, entity_id_field=None, entity_metadata_field=None + ) + breakdown: Final = result["results"][0].breakdown + assert breakdown.mcp_servers["deleted-server/tool"].api_key_breakdown == breakdown.api_keys + assert breakdown.mcp_servers["deleted-server/tool"].api_key_breakdown[bucket_key].metrics.spend == 0.25 + + +def test_ptu_rows_do_not_create_endpoint_or_mcp_key_buckets(ptu_cost_attribution_enabled: None) -> None: + from litellm.constants import PTU_SENTINEL_API_KEY + from litellm.types.proxy.management_endpoints.common_daily_activity import BreakdownMetrics + + record: Final = SimpleNamespace( + **{ + **vars(_spend_record(PTU_SENTINEL_API_KEY, ptu_flat_cost=2.0)), + "mcp_namespaced_tool_name": "server/tool", + "endpoint": "/mcp", + } + ) + breakdown: Final = common_daily_activity_module.update_breakdown_metrics(BreakdownMetrics(), record, {}, {}, {}) + assert breakdown.api_keys == {} + assert breakdown.endpoints["/mcp"].api_key_breakdown == {} + assert breakdown.mcp_servers["server/tool"].api_key_breakdown == {} + assert breakdown.endpoints["/mcp"].metrics.flat_cost == 2.0 + assert breakdown.mcp_servers["server/tool"].metrics.flat_cost == 2.0 + + +def test_metric_merges_preserve_every_additive_field() -> None: + metrics: Final = SpendMetrics.model_validate(dict.fromkeys(SpendMetrics.model_fields, 2)) + merged: Final = common_daily_activity_module.sum_spend_metrics((SpendMetrics(), metrics, metrics)) + assert merged.model_dump() == dict.fromkeys(SpendMetrics.model_fields, 4) diff --git a/tests/unit/proxy/management_endpoints/test_daily_activity_routes.py b/tests/unit/proxy/management_endpoints/test_daily_activity_routes.py index 2a273034a97..e487c00f25d 100644 --- a/tests/unit/proxy/management_endpoints/test_daily_activity_routes.py +++ b/tests/unit/proxy/management_endpoints/test_daily_activity_routes.py @@ -628,7 +628,9 @@ def test_aggregated_routes_return_scoped_results( assert len(body["results"]) == 2, response.text -def _ptu_sized_router(model_group: str, shares: Mapping[str, int] = MappingProxyType({"team-a": 30, "team-b": 20})) -> Router: +def _ptu_sized_router( + model_group: str, shares: Mapping[str, int] = MappingProxyType({"team-a": 30, "team-b": 20}) +) -> Router: return Router( model_list=[ { @@ -1268,13 +1270,15 @@ def test_team_scope_applies_membership_and_user_key_filter( assert repository.aggregated.call_args.kwargs["include_entity_breakdown"] is True +@pytest.mark.parametrize("api_key", ["key-beta", "unattributed", "raw:unattributed", "raw:raw:unattributed"]) def test_team_scope_does_not_allow_an_unowned_api_key( daily_activity_client: tuple[TestClient, _FakeRepository], + api_key: str, ) -> None: client, _ = daily_activity_client response: Final = client.get( "/team/daily/activity/aggregated", - params={**_entity_params("team_ids", "team-a"), "api_key": "key-beta"}, + params={**_entity_params("team_ids", "team-a"), "api_key": api_key}, headers={"x-user-role": LitellmUserRoles.INTERNAL_USER.value, "x-user-id": "user-a"}, ) assert response.status_code == 200, response.text @@ -1622,3 +1626,17 @@ def test_export_content_disposition_is_ascii_and_built_from_canonical_dates( disposition: Final = response.headers["content-disposition"] assert disposition == 'attachment; filename="team-usage-2025-01-01-2025-01-02-daily.csv"' assert disposition.isascii() + + +@pytest.mark.parametrize(("prefix", "query_name", "entity_id"), _ENTITY_CASES) +def test_unattributed_key_filter_does_not_become_an_unfiltered_request( + daily_activity_client: tuple[TestClient, _FakeRepository], prefix: str, query_name: str, entity_id: str +) -> None: + client, repository = daily_activity_client + response: Final = client.get( + f"{prefix}/daily/activity/aggregated", + params={**_entity_params(query_name, entity_id), "api_key": "unattributed"}, + ) + assert response.status_code == 200, response.text + assert response.json()["metadata"]["total_spend"] == 0 + assert repository.aggregated.call_args.args[0].api_keys == ("",) diff --git a/tests/unit/proxy/management_endpoints/test_model_management_endpoints.py b/tests/unit/proxy/management_endpoints/test_model_management_endpoints.py index b892dc97e5c..835e7605955 100644 --- a/tests/unit/proxy/management_endpoints/test_model_management_endpoints.py +++ b/tests/unit/proxy/management_endpoints/test_model_management_endpoints.py @@ -3617,6 +3617,33 @@ class TestDeleteTeamModels: assert any(e[0] == "delete_many" for e in prisma.events) +@pytest.mark.parametrize( + ("model_info_patch", "expected_gate"), + [ + pytest.param({"tpm": 5}, True, id="unrelated-field"), + pytest.param({"supported_endpoints": ["/v1/embeddings", "/v1/rerank"]}, True, id="endpoint-list-only"), + pytest.param({"enforce_supported_endpoints": False}, False, id="explicit-false"), + ], +) +def test_model_info_patch_changes_the_endpoint_gate_only_when_it_sends_the_flag( + model_info_patch: dict[str, object], expected_gate: bool +): + from litellm.proxy.management_endpoints.model_management_endpoints import update_db_model + + result: Final = update_db_model( + db_model=Deployment( + model_name="embedding-only", + litellm_params=LiteLLM_Params(model="openai/text-embedding-3-small"), + model_info=ModelInfo( + id="embedding-only", enforce_supported_endpoints=True, supported_endpoints=["/v1/embeddings"] + ), + ), + updated_patch=updateDeployment.model_validate({"model_info": model_info_patch}), + ) + + assert json.loads(result["model_info"]).get("enforce_supported_endpoints") is expected_gate + + def _build_db_model_for_blocked_test(): from litellm.types.router import Deployment, LiteLLM_Params, ModelInfo diff --git a/tests/unit/proxy/management_endpoints/test_tag_management_endpoints.py b/tests/unit/proxy/management_endpoints/test_tag_management_endpoints.py index e5d13a381ae..a2e509b6eb8 100644 --- a/tests/unit/proxy/management_endpoints/test_tag_management_endpoints.py +++ b/tests/unit/proxy/management_endpoints/test_tag_management_endpoints.py @@ -1058,7 +1058,8 @@ async def test_internal_user_tag_daily_activity_is_scoped_to_their_keys(): @pytest.mark.asyncio -async def test_internal_user_tag_daily_activity_rejects_unowned_api_key_filter(): +@pytest.mark.parametrize("api_key", ["unowned-key", "unattributed", "raw:unattributed", "raw:raw:unattributed"]) +async def test_internal_user_tag_daily_activity_rejects_unowned_api_key_filter(api_key: str): """ If an internal user filters tag usage by an API key they do not own, the endpoint should return an empty scoped filter instead of exposing that key's @@ -1092,7 +1093,7 @@ async def test_internal_user_tag_daily_activity_rejects_unowned_api_key_filter() result = await get_tag_daily_activity( start_date="2025-01-01", end_date="2025-01-31", - api_key="unowned-key", + api_key=api_key, user_api_key_dict=mock_user_auth, ) diff --git a/tests/unit/proxy/pass_through_endpoints/llm_provider_handlers/test_cohere_passthrough_logging_handler.py b/tests/unit/proxy/pass_through_endpoints/llm_provider_handlers/test_cohere_passthrough_logging_handler.py index 32e87d93fdb..f2bb9eaa9a5 100644 --- a/tests/unit/proxy/pass_through_endpoints/llm_provider_handlers/test_cohere_passthrough_logging_handler.py +++ b/tests/unit/proxy/pass_through_endpoints/llm_provider_handlers/test_cohere_passthrough_logging_handler.py @@ -1,18 +1,23 @@ import json +import logging +from collections.abc import Mapping from datetime import datetime +from typing import Final from unittest.mock import MagicMock, patch import httpx import pytest - +import litellm from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj from litellm.proxy.pass_through_endpoints.llm_provider_handlers.cohere_passthrough_logging_handler import ( CoherePassthroughLoggingHandler, ) +from litellm.proxy.pass_through_endpoints.success_handler import PassThroughEndpointLogging from litellm.types.passthrough_endpoints.pass_through_endpoints import ( PassthroughStandardLoggingPayload, ) +from litellm.types.utils import CallTypes class TestCoherePassthroughLoggingHandler: @@ -64,46 +69,25 @@ class TestCoherePassthroughLoggingHandler: ) @patch("litellm.completion_cost") - @patch("litellm.litellm_core_utils.litellm_logging.get_standard_logging_object_payload") - @patch("litellm.llms.cohere.embed.v1_transformation.CohereEmbeddingConfig._transform_response") - def test_cohere_embed_passthrough_cost_tracking( - self, mock_transform_response, mock_get_standard_logging, mock_completion_cost - ): + def test_cohere_embed_passthrough_cost_tracking(self, mock_completion_cost: MagicMock) -> None: """Test successful cost tracking for Cohere embed passthrough""" - # Arrange - from litellm.types.utils import EmbeddingResponse + mock_completion_cost.return_value = 3.6e-07 - # Create a mock embedding response - mock_embedding_response = EmbeddingResponse() - mock_embedding_response.data = [ - {"object": "embedding", "index": 0, "embedding": [0.1, 0.2, 0.3]}, - {"object": "embedding", "index": 1, "embedding": [0.4, 0.5, 0.6]}, - ] - mock_embedding_response.model = "embed-english-v3.0" - mock_embedding_response.object = "list" - from litellm.types.utils import Usage + mock_httpx_response: Final = self._create_mock_httpx_response() + mock_logging_obj: Final = self._create_mock_logging_obj() + passthrough_payload: Final = self._create_passthrough_logging_payload() - mock_embedding_response.usage = Usage(prompt_tokens=3, completion_tokens=0, total_tokens=3) - - mock_transform_response.return_value = mock_embedding_response - mock_completion_cost.return_value = 3.6e-07 # Expected cost for embed-v4.0 - mock_get_standard_logging.return_value = {"test": "logging_payload"} - - mock_httpx_response = self._create_mock_httpx_response() - mock_logging_obj = self._create_mock_logging_obj() - passthrough_payload = self._create_passthrough_logging_payload() - - kwargs = { + kwargs: Final = { "passthrough_logging_payload": passthrough_payload, } - request_body = { + request_body: Final = { "model": "embed-english-v3.0", "texts": ["test passthrough"], } # Act - result = self.handler.cohere_passthrough_handler( + result: Final = self.handler.cohere_passthrough_handler( httpx_response=mock_httpx_response, response_body=self.mock_cohere_embed_response, logging_obj=mock_logging_obj, @@ -126,7 +110,7 @@ class TestCoherePassthroughLoggingHandler: # Verify cost calculation was called with correct parameters mock_completion_cost.assert_called_once() - call_args = mock_completion_cost.call_args + call_args: Final = mock_completion_cost.call_args assert call_args.kwargs["model"] == "embed-english-v3.0" assert call_args.kwargs["custom_llm_provider"] == "cohere" assert call_args.kwargs["call_type"] == "aembedding" @@ -174,5 +158,167 @@ class TestCoherePassthroughLoggingHandler: assert result == {"result": None, "kwargs": {}} +COHERE_EMBED_V2_TEXT_BODY: Final[Mapping[str, object]] = { + "id": "da6e531f-83f4-4bcb-8e9a-f1a2c6e7a1b3", + "embeddings": {"float": [[0.1, 0.2], [0.3, 0.4]]}, + "texts": ["hello world", "second text"], + "meta": {"api_version": {"version": "2"}, "billed_units": {"input_tokens": 9}}, + "response_type": "embeddings_by_type", +} +COHERE_EMBED_V2_IMAGE_BODY: Final[Mapping[str, object]] = { + "id": "5807ee2e-0cda-445a-9ec8-864c60a06606", + "embeddings": {"float": [[0.125, -0.375]]}, + "texts": [], + "images": [{"width": 64, "height": 64, "format": "image/png", "bit_depth": 8}], + "meta": {"api_version": {"version": "2"}, "billed_units": {"input_tokens": 0, "image_tokens": 6}}, + "response_type": "embeddings_by_type", +} +COHERE_EMBED_V1_FLOATS_BODY: Final[Mapping[str, object]] = { + "id": "5d4e1f3a-7c1b-4f3e-9a3e-1b2c3d4e5f60", + "embeddings": [[0.1, 0.2], [0.3, 0.4]], + "texts": ["hello world", "second text"], + "meta": {"api_version": {"version": "1"}, "billed_units": {"input_tokens": 3}}, + "response_type": "embeddings_floats", +} + + +_CALL_START: Final = datetime(2026, 10, 1, 12, 0, 0) +_CALL_END: Final = datetime(2026, 10, 1, 12, 0, 1) + + +def _passthrough_logging_obj() -> LiteLLMLoggingObj: + return LiteLLMLoggingObj( + model="unknown", + messages=[{"role": "user", "content": "no-message-pass-through-endpoint"}], + stream=False, + call_type=CallTypes.pass_through.value, + start_time=_CALL_START, + litellm_call_id="cohere-embed-passthrough-call", + function_id="cohere-embed-passthrough-function", + ) + + +@pytest.mark.parametrize( + ("url_route", "body", "input_tokens", "image_tokens"), + [ + ("https://api.cohere.com/v2/embed", COHERE_EMBED_V2_TEXT_BODY, 9, 0), + ("https://api.cohere.com/v2/embed", COHERE_EMBED_V2_IMAGE_BODY, 0, 6), + ("https://api.cohere.com/v1/embed", COHERE_EMBED_V1_FLOATS_BODY, 3, 0), + ], + ids=["v2_text", "v2_image", "v1_floats"], +) +def test_cohere_embed_passthrough_is_costed_from_billed_units( + url_route: str, body: Mapping[str, object], input_tokens: int, image_tokens: int +) -> None: + request_body: Final = { + "model": "embed-v4.0", + "texts": ["hello world", "second text"], + "input_type": "search_document", + } + model_info: Final = litellm.get_model_info("cohere/embed-v4.0") + expected_cost: Final = ( + input_tokens * model_info["input_cost_per_token"] + image_tokens * model_info["input_cost_per_image_token"] + ) + + logged: Final = PassThroughEndpointLogging().normalize_llm_passthrough_logging_payload( + httpx_response=httpx.Response(200, json=body, request=httpx.Request("POST", url_route)), + response_body=dict(body), + request_body=request_body, + logging_obj=_passthrough_logging_obj(), + url_route=url_route, + result="", + start_time=_CALL_START, + end_time=_CALL_END, + cache_hit=False, + ) + + assert expected_cost > 0 + assert logged["kwargs"]["custom_llm_provider"] == "cohere" + assert logged["kwargs"]["response_cost"] == pytest.approx(expected_cost) + + + +def _normalize_cohere_embed(url_route: str, body: Mapping[str, object], model: str) -> Mapping[str, object]: + return PassThroughEndpointLogging().normalize_llm_passthrough_logging_payload( + httpx_response=httpx.Response(200, json=body, request=httpx.Request("POST", url_route)), + response_body=dict(body), + request_body={"model": model, "texts": ["hello world"], "input_type": "search_document"}, + logging_obj=_passthrough_logging_obj(), + url_route=url_route, + result="", + start_time=_CALL_START, + end_time=_CALL_END, + cache_hit=False, + ) + + +def test_cohere_v3_image_embed_passthrough_is_costed_per_billed_image() -> None: + body: Final = { + "id": "8a1b2c3d-4e5f-4a6b-8c7d-9e0f1a2b3c4d", + "embeddings": {"float": [[0.125, -0.375]]}, + "texts": [], + "images": [{"width": 64, "height": 64, "format": "image/png", "bit_depth": 8}], + "meta": {"api_version": {"version": "2"}, "billed_units": {"images": 1}}, + "response_type": "embeddings_by_type", + } + per_image: Final = litellm.get_model_info("embed-english-v3.0", custom_llm_provider="cohere")["input_cost_per_image"] + + logged: Final = _normalize_cohere_embed("https://api.cohere.com/v2/embed", body, "embed-english-v3.0") + + assert per_image is not None and per_image > 0 + assert logged["kwargs"]["response_cost"] == pytest.approx(per_image) + + +def test_cohere_embed_passthrough_rows_keep_their_embedding_type() -> None: + body: Final = { + **COHERE_EMBED_V2_TEXT_BODY, + "embeddings": {"float": [[0.1, 0.2], [0.3, 0.4]], "int8": [[1, 2], [3, 4]]}, + } + + logged: Final = _normalize_cohere_embed("https://api.cohere.com/v2/embed", body, "embed-v4.0") + + assert [(row["type"], row["index"], row["embedding"]) for row in logged["standard_logging_response_object"].data] == [ + ("float", 0, [0.1, 0.2]), + ("float", 1, [0.3, 0.4]), + ("int8", 0, [1, 2]), + ("int8", 1, [3, 4]), + ] + + +def test_unpriceable_cohere_embed_passthrough_falls_back_to_the_plain_passthrough_row( + caplog: pytest.LogCaptureFixture, +) -> None: + with caplog.at_level(logging.WARNING): + logged: Final = _normalize_cohere_embed( + "https://api.cohere.com/v2/embed", COHERE_EMBED_V2_TEXT_BODY, "embed-model-missing-from-the-cost-map" + ) + + assert logged["standard_logging_response_object"] is None + assert "response_cost" not in logged["kwargs"] + assert any( + "Could not price the Cohere passthrough embed response" in record.getMessage() + for record in caplog.records + if record.levelno == logging.WARNING + ) + + +@pytest.mark.parametrize( + ("url_route", "body"), + [ + ("https://api.cohere.com/v1/embed-jobs", {"job_id": "job-1", "meta": {"api_version": {"version": "1"}}}), + ( + "https://api.cohere.com/v1/embed-jobs/job-1", + {"job_id": "job-1", "status": "processing", "model": "embed-english-v3.0"}, + ), + ], +) +def test_cohere_embed_job_passthrough_logs_the_plain_passthrough_row( + url_route: str, body: Mapping[str, object] +) -> None: + logged: Final = _normalize_cohere_embed(url_route, body, "embed-english-v3.0") + + assert logged["standard_logging_response_object"] is None + assert "response_cost" not in logged["kwargs"] + if __name__ == "__main__": pytest.main([__file__]) diff --git a/tests/unit/proxy/pass_through_endpoints/llm_provider_handlers/test_openai_passthrough_logging_handler.py b/tests/unit/proxy/pass_through_endpoints/llm_provider_handlers/test_openai_passthrough_logging_handler.py index c9064caf929..a4f3fd0a86f 100644 --- a/tests/unit/proxy/pass_through_endpoints/llm_provider_handlers/test_openai_passthrough_logging_handler.py +++ b/tests/unit/proxy/pass_through_endpoints/llm_provider_handlers/test_openai_passthrough_logging_handler.py @@ -24,8 +24,10 @@ from litellm.proxy._types import SpendLogsPayload from litellm.proxy.pass_through_endpoints.success_handler import ( PassThroughEndpointLogging, ) +from litellm.proxy.pass_through_endpoints.streaming_handler import PassThroughStreamingHandler from litellm.proxy.spend_tracking.spend_tracking_utils import get_logging_payload from litellm.types.passthrough_endpoints.pass_through_endpoints import ( + EndpointType, PassthroughStandardLoggingPayload, ) @@ -2088,6 +2090,7 @@ COMPACTION: Final = { "output": [{"id": "cmp_082492f9", "type": "compaction", "encrypted_content": "gAAAA"}], "usage": RESPONSES_USAGE, } +AGENTS_SESSIONS_URL: Final = "https://api.openai.com/v1/agents/sessions" ASSISTANT_MESSAGE: Final = { "id": "msg_080b10ff5d740c69", "type": "message", @@ -2119,6 +2122,28 @@ def _stored_response(status: str, background: bool, usage: Mapping[str, object] } +def _agent_session(agent_model: str | None) -> dict[str, object]: + agent: Final = ( + {} + if agent_model is None + else { + "agent": {"id": "agent_6a0b0f8e", "model": agent_model, "name": None, "service_tier": "auto", "tools": []} + } + ) + return { + "id": "sess_6a0b0f8e2c1d4e5f", + "object": "agent.session", + "created_at": 1792270025, + "last_active_at": 1792270025, + "status": "in_progress", + "metadata": {}, + "required_actions": [], + "vault_ids": [], + "usage": None, + **agent, + } + + @dataclass(frozen=True, slots=True) class _LoggedCall: spend_row: SpendLogsPayload @@ -2296,20 +2321,12 @@ class TestOpenAIPassthroughResponsesSubRouteBilling: assert not dispatched["kwargs"].get("response_cost") assert [record.getMessage() for record in caplog.records if record.levelno >= logging.ERROR] == [] - async def _logged_call( - self, - method: str, - sub_route: str, - request_body: dict[str, object], - response_body: dict[str, object], - litellm_call_id: str, - ) -> _LoggedCall: - url_route: Final = f"{RESPONSES_URL}{sub_route}" - recorder: Final = _SpendRowRecorder() + @staticmethod + def _recorded_logging_obj(recorder: _SpendRowRecorder, litellm_call_id: str, stream: bool) -> LiteLLMLoggingObj: logging_obj: Final = LiteLLMLoggingObj( model="unknown", messages=[], - stream=False, + stream=stream, call_type="pass_through_endpoint", start_time=DISPATCH_TIME, litellm_call_id=litellm_call_id, @@ -2319,6 +2336,36 @@ class TestOpenAIPassthroughResponsesSubRouteBilling: logging_obj.update_environment_variables( model="unknown", user="unknown", optional_params={}, litellm_params={}, call_type="pass_through_endpoint" ) + return logging_obj + + async def _logged_stream( + self, url_route: str, request_body: dict[str, object], sse_events: tuple[bytes, ...], litellm_call_id: str + ) -> _LoggedCall: + recorder: Final = _SpendRowRecorder() + await PassThroughStreamingHandler.route_streaming_logging_to_handler( + litellm_logging_obj=self._recorded_logging_obj(recorder, litellm_call_id, stream=True), + passthrough_success_handler_obj=PassThroughEndpointLogging(), + url_route=url_route, + request_body=request_body, + endpoint_type=EndpointType.OPENAI, + start_time=DISPATCH_TIME, + raw_bytes=sse_events, + end_time=DISPATCH_TIME, + ) + return await asyncio.wait_for(recorder.call, timeout=5) + + async def _logged_call( + self, + method: str, + sub_route: str, + request_body: dict[str, object], + response_body: dict[str, object], + litellm_call_id: str, + base_url: str = RESPONSES_URL, + ) -> _LoggedCall: + url_route: Final = f"{base_url}{sub_route}" + recorder: Final = _SpendRowRecorder() + logging_obj: Final = self._recorded_logging_obj(recorder, litellm_call_id, stream=False) await PassThroughEndpointLogging().pass_through_async_success_handler( httpx_response=httpx.Response(200, json=response_body, request=httpx.Request(method, url_route)), @@ -2350,6 +2397,79 @@ class TestOpenAIPassthroughResponsesSubRouteBilling: assert spend_log_row["model"] == response_body["model"] assert not spend_log_row["spend"] + @pytest.mark.asyncio + @pytest.mark.parametrize( + ("request_agent", "response_agent_model", "logged_model"), + [ + pytest.param({"agent_id": "agent_6a0b0f8e"}, "gpt-5.4-mini", "gpt-5.4-mini", id="saved_agent"), + pytest.param({"agent": {"model": "gpt-5.4-mini"}}, None, "gpt-5.4-mini", id="request_agent_only"), + pytest.param({"agent": {"model": "gpt-5.4"}}, "gpt-5.4-mini", "gpt-5.4-mini", id="response_agent_wins"), + ], + ) + async def test_unbilled_agents_session_spend_log_names_the_agent_model( + self, request_agent: dict[str, object], response_agent_model: str | None, logged_model: str + ): + spend_log_row: Final = ( + await self._logged_call( + "POST", + "", + {"environment": {"type": "none"}, "input": "Say hi in one word", **request_agent}, + _agent_session(response_agent_model), + "2f7c9e1a-4b3d-4a6e-8c5f-1d0e9b7a3c62", + AGENTS_SESSIONS_URL, + ) + ).spend_row + + assert spend_log_row["model"] == logged_model + assert not spend_log_row["spend"] + + @pytest.mark.asyncio + async def test_unbilled_saved_agent_spend_log_names_the_agent_model(self): + saved_agent: Final = {"id": "agent_6a0b0f8e", "object": "agent", "model": "gpt-5.4-mini", "tools": []} + + spend_log_row: Final = ( + await self._logged_call( + "POST", + "", + {"model": "gpt-5.4-mini", "instructions": "Answer in one word"}, + saved_agent, + "8d2e4f6a-1b3c-4d5e-9f7a-2c4e6a8b0d13", + "https://api.openai.com/v1/agents", + ) + ).spend_row + + assert spend_log_row["model"] == saved_agent["model"] + assert not spend_log_row["spend"] + + @pytest.mark.asyncio + @pytest.mark.parametrize( + ("request_body", "session_agent_model", "logged_model"), + [ + pytest.param({}, "gpt-5.4-mini", "gpt-5.4-mini", id="session_event_names_the_model"), + pytest.param({"agent": {"model": "gpt-5.4"}}, None, "gpt-5.4", id="request_agent_fallback"), + ], + ) + async def test_streamed_agents_session_spend_log_names_the_agent_model( + self, request_body: dict[str, object], session_agent_model: str | None, logged_model: str + ): + session_event: Final = {"type": "agent.session.in_progress", "session": _agent_session(session_agent_model)} + sse_events: Final = ( + f"event: agent.session.in_progress\ndata: {json.dumps(session_event)}\n\n".encode(), + b'event: agent.turn.completed\ndata: {"type": "agent.turn.completed", "usage": null}\n\n', + ) + + spend_log_row: Final = ( + await self._logged_stream( + f"{AGENTS_SESSIONS_URL}/sess_6a0b0f8e2c1d4e5f/events", + request_body, + sse_events, + "5b7d9f1a-3c5e-4a7b-8d9f-0e2a4c6b8d35", + ) + ).spend_row + + assert spend_log_row["model"] == logged_model + assert not spend_log_row["spend"] + @pytest.mark.asyncio @pytest.mark.parametrize( ("method", "sub_route", "response_body"), diff --git a/tests/unit/proxy/pass_through_endpoints/test_llm_pass_through_endpoints.py b/tests/unit/proxy/pass_through_endpoints/test_llm_pass_through_endpoints.py index 43fe5d4e786..33ddd98610b 100644 --- a/tests/unit/proxy/pass_through_endpoints/test_llm_pass_through_endpoints.py +++ b/tests/unit/proxy/pass_through_endpoints/test_llm_pass_through_endpoints.py @@ -158,63 +158,30 @@ class TestBaseOpenAIPassThroughHandler: print(f"Base URL with trailing slash: '{base_url}' + '{path}' → '{result}'") assert str(result) == "https://api.example.com/v1/chat/completions" - def test_append_openai_beta_header(self): - print("\nTesting _append_openai_beta_header method...") + def test_assemble_headers_keeps_the_operators_openai_beta_over_the_clients(self): + client_request: Final = Request( + { + "type": "http", + "method": "POST", + "path": "/azure/v1/agents/sessions", + "headers": [(b"openai-beta", b"agents=v1")], + "query_string": b"", + } + ) - # Create mock requests with different paths - assistants_request = MagicMock(spec=Request) - assistants_request.url = MagicMock() - assistants_request.url.path = "/v1/threads/thread_123456/messages" + assembled: Final = BaseOpenAIPassThroughHandler._assemble_headers( + api_key="sk-upstream", + endpoint="v1/agents/sessions", + request=client_request, + custom_llm_provider=litellm.LlmProviders.AZURE_AI, + extra_headers={"OpenAI-Beta": "agents=v2"}, + ) - non_assistants_request = MagicMock(spec=Request) - non_assistants_request.url = MagicMock() - non_assistants_request.url.path = "/v1/chat/completions" - - headers = {"authorization": "Bearer test_key"} - - # Test with assistants API request - result = BaseOpenAIPassThroughHandler._append_openai_beta_header(headers, assistants_request) - print(f"Assistants API request: Added header: {result}") - assert result["OpenAI-Beta"] == "assistants=v2" - - # Test with non-assistants API request - headers = {"authorization": "Bearer test_key"} - result = BaseOpenAIPassThroughHandler._append_openai_beta_header(headers, non_assistants_request) - print(f"Non-assistants API request: Headers: {result}") - assert "OpenAI-Beta" not in result - - # Test with assistant in the path - assistant_request = MagicMock(spec=Request) - assistant_request.url = MagicMock() - assistant_request.url.path = "/v1/assistants/asst_123456" - - headers = {"authorization": "Bearer test_key"} - result = BaseOpenAIPassThroughHandler._append_openai_beta_header(headers, assistant_request) - print(f"Assistant API request: Added header: {result}") - assert result["OpenAI-Beta"] == "assistants=v2" - - def test_assemble_headers(self): - print("\nTesting _assemble_headers method...") - - # Mock request - mock_request = MagicMock(spec=Request) - api_key = "test_api_key" - - # Patch the _append_openai_beta_header method to avoid testing it again - with patch.object( - BaseOpenAIPassThroughHandler, - "_append_openai_beta_header", - return_value={ - "authorization": "Bearer test_api_key", - "api-key": "test_api_key", - "test-header": "value", - }, - ): - result = BaseOpenAIPassThroughHandler._assemble_headers(api_key, mock_request) - print(f"Assembled headers: {result}") - assert result["authorization"] == "Bearer test_api_key" - assert result["api-key"] == "test_api_key" - assert result["test-header"] == "value" + assert assembled == { + "authorization": "Bearer sk-upstream", + "api-key": "sk-upstream", + "OpenAI-Beta": "agents=v2", + } @patch( # test-quality-ok: patching litellm internal for unit test isolation "litellm.proxy.pass_through_endpoints.llm_passthrough_endpoints.create_pass_through_route" @@ -4057,6 +4024,129 @@ def test_openai_passthrough_forwards_verbatim_to_openai( assert route.calls.last.request.headers["authorization"] == "Bearer sk-upstream" +@pytest.mark.parametrize( + ("method", "client_base_path", "path", "client_headers", "upstream_openai_beta"), + [ + pytest.param( + "POST", "/openai/v1", "/agents/sessions", {"OpenAI-Beta": "agents=v1"}, ["agents=v1"], id="sdk_agents" + ), + pytest.param( + "GET", + "/openai_passthrough/v1", + "/agents/sessions/sess_123/events", + {"OpenAI-Beta": "agents=v1"}, + ["agents=v1"], + id="sdk_agents_events", + ), + pytest.param("POST", "/openai/v1", "/agents/sessions", {}, ["agents=v1"], id="agents_default"), + pytest.param( + "POST", + "/openai/v1", + "/agents/sessions", + {"OpenAI-Beta": "agents=v2"}, + ["agents=v2"], + id="client_value_beats_route_default", + ), + pytest.param("POST", "/openai", "/agents/sessions", {}, ["agents=v1"], id="agents_default_without_v1"), + pytest.param("GET", "/openai_passthrough/v1", "/vaults", {}, ["agents=v1"], id="vaults_default"), + pytest.param( + "POST", + "/openai/v1", + "/chatkit/sessions", + {"OpenAI-Beta": "chatkit_beta=v1"}, + ["chatkit_beta=v1"], + id="sdk_chatkit", + ), + pytest.param( + "POST", + "/openai/v1", + "/agents/sessions", + {"OpenAI-Beta": "agents=v1", "x-pass-openai-beta": "agents=v2"}, + ["agents=v2"], + id="x_pass_wins", + ), + pytest.param("POST", "/openai/v1", "/chatkit/threads", {}, [], id="chatkit_threads_no_default"), + pytest.param("POST", "/openai/v1", "/assistants", {}, ["assistants=v2"], id="assistants_default"), + pytest.param("POST", "/openai/v1", "/threads/thread_123/messages", {}, ["assistants=v2"], id="threads_default"), + pytest.param("POST", "/openai/v1", "/chat/completions", {}, [], id="no_beta"), + pytest.param( + "POST", + "/openai_passthrough/v1", + "/responses", + {"OpenAI-Beta": "12345"}, + [], + id="client_beta_dropped_off_beta_paths", + ), + pytest.param( + "POST", + "/openai/v1", + "/chat/completions", + {"OpenAI-Beta": "assistants=v2"}, + [], + id="client_beta_dropped_on_chat_completions", + ), + pytest.param( + "POST", + "/openai/v1", + "/threads/thread_123/messages", + {"OpenAI-Beta": "assistants=v1"}, + ["assistants=v2"], + id="assistants_client_beta_replaced", + ), + ], +) +def test_openai_passthrough_sends_the_openai_beta_header_upstream( + openai_passthrough_client: TestClient, + method: str, + client_base_path: str, + path: str, + client_headers: dict[str, str], + upstream_openai_beta: list[str], +) -> None: + with respx.mock(assert_all_called=True) as upstream: + route: Final = upstream.request(method, f"https://api.openai.com/v1{path}").mock( + return_value=httpx.Response(200, json={"id": "upstream_123"}) + ) + response: Final = openai_passthrough_client.request(method, f"{client_base_path}{path}", headers=client_headers) + + assert response.status_code == 200 + assert route.calls.last.request.headers.get_list("openai-beta") == upstream_openai_beta + + +@pytest.mark.parametrize( + ("method", "path", "client_headers", "upstream_openai_beta"), + [ + pytest.param("GET", "/openai/v1/vector_stores", {"OpenAI-Beta": "assistants=v2"}, [], id="client_beta_dropped"), + pytest.param("POST", "/v1/agents/sessions", {}, [], id="no_agents_default"), + pytest.param( + "POST", + "/openai/v1/threads/thread_123/messages", + {"OpenAI-Beta": "assistants=v1"}, + ["assistants=v2"], + id="assistants_default_kept", + ), + ], +) +def test_azure_passthrough_keeps_its_own_openai_beta_rule( + openai_passthrough_client: TestClient, + monkeypatch: pytest.MonkeyPatch, + method: str, + path: str, + client_headers: dict[str, str], + upstream_openai_beta: list[str], +) -> None: + monkeypatch.setenv("AZURE_API_BASE", "https://operator.openai.azure.com") + monkeypatch.setenv("AZURE_API_KEY", "operator-key") + with respx.mock(assert_all_called=True) as upstream: + route: Final = upstream.request(method, f"https://operator.openai.azure.com{path}").mock( + return_value=httpx.Response(200, json={"id": "upstream_123"}) + ) + response: Final = openai_passthrough_client.request(method, f"/azure{path}", headers=client_headers) + + assert response.status_code == 200 + assert route.calls.last.request.headers.get_list("openai-beta") == upstream_openai_beta + + @pytest.fixture def openai_wif_env(monkeypatch: pytest.MonkeyPatch, tmp_path) -> None: from litellm.llms.openai.workload_identity import _workload_identity_auth @@ -7306,9 +7396,7 @@ async def test_bedrock_native_count_tokens_logs_the_count_against_the_router_mod respx_mock: respx.MockRouter, token_count_log_lines: Callable[[], tuple[dict[str, object], ...]], ) -> None: - result: Final = await _bedrock_native_count( - monkeypatch, respx_mock, httpx.Response(200, json={"inputTokens": 12}) - ) + result: Final = await _bedrock_native_count(monkeypatch, respx_mock, httpx.Response(200, json={"inputTokens": 12})) assert result == {"input_tokens": 12} assert len(respx_mock.calls) == 1 @@ -7390,7 +7478,6 @@ async def test_bedrock_native_count_tokens_without_a_model_logs_the_400_it_answe ) - @pytest.mark.asyncio async def test_bedrock_native_count_tokens_with_a_region_only_model_answers_the_validation_error_and_logs_it( monkeypatch: pytest.MonkeyPatch, @@ -7433,6 +7520,7 @@ async def test_bedrock_native_count_tokens_with_a_region_only_model_answers_the_ }, ) + class _AzureGroupRouter: def __init__(self, captured: list[dict]) -> None: self.captured = captured diff --git a/tests/unit/proxy/pass_through_endpoints/test_pass_through_endpoints.py b/tests/unit/proxy/pass_through_endpoints/test_pass_through_endpoints.py index b0504af1b5e..df36d805377 100644 --- a/tests/unit/proxy/pass_through_endpoints/test_pass_through_endpoints.py +++ b/tests/unit/proxy/pass_through_endpoints/test_pass_through_endpoints.py @@ -10437,6 +10437,42 @@ def test_init_kwargs_no_authenticated_identity_field_is_client_settable(mock_req survived: Final = {key: metadata.get(key) for key in expected if metadata.get(key) != expected[key]} assert survived == {}, f"client-supplied values survived for: {sorted(survived)}" + +def test_init_kwargs_client_metadata_cannot_mark_a_pass_through_call_as_a_silent_experiment( + mock_user_api_key_dict: UserAPIKeyAuth, +) -> None: + from litellm.proxy.spend_tracking.spend_tracking_utils import get_logging_payload + + started: Final = datetime(2026, 1, 15, 12, 0, 0) + result: Final = HttpPassThroughEndpointHelpers._init_kwargs_for_pass_through_endpoint( + request=Request({"type": "http", "method": "POST", "path": "/test", "headers": [], "query_string": b""}), + user_api_key_dict=mock_user_api_key_dict, + passthrough_logging_payload=PassthroughStandardLoggingPayload(url="https://test.com", request_body={}), + litellm_call_id="test-call-id", + logging_obj=LiteLLMLoggingObj( + model="test-model", + messages=[], + stream=False, + call_type="test-call-type", + start_time=started, + litellm_call_id="test-call-id", + function_id="test-function-id", + ), + _parsed_body={ + "litellm_metadata": {"is_silent_experiment": True, "client_key": "kept"}, + "metadata": {"is_silent_experiment": True}, + }, + ) + + row: Final = get_logging_payload( + kwargs={"model": "test-model", "litellm_params": result["litellm_params"]}, + response_obj=litellm.ModelResponse(id="chatcmpl-test", choices=[]), + start_time=started, + end_time=started, + ) + assert "is_silent_experiment" not in json.loads(row["metadata"]) + assert result["litellm_params"]["metadata"]["client_key"] == "kept" + @pytest.mark.asyncio async def test_direct_passthrough_keeps_mapped_baggage_tags_outside_upstream_body(): from litellm.proxy.common_utils.http_parsing_utils import read_request_body diff --git a/tests/unit/proxy/pass_through_endpoints/test_passthrough_endpoint_router.py b/tests/unit/proxy/pass_through_endpoints/test_passthrough_endpoint_router.py index 9c50a6d4df6..ab4f3f7a675 100644 --- a/tests/unit/proxy/pass_through_endpoints/test_passthrough_endpoint_router.py +++ b/tests/unit/proxy/pass_through_endpoints/test_passthrough_endpoint_router.py @@ -485,7 +485,7 @@ def test_oidc_env_path_reference_is_not_resolved(tmp_path, monkeypatch): ) -def test_network_backed_oidc_reference_is_not_fetched_inline(monkeypatch): +def test_oidc_google_reference_is_refused_without_a_metadata_fetch(monkeypatch): def fail(*args, **kwargs): raise AssertionError("network-backed oidc reference was resolved inside the request path") @@ -494,11 +494,24 @@ def test_network_backed_oidc_reference_is_not_fetched_inline(monkeypatch): lambda name, *a, **k: "oidc/google/https://example.com" if name == "OPENAI_API_KEY" else fail(), ) - assert ( + with pytest.raises(HTTPException) as excinfo: _passthrough_router(None).get_credentials(custom_llm_provider="openai", region_name=None) - == "oidc/google/https://example.com" + + assert excinfo.value.status_code == 401 + assert "pass-through credential oidc/google" in str(excinfo.value.detail) + assert "example.com" not in str(excinfo.value.detail) + + +def test_deployment_oidc_google_reference_is_not_sent_upstream(monkeypatch): + llm_router = litellm.Router( + model_list=[_flagged_deployment("openai/gpt-4o", api_key="oidc/google/https://vllm.example.test")] ) + with pytest.raises(HTTPException) as excinfo: + _passthrough_router(llm_router).get_credentials(custom_llm_provider="openai", region_name=None) + + assert excinfo.value.status_code == 401 + @pytest.fixture() async def _drain_logging_worker(): diff --git a/tests/unit/proxy/proxy_server/test_lifecycle.py b/tests/unit/proxy/proxy_server/test_lifecycle.py index 8b1d7c1222a..a8706c5bccd 100644 --- a/tests/unit/proxy/proxy_server/test_lifecycle.py +++ b/tests/unit/proxy/proxy_server/test_lifecycle.py @@ -35,6 +35,7 @@ from typing_extensions import TypedDict import litellm.proxy.proxy_server as ps from litellm.proxy.management_helpers.audit_logs import track_audit_task from litellm.proxy.proxy_server import ( + _proxy_startup_event, ProxyStartupEvent, _resolve_pydantic_type, _resolve_typed_dict_type, @@ -1059,7 +1060,7 @@ def test_otel_global_provider_published_after_callback_init(): span was orphaned. The publish (``publish_global_otel_v2_provider``) must therefore appear after ``_initialize_startup_logging`` in the lifespan source. """ - wrapped = getattr(proxy_startup_event, "__wrapped__", proxy_startup_event) + wrapped = getattr(_proxy_startup_event, "__wrapped__", _proxy_startup_event) source = inspect.getsource(wrapped) init_pos = source.find("ProxyStartupEvent.initialize_startup_logging(") publish_pos = source.find("publish_global_otel_v2_provider(") @@ -1145,7 +1146,7 @@ def test_proxy_startup_event_warns_for_global_budget_without_database(): The call must follow Prisma setup so DB-backed deployments do not false-positive. Direct ``_warn_budget_without_db`` tests cover the warning behavior itself. """ - wrapped = getattr(proxy_startup_event, "__wrapped__", proxy_startup_event) + wrapped = getattr(_proxy_startup_event, "__wrapped__", _proxy_startup_event) source = inspect.getsource(wrapped) budget_check_pos = source.find("if prisma_client is not None and litellm.max_budget > 0:") warn_pos = source.find("warn_budget_without_db(") diff --git a/tests/unit/proxy/public_endpoints/test_public_endpoints.py b/tests/unit/proxy/public_endpoints/test_public_endpoints.py index e694bca3c8d..da608dbca0e 100644 --- a/tests/unit/proxy/public_endpoints/test_public_endpoints.py +++ b/tests/unit/proxy/public_endpoints/test_public_endpoints.py @@ -566,6 +566,7 @@ ADD_MODEL_UNLISTED_PROVIDERS: Final = frozenset( "mongodb", "nano-gpt", "neosantara", + "opensearch", "parasail", "pinstripes", "poe", diff --git a/tests/unit/proxy/rag_endpoints/test_rag_endpoints.py b/tests/unit/proxy/rag_endpoints/test_rag_endpoints.py index d9f3ceda1ed..bf4b5d5d77a 100644 --- a/tests/unit/proxy/rag_endpoints/test_rag_endpoints.py +++ b/tests/unit/proxy/rag_endpoints/test_rag_endpoints.py @@ -1671,6 +1671,62 @@ def test_rag_query_rejects_caller_embedding_selection_params(client_internal_use assert blocked_key in str(response.json()) +@pytest.mark.parametrize( + ("provider", "expected_status", "expected_searches"), + [("opensearch", 400, 0), ("opensearch/x", 400, 0), ("openai", 200, 1)], +) +def test_rag_query_rejects_caller_opensearch_index_name_only_on_opensearch_stores( + client_internal_user, provider, expected_status, expected_searches +): + """ + Regression: retrieval_config must not pick the index an OpenSearch store searches, + while other providers keep ignoring the key as they did before OpenSearch support. + """ + import litellm + from litellm import Router + from litellm.types.vector_stores import VectorStoreSearchResponse + + store = { + "vector_store_id": "kb", + "custom_llm_provider": provider, + "litellm_params": {"vector_store_id": "kb", "custom_llm_provider": provider}, + } + registry = MagicMock() + registry.get_litellm_managed_vector_store_from_registry.return_value = store + fake_search = AsyncMock( + return_value=VectorStoreSearchResponse(object="vector_store.search_results.page", search_query="q", data=[]) + ) + router = Router( + model_list=[ + { + "model_name": "gpt-4o-mini", + "litellm_params": {"model": "openai/gpt-4o-mini", "api_key": "sk-test", "mock_response": "hi"}, + } + ] + ) + + with ( + patch("litellm.vector_stores.asearch", new=fake_search), # test-quality-ok: the search boundary under test + patch.object(litellm, "vector_store_registry", registry), # test-quality-ok: seeds the store under test + patch("litellm.proxy.proxy_server.llm_router", router), # test-quality-ok: mock-response router for completion + patch( # test-quality-ok: store access is not under test, so the request reaches the search boundary + "litellm.proxy.vector_store_endpoints.utils.can_user_access_vector_store", + new=AsyncMock(return_value=True), + ), + ): + response = client_internal_user.post( + "/v1/rag/query", + json={ + "model": "gpt-4o-mini", + "messages": [{"role": "user", "content": "hello"}], + "retrieval_config": {"vector_store_id": "kb", "opensearch_index_name": "another-teams-index"}, + }, + ) + + assert response.status_code == expected_status, response.json() + assert fake_search.await_count == expected_searches + + EICAR = r"X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*" INGEST_REQUEST = '{"ingest_options":{"vector_store":{"custom_llm_provider":"openai"}}}' diff --git a/tests/unit/proxy/response_api_endpoints/test_endpoints.py b/tests/unit/proxy/response_api_endpoints/test_endpoints.py index e3d514b2ea9..b11dcc58ef6 100644 --- a/tests/unit/proxy/response_api_endpoints/test_endpoints.py +++ b/tests/unit/proxy/response_api_endpoints/test_endpoints.py @@ -2055,6 +2055,22 @@ def test_responses_ws_failure_frame_uses_the_http_status_for_the_same_failure( assert frame["error"]["message"] == handle_exception_on_proxy(failure).message +def test_responses_ws_failure_frame_shows_only_the_refusal_for_a_model_that_refuses_the_endpoint(): + from litellm.exceptions import EndpointNotSupportedByModelError + from litellm.proxy.response_api_endpoints.endpoints import _responses_ws_failure_frame + + refusal: Final = "/v1/responses is not supported by model=embed-only. Supported endpoints: /v1/embeddings" + failure: Final = EndpointNotSupportedByModelError( + message=f"{refusal}\n\nLiteLLM: model group 'embed-only' failed with the error above", model="embed-only" + ) + + assert json.loads(_responses_ws_failure_frame(failure)) == { + "type": "error", + "status": 400, + "error": {"type": "invalid_request_error", "message": refusal}, + } + + class TestWSSessionCostTracking: @pytest.mark.asyncio async def test_router_budget_limiter_skips_arealtime_call_type(self): diff --git a/tests/unit/proxy/shutdown/test_graceful_shutdown_manager.py b/tests/unit/proxy/shutdown/test_graceful_shutdown_manager.py index d38852617b9..fdf1adda1f4 100644 --- a/tests/unit/proxy/shutdown/test_graceful_shutdown_manager.py +++ b/tests/unit/proxy/shutdown/test_graceful_shutdown_manager.py @@ -179,3 +179,107 @@ async def test_emits_periodic_drain_waiting_log_while_waiting(): timeout=10, count_fn=count_fn, poll_interval=0, log_interval=0 ) assert drained == 3 + + +@pytest.mark.parametrize("elapsed, cap, expected", [(4, None, 26), (25, 2, 2), (29, 5, 1), (31, None, 0)]) +def test_remaining_budget_includes_time_before_lifespan( + monkeypatch: pytest.MonkeyPatch, elapsed: float, cap: float | None, expected: float +) -> None: + monkeypatch.setenv("GRACEFUL_SHUTDOWN_TIMEOUT", "30") + GracefulShutdownManager.start_shutdown(clock=lambda: 100.0) + GracefulShutdownManager.start_shutdown(clock=lambda: 100.0 + elapsed) + assert GracefulShutdownManager.remaining(cap, clock=lambda: 100.0 + elapsed) == expected + + +def test_remaining_budget_before_shutdown(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setenv("GRACEFUL_SHUTDOWN_TIMEOUT", "30") + assert GracefulShutdownManager.remaining(clock=lambda: 100.0) == 30.0 + assert GracefulShutdownManager.remaining(-1, clock=lambda: 100.0) == 0.0 + + +@pytest.mark.parametrize("prestop", [False, True]) +def test_sigterm_stamps_before_chaining_and_restores_server_handler( + monkeypatch: pytest.MonkeyPatch, prestop: bool +) -> None: + import signal + from queue import SimpleQueue + from types import FrameType + from typing import Final + + monkeypatch.setenv("GRACEFUL_SHUTDOWN_TIMEOUT", "30") + calls: Final[SimpleQueue[float]] = SimpleQueue() + + def server_handler(signum: int, frame: FrameType | None) -> None: + assert signum == signal.SIGTERM + calls.put(GracefulShutdownManager.remaining(clock=lambda: 105.0)) + + original: Final = signal.signal(signal.SIGTERM, server_handler) + try: + if prestop: + GracefulShutdownManager.start_shutdown(clock=lambda: 95.0) + with GracefulShutdownManager.capture_sigterm(clock=lambda: 100.0): + installed: Final = signal.getsignal(signal.SIGTERM) + with GracefulShutdownManager.capture_sigterm(clock=lambda: 102.0): + assert signal.getsignal(signal.SIGTERM) is installed + signal.raise_signal(signal.SIGTERM) + signal.raise_signal(signal.SIGTERM) + assert calls.get_nowait() == (20.0 if prestop else 25.0) + assert calls.get_nowait() == (20.0 if prestop else 25.0) + assert calls.empty() + assert signal.getsignal(signal.SIGTERM) is server_handler + finally: + signal.signal(signal.SIGTERM, original) + + +def test_sigterm_capture_preserves_a_replacement_handler() -> None: + import signal + from types import FrameType + from typing import Final + + def server_handler(signum: int, frame: FrameType | None) -> None: + pass + + original: Final = signal.signal(signal.SIGTERM, server_handler) + try: + with GracefulShutdownManager.capture_sigterm(): + signal.signal(signal.SIGTERM, signal.SIG_IGN) + assert signal.getsignal(signal.SIGTERM) is signal.SIG_IGN + finally: + signal.signal(signal.SIGTERM, original) + + +@pytest.mark.parametrize("handler", ["SIG_DFL", "SIG_IGN"]) +def test_sigterm_capture_preserves_default_and_ignored_dispositions(handler: str) -> None: + import signal + from typing import Final + + disposition: Final = signal.SIG_DFL if handler == "SIG_DFL" else signal.SIG_IGN + original: Final = signal.signal(signal.SIGTERM, disposition) + try: + with GracefulShutdownManager.capture_sigterm(): + assert signal.getsignal(signal.SIGTERM) is disposition + finally: + signal.signal(signal.SIGTERM, original) + + +def test_sigterm_capture_in_worker_thread_leaves_signal_ownership_unchanged() -> None: + import signal + from concurrent.futures import ThreadPoolExecutor + from typing import Final + + original: Final = signal.getsignal(signal.SIGTERM) + + def run_lifespan() -> None: + with GracefulShutdownManager.capture_sigterm(): + assert signal.getsignal(signal.SIGTERM) is original + + with ThreadPoolExecutor(max_workers=1) as executor: + executor.submit(run_lifespan).result() + assert signal.getsignal(signal.SIGTERM) is original + + +@pytest.mark.asyncio +async def test_lifespan_request_drain_does_not_restart_an_exhausted_budget(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setenv("GRACEFUL_SHUTDOWN_TIMEOUT", "30") + GracefulShutdownManager.start_shutdown(clock=lambda: 100.0) + assert await GracefulShutdownManager.wait_for_drain(count_fn=lambda: 2, clock=lambda: 130.0) == 2 diff --git a/tests/unit/proxy/spend_tracking/test_spend_tracking_utils.py b/tests/unit/proxy/spend_tracking/test_spend_tracking_utils.py index 86d064050eb..740334252d2 100644 --- a/tests/unit/proxy/spend_tracking/test_spend_tracking_utils.py +++ b/tests/unit/proxy/spend_tracking/test_spend_tracking_utils.py @@ -5384,6 +5384,28 @@ def test_get_logging_payload_router_rejected_request_takes_provider_from_model_g assert payload["custom_llm_provider"] == expected_provider +@pytest.mark.parametrize("client_model_group", [7, ["embed-only"], {"name": "embed-only"}]) +def test_get_logging_payload_drops_a_client_model_group_that_is_not_a_string(client_model_group: object): + payload: Final = get_logging_payload( + kwargs={ + "model": "embed-only", + "litellm_params": { + "metadata": {"user_api_key": "test-key", "model_group": client_model_group, "status": "failure"} + }, + "standard_logging_object": { + **_make_failed_request_standard_logging_payload(), + "model_group": client_model_group, + }, + }, + response_obj={}, + start_time=datetime.datetime.now(timezone.utc), + end_time=datetime.datetime.now(timezone.utc), + llm_router=None, + ) + + assert payload["model_group"] == "" + + def test_get_logging_payload_router_rejected_request_without_router_leaves_provider_empty(): assert _router_rejected_failure_payload("openai-group", None)["custom_llm_provider"] == "" @@ -6830,7 +6852,7 @@ def test_spend_logs_payload(model_id: str | None): ) assert len(payload["request_id"]) > 0 - expected_metadata_keys: Final = SpendLogsMetadata.__annotations__.keys() + expected_metadata_keys: Final = SpendLogsMetadata.__annotations__.keys() - {"is_silent_experiment"} assert "metadata" in payload assert isinstance(payload["metadata"], str) diff --git a/tests/unit/proxy/test_common_request_processing.py b/tests/unit/proxy/test_common_request_processing.py index fdcbcbfcd05..e58a1de9eb5 100644 --- a/tests/unit/proxy/test_common_request_processing.py +++ b/tests/unit/proxy/test_common_request_processing.py @@ -456,7 +456,7 @@ class TestProxyBaseLLMRequestProcessing: mock_add_litellm_data_to_request, ) mock_general_settings = {} - mock_user_api_key_dict = MagicMock(spec=UserAPIKeyAuth) + mock_user_api_key_dict = ProxyUserAPIKeyAuth() mock_proxy_config = MagicMock(spec=ProxyConfig) route_type = "acompletion" @@ -740,7 +740,7 @@ class TestProxyBaseLLMRequestProcessing: returned_data, logging_obj = await processing_obj.common_processing_pre_call_logic( request=mock_request, general_settings={}, - user_api_key_dict=MagicMock(spec=UserAPIKeyAuth), + user_api_key_dict=ProxyUserAPIKeyAuth(), proxy_logging_obj=mock_proxy_logging_obj, proxy_config=MagicMock(spec=ProxyConfig), route_type=route_type, @@ -1186,7 +1186,7 @@ class TestProxyBaseLLMRequestProcessing: ) mock_general_settings = {} - mock_user_api_key_dict = MagicMock(spec=UserAPIKeyAuth) + mock_user_api_key_dict = ProxyUserAPIKeyAuth() mock_proxy_config = MagicMock(spec=ProxyConfig) mock_router_settings = { @@ -2208,7 +2208,7 @@ class TestProxyBaseLLMRequestProcessing: mock_add_litellm_data_to_request, ) mock_general_settings = {} - mock_user_api_key_dict = MagicMock(spec=UserAPIKeyAuth) + mock_user_api_key_dict = ProxyUserAPIKeyAuth() mock_proxy_config = MagicMock(spec=ProxyConfig) route_type = "acompletion" @@ -7706,6 +7706,56 @@ class TestPreCallWithFallbacksOnLocalRateLimit: assert processor.data["model"] == fallback_model assert call_count == 2 + @pytest.mark.asyncio + async def test_local_rate_limit_fallback_skips_an_aliased_embedding_only_model(self): + from litellm.proxy.common_request_processing import ProxyBaseLLMRequestProcessing + from litellm.proxy.common_utils.proxy_rate_limit_error import ProxyRateLimitError + + router: Final = Router( + model_list=[ + { + "model_name": "embed-only", + "litellm_params": {"model": "openai/text-embedding-3-small", "api_key": "sk-synthetic"}, + "model_info": {"enforce_supported_endpoints": True, "supported_endpoints": ["/v1/embeddings"]}, + }, + {"model_name": "chat-model", "litellm_params": {"model": "openai/gpt-5.4-nano", "api_key": "sk-synthetic"}}, + ], + fallbacks=[{"rate-limited": ["embed-alias", "chat-model"]}], + ) + processor: Final = ProxyBaseLLMRequestProcessing( + data={ + "model": "rate-limited", + "router_settings_override": {"model_group_alias": {"embed-alias": "embed-only"}}, + } + ) + attempted: Final[list[object]] = [] + + async def pre_call_logic(**_: object): + attempted.append(processor.data.get("model")) + if processor.data.get("model") == "rate-limited": + raise ProxyRateLimitError(detail="key rpm limit exceeded", headers={"retry-after": "30"}) + return processor.data, MagicMock() + + with patch.object(processor, "common_processing_pre_call_logic", side_effect=pre_call_logic): + await processor._pre_call_with_fallbacks( + request=MagicMock(), + general_settings={}, + proxy_logging_obj=MagicMock(), + user_api_key_dict=ProxyUserAPIKeyAuth(), + version=None, + proxy_config=MagicMock(), + user_model=None, + user_temperature=None, + user_request_timeout=None, + user_max_tokens=None, + user_api_base=None, + model="rate-limited", + route_type="acompletion", + llm_router=router, + ) + + assert attempted == ["rate-limited", "chat-model"] + @pytest.mark.asyncio async def test_raises_when_no_fallbacks_configured(self): from litellm.proxy.common_utils.proxy_rate_limit_error import ProxyRateLimitError diff --git a/tests/unit/proxy/test_fallback_management_endpoints.py b/tests/unit/proxy/test_fallback_management_endpoints.py index 530d22f02be..4a0e8cb7a9a 100644 --- a/tests/unit/proxy/test_fallback_management_endpoints.py +++ b/tests/unit/proxy/test_fallback_management_endpoints.py @@ -10,16 +10,18 @@ Tests: import copy import json -from collections.abc import AsyncIterator -from types import SimpleNamespace -from typing import Final +from collections.abc import AsyncIterator, Mapping +from types import MappingProxyType, SimpleNamespace +from typing import Final, get_args from unittest.mock import AsyncMock, MagicMock, patch import pytest from fastapi import HTTPException from pydantic import JsonValue, TypeAdapter +from redis.exceptions import NoPermissionError from litellm import Router +from litellm.proxy.config_resolvers.settings_rules import Section from litellm.proxy.management_endpoints.fallback_management_endpoints import ( FallbackCreateRequest, FallbackDeleteResponse, @@ -29,10 +31,19 @@ from litellm.proxy.management_endpoints.fallback_management_endpoints import ( get_fallback, ) from litellm.proxy.proxy_server import ProxyConfig -from litellm.proxy.utils import evict_config_param, get_config_param +from litellm.proxy.utils import evict_config_param, get_config_param, litellm_config_cache from litellm.types.router import DeploymentTypedDict, LiteLLMParamsTypedDict +@pytest.fixture(autouse=True) +async def empty_config_cache() -> AsyncIterator[None]: + for section in get_args(Section): + await evict_config_param(section) + yield + for section in get_args(Section): + await evict_config_param(section) + + class TestFallbackCreateRequest: """Test the FallbackCreateRequest validation""" @@ -123,7 +134,7 @@ PRIMARY_DEPLOYMENT_ID: Final = "team-primary-id" FALLBACK_DEPLOYMENT_ID: Final = "team-fallback-id" FallbackRules = list[dict[str, list[str]] | dict[str, object] | str] -RouterSettings = dict[str, FallbackRules | None] +RouterSettings = dict[str, FallbackRules | str | None] _ROUTER_SETTINGS_JSON: Final = TypeAdapter(dict[str, JsonValue]) @@ -168,17 +179,18 @@ class TestCreateFallbackForTeamScopedModels: @pytest.fixture def prisma_client(self) -> MagicMock: client: Final = MagicMock() + client.writer_db = client.db + client.db.litellm_config.find_unique = AsyncMock(return_value=None) client.db.litellm_config.upsert = AsyncMock() + client.get_generic_data = AsyncMock(return_value=None) return client @pytest.fixture - def proxy_config(self) -> MagicMock: - config: Final = MagicMock() - config.get_config = AsyncMock(return_value={"router_settings": {}}) - return config + def proxy_config(self) -> ProxyConfig: + return _ConfigFileProxyConfig() async def _create( - self, request: FallbackCreateRequest, router: Router, prisma_client: MagicMock, proxy_config: MagicMock + self, request: FallbackCreateRequest, router: Router, prisma_client: MagicMock, proxy_config: ProxyConfig ) -> FallbackResponse: with ( patch("litellm.proxy.proxy_server.llm_router", router), @@ -254,26 +266,43 @@ def _config_row(router_settings: RouterSettings) -> SimpleNamespace: return SimpleNamespace(param_name="router_settings", param_value=router_settings) +class _ConfigFileProxyConfig(ProxyConfig): + def __init__(self, config_file: RouterSettings | None = None) -> None: + super().__init__() + self.config_file: RouterSettings = config_file or {} + self.router_settings.load_yaml(_ROUTER_SETTINGS_JSON.validate_python(self.config_file)) + + async def _get_config_from_file(self, config_file_path: str | None = None) -> dict[str, RouterSettings]: + return {"router_settings": copy.deepcopy(self.config_file)} + + class _StoredRouterSettings: """The LiteLLM_Config router_settings row, with the proxy objects that read it through the config cache""" - def __init__(self, router_settings: RouterSettings | None, config_file: RouterSettings | None = None) -> None: + def __init__( + self, + router_settings: RouterSettings | None, + config_file: RouterSettings | None = None, + *, + lagging_replica: bool = False, + ) -> None: self.row: SimpleNamespace | None = None if router_settings is None else _config_row(router_settings) self.prisma_client: Final = MagicMock() - self.prisma_client.get_generic_data = AsyncMock(side_effect=lambda **_: self.row) + self.prisma_client.get_generic_data = AsyncMock( + side_effect=lambda value, **_: self.row if value == "router_settings" else None + ) + self.prisma_client.writer_db.litellm_config.find_unique = AsyncMock( + side_effect=lambda **_: copy.deepcopy(self.row) + ) + self.prisma_client.db.litellm_config.find_unique = AsyncMock( + side_effect=lambda **_: None if lagging_replica else copy.deepcopy(self.row) + ) self.prisma_client.db.litellm_config.upsert = AsyncMock(side_effect=self._upsert) - self.proxy_config: Final = MagicMock() if config_file is None else ProxyConfig() - if config_file is not None: - self.proxy_config.router_settings.load_yaml(_ROUTER_SETTINGS_JSON.validate_python(config_file)) - self.proxy_config.get_config = AsyncMock(side_effect=self._get_config) + self.proxy_config: Final = _ConfigFileProxyConfig(config_file) async def _upsert(self, where: dict[str, str], data: dict[str, dict[str, str]]) -> None: self.row = _config_row(json.loads(data["update"]["param_value"])) - async def _get_config(self) -> dict[str, RouterSettings]: - row: Final = await get_config_param(self.prisma_client, "router_settings") - return {"router_settings": copy.deepcopy(row.param_value) if row is not None else {}} - def written_by_another_instance(self, router_settings: RouterSettings) -> None: self.row = _config_row(router_settings) @@ -295,15 +324,23 @@ NON_STANDARD_RULES: Final = [ ] +class _RedisThatRefusesDel: + def __init__(self) -> None: + self.values: Mapping[str, object] = MappingProxyType({}) + + async def async_get_cache(self, key: str, parent_otel_span: object = None) -> object: + return self.values.get(key) + + async def async_set_cache(self, key: str, value: object, **_: object) -> None: + self.values = MappingProxyType({**self.values, key: value}) + + async def async_delete_cache(self, key: str) -> None: + raise NoPermissionError("this user has no permissions to run the 'del' command") + + class TestFallbackWritesSeeTheLatestStoredRules: """A write reads the rules the database holds now and leaves no stale copy in the config cache behind""" - @pytest.fixture(autouse=True) - async def clean_config_cache(self) -> AsyncIterator[None]: - await evict_config_param("router_settings") - yield - await evict_config_param("router_settings") - async def _create(self, request: FallbackCreateRequest, stored: _StoredRouterSettings) -> FallbackResponse: with ( patch("litellm.proxy.proxy_server.llm_router", _team_router()), @@ -322,6 +359,13 @@ class TestFallbackWritesSeeTheLatestStoredRules: ): return await delete_fallback(model, "general", MagicMock()) + async def test_a_create_reads_the_rules_from_the_writer_while_the_replica_lags(self) -> None: + stored: Final = _StoredRouterSettings({"fallbacks": [TEAM_RULE]}, lagging_replica=True) + + await self._create(FallbackCreateRequest(model="gpt-5.4-mini", fallback_models=["team-fallback"]), stored) + + assert stored.stored_fallbacks() == [TEAM_RULE, GATEWAY_RULE] + async def test_a_second_create_keeps_the_first_rule(self) -> None: stored: Final = _StoredRouterSettings(None) @@ -331,6 +375,17 @@ class TestFallbackWritesSeeTheLatestStoredRules: assert stored.stored_fallbacks() == [TEAM_RULE, GATEWAY_RULE] assert await stored.cached_fallbacks() == [TEAM_RULE, GATEWAY_RULE] + async def test_a_second_create_keeps_the_first_rule_when_redis_refuses_to_evict_the_cached_settings(self) -> None: + stored: Final = _StoredRouterSettings(None) + + with patch.object(litellm_config_cache, "redis_cache", _RedisThatRefusesDel()): + await self._create(FallbackCreateRequest(model="team-primary", fallback_models=["team-fallback"]), stored) + await self._create(FallbackCreateRequest(model="gpt-5.4-mini", fallback_models=["team-fallback"]), stored) + await self._delete("team-primary", stored) + await self._create(FallbackCreateRequest(model="team-primary", fallback_models=["team-fallback"]), stored) + + assert stored.stored_fallbacks() == [GATEWAY_RULE, TEAM_RULE] + async def test_a_create_keeps_a_rule_another_instance_stored_since_this_one_last_read(self) -> None: stored: Final = _StoredRouterSettings({}) await get_config_param(stored.prisma_client, "router_settings") @@ -380,6 +435,15 @@ class TestFallbackWritesSeeTheLatestStoredRules: assert stored.stored_fallbacks() == [GATEWAY_RULE] + @pytest.mark.parametrize("param_value", [None, ["team-primary"], 7, "not json"]) + async def test_a_create_replaces_a_stored_row_that_is_not_an_object(self, param_value: JsonValue) -> None: + stored: Final = _StoredRouterSettings(None) + stored.row = SimpleNamespace(param_name="router_settings", param_value=param_value) + + await self._create(FallbackCreateRequest(model="gpt-5.4-mini", fallback_models=["team-fallback"]), stored) + + assert stored.row.param_value == {"fallbacks": [GATEWAY_RULE]} + CONFIG_RULE: Final = {"gpt-5.4-mini": ["team-fallback"]} CONFIG_WINDOW_RULE: Final = {"team-primary": ["gpt-5.4-mini"]} @@ -438,6 +502,18 @@ class TestFallbackWritesHonourTheConfigFile: stored.prisma_client.db.litellm_config.upsert.assert_not_awaited() assert router.fallbacks == [CONFIG_RULE] + async def test_a_create_after_the_config_file_takes_over_the_key_is_refused(self, router: Router) -> None: + stored: Final = _StoredRouterSettings({"fallbacks": [TEAM_RULE]}) + stored.proxy_config.config_file = {"fallbacks": [CONFIG_RULE]} + request: Final = FallbackCreateRequest(model="team-primary", fallback_models=["gpt-5.4-mini"]) + + with pytest.raises(HTTPException) as refused: + await self._create(request, router, stored) + + assert refused.value.status_code == 400 + assert "key 'fallbacks' is set in the config file" in str(refused.value.detail) + assert stored.stored_fallbacks() == [TEAM_RULE] + async def test_a_delete_of_a_config_owned_rule_is_refused( self, router: Router, stored: _StoredRouterSettings ) -> None: @@ -452,6 +528,57 @@ class TestFallbackWritesHonourTheConfigFile: stored.prisma_client.db.litellm_config.upsert.assert_not_awaited() assert router.fallbacks == [CONFIG_RULE] + async def test_a_delete_of_a_rule_only_the_config_file_holds_is_refused(self, router: Router) -> None: + stored: Final = _StoredRouterSettings(None, config_file={"fallbacks": [CONFIG_RULE]}) + router.fallbacks = [CONFIG_RULE] + + with pytest.raises(HTTPException) as refused: + await self._delete("gpt-5.4-mini", router, stored) + + assert refused.value.status_code == 400 + assert "key 'fallbacks' is set in the config file" in str(refused.value.detail) + assert stored.row is None + assert router.fallbacks == [CONFIG_RULE] + + async def test_writes_to_a_config_file_env_reference_that_resolves_to_nothing_are_refused( + self, router: Router, monkeypatch: pytest.MonkeyPatch + ) -> None: + monkeypatch.delenv("LITELLM_TEST_UNSET_FALLBACKS", raising=False) + stored: Final = _StoredRouterSettings(None, config_file={"fallbacks": "os.environ/LITELLM_TEST_UNSET_FALLBACKS"}) + + with pytest.raises(HTTPException) as refused: + await self._create(FallbackCreateRequest(model="team-primary", fallback_models=["team-fallback"]), router, stored) + with pytest.raises(HTTPException) as not_found: + await self._delete("team-primary", router, stored) + + assert refused.value.status_code == 400 + assert "key 'fallbacks' is set in the config file" in str(refused.value.detail) + assert not_found.value.status_code == 404 + assert stored.row is None + + async def test_a_create_that_restates_one_of_several_config_rules_writes_nothing(self, router: Router) -> None: + stored: Final = _StoredRouterSettings(None, config_file={"fallbacks": [CONFIG_RULE, CONFIG_WINDOW_RULE]}) + request: Final = FallbackCreateRequest(model="gpt-5.4-mini", fallback_models=["team-fallback"]) + + response: Final = await self._create(request, router, stored) + + assert response.fallback_models == ["team-fallback"] + assert stored.row is None + assert router.fallbacks == [CONFIG_RULE, CONFIG_WINDOW_RULE] + + async def test_a_create_that_restates_a_config_rule_next_to_an_env_reference_writes_nothing( + self, router: Router, monkeypatch: pytest.MonkeyPatch + ) -> None: + monkeypatch.setenv("LITELLM_TEST_FALLBACK_MODEL", "team-fallback") + env_rule: Final = {"team-primary": "os.environ/LITELLM_TEST_FALLBACK_MODEL"} + stored: Final = _StoredRouterSettings(None, config_file={"fallbacks": [CONFIG_RULE, env_rule]}) + request: Final = FallbackCreateRequest(model="gpt-5.4-mini", fallback_models=["team-fallback"]) + + response: Final = await self._create(request, router, stored) + + assert response.fallback_models == ["team-fallback"] + assert stored.row is None + async def test_a_create_that_restates_the_config_rule_is_accepted( self, router: Router, stored: _StoredRouterSettings ) -> None: @@ -462,6 +589,20 @@ class TestFallbackWritesHonourTheConfigFile: assert response.fallback_models == ["team-fallback"] assert stored.stored_fallbacks() == [CONFIG_RULE] + async def test_a_write_stores_only_the_database_rules_never_the_config_files_router_settings( + self, router: Router + ) -> None: + stored: Final = _StoredRouterSettings( + {"fallbacks": [TEAM_RULE]}, config_file={"routing_strategy": "latency-based-routing"} + ) + + await self._create(FallbackCreateRequest(model="gpt-5.4-mini", fallback_models=["team-fallback"]), router, stored) + assert stored.row is not None + assert stored.row.param_value == {"fallbacks": [TEAM_RULE, GATEWAY_RULE]} + + await self._delete("team-primary", router, stored) + assert stored.row.param_value == {"fallbacks": [GATEWAY_RULE]} + async def test_a_sibling_list_the_config_file_does_not_set_stays_writable( self, router: Router, stored: _StoredRouterSettings ) -> None: @@ -496,16 +637,17 @@ class TestCreateFallback: def mock_prisma_client(self): """Create a mock prisma client""" client = MagicMock() + client.writer_db = client.db + client.db.litellm_config.find_unique = AsyncMock(return_value=None) client.db.litellm_config.upsert = AsyncMock() + client.get_generic_data = AsyncMock(return_value=None) client.jsonify_object = lambda x: x return client @pytest.fixture def mock_proxy_config(self): """Create a mock proxy config""" - config = MagicMock() - config.get_config = AsyncMock(return_value={"router_settings": {}}) - return config + return _ConfigFileProxyConfig() @pytest.fixture def mock_user_api_key_dict(self): @@ -803,22 +945,19 @@ class TestDeleteFallback: def mock_prisma_client(self): """Create a mock prisma client""" client = MagicMock() + client.writer_db = client.db + client.db.litellm_config.find_unique = AsyncMock( + return_value=_config_row({"fallbacks": [{"gpt-3.5-turbo": ["gpt-4", "claude-3-haiku"]}]}) + ) client.db.litellm_config.upsert = AsyncMock() + client.get_generic_data = AsyncMock(return_value=None) client.jsonify_object = lambda x: x return client @pytest.fixture def mock_proxy_config(self): """Create a mock proxy config""" - config = MagicMock() - config.get_config = AsyncMock( - return_value={ - "router_settings": { - "fallbacks": [{"gpt-3.5-turbo": ["gpt-4", "claude-3-haiku"]}] - } - } - ) - return config + return _ConfigFileProxyConfig() @pytest.fixture def mock_user_api_key_dict(self): diff --git a/tests/unit/proxy/test_proxy_cli.py b/tests/unit/proxy/test_proxy_cli.py index afefae0dbb1..1db334039ec 100644 --- a/tests/unit/proxy/test_proxy_cli.py +++ b/tests/unit/proxy/test_proxy_cli.py @@ -4393,3 +4393,20 @@ def test_granian_flag_notices_are_json_records_in_json_mode( "LiteLLM: --ciphers is not applied when using --run_granian.", ] assert [record["level"] for record in records] == ["INFO", "WARNING", "WARNING"] + + +def test_migration_data_check_exits_zero_when_there_is_nothing_to_dry_run(monkeypatch, capfd): + monkeypatch.setenv("DATABASE_URL", "postgresql://user:pass@db.example/litellm") + + assert ProxyInitializationHelpers.run_migration_data_check(use_migrate=False) == 0 + + out, err = capfd.readouterr() + assert out == "LiteLLM Proxy: The schema is pushed rather than migrated, there is nothing to dry-run.\n" + assert err == "" + + +def test_migration_data_check_requires_database_url(monkeypatch): + monkeypatch.delenv("DATABASE_URL", raising=False) + + with pytest.raises(click.UsageError, match="requires DATABASE_URL"): + ProxyInitializationHelpers.run_migration_data_check(use_migrate=True) diff --git a/tests/unit/proxy/test_proxy_server.py b/tests/unit/proxy/test_proxy_server.py index 22d6316c448..c835e1e27ea 100644 --- a/tests/unit/proxy/test_proxy_server.py +++ b/tests/unit/proxy/test_proxy_server.py @@ -263,6 +263,174 @@ async def test_shutdown_commits_spend_a_logging_task_queued_while_the_first_comm token = MASTER_KEY + +@pytest.mark.asyncio +async def test_shutdown_persists_accounting_from_worker_callbacks_before_returning() -> None: + from litellm.litellm_core_utils.logging_worker import LoggingWorker + from litellm.responses.streaming_iterator import spawn_logging_task + + worker: Final = LoggingWorker() + started: Final = asyncio.Event() + release: Final = asyncio.Event() + first_flush: Final = asyncio.Event() + pending: Final[asyncio.Queue[str]] = asyncio.Queue() + persisted: Final[asyncio.Queue[str]] = asyncio.Queue() + + async def enqueue_spend() -> None: + pending.put_nowait("accounted request") + + async def callback() -> None: + started.set() + await release.wait() + spawn_logging_task(asyncio.get_running_loop(), enqueue_spend(), task_name="accounting") + + async def flush_spend() -> None: + while not pending.empty(): + persisted.put_nowait(pending.get_nowait()) + first_flush.set() + + worker.ensure_initialized_and_enqueue(callback()) + await started.wait() + shutdown: Final = asyncio.create_task( + litellm.proxy.proxy_server._flush_spend_around_logging_drain_on_shutdown(flush_spend, worker=worker) + ) + try: + await first_flush.wait() + waited_for_callback: Final = not shutdown.done() + release.set() + await shutdown + assert waited_for_callback, "Shutdown returned while accounting callbacks were still running" + assert persisted.get_nowait() == "accounted request" + assert pending.empty() + finally: + release.set() + await worker.flush() + await worker.stop() + + +@pytest.mark.asyncio +async def test_shutdown_reports_worker_timeout_and_flushes_completed_accounting( + monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture +) -> None: + from litellm.litellm_core_utils.logging_worker import LoggingWorker + from litellm.proxy.shutdown.graceful_shutdown_manager import GracefulShutdownManager + + monkeypatch.setenv("GRACEFUL_SHUTDOWN_TIMEOUT", "30") + worker: Final = LoggingWorker() + started: Final = asyncio.Event() + cancelled: Final = asyncio.Event() + flushed_after_cancellation: Final = asyncio.Event() + GracefulShutdownManager.start_shutdown(clock=lambda: 100.0) + + async def callback() -> None: + started.set() + try: + await asyncio.Event().wait() + finally: + cancelled.set() + + async def flush_spend() -> None: + if cancelled.is_set(): + flushed_after_cancellation.set() + + worker.ensure_initialized_and_enqueue(callback()) + await started.wait() + try: + with caplog.at_level(logging.WARNING, logger="LiteLLM Proxy"): + await litellm.proxy.proxy_server._flush_spend_around_logging_drain_on_shutdown( + flush_spend, worker=worker, clock=lambda: 120.0 + ) + assert cancelled.is_set() + assert flushed_after_cancellation.is_set() + assert "budget exhausted with 1 unfinished callbacks" in caplog.text + finally: + GracefulShutdownManager.reset() + await worker.stop() + + +@pytest.mark.asyncio +async def test_shutdown_bounds_callback_cancellation_and_flushes_accounting( + monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture +) -> None: + from litellm.litellm_core_utils.logging_worker import LoggingWorker + from litellm.proxy.shutdown.graceful_shutdown_manager import GracefulShutdownManager + + monkeypatch.setenv("GRACEFUL_SHUTDOWN_TIMEOUT", "30") + worker: Final = LoggingWorker() + started: Final = asyncio.Event() + cancelling: Final = asyncio.Event() + release: Final = asyncio.Event() + finished: Final = asyncio.Event() + flushed: Final = asyncio.Event() + GracefulShutdownManager.start_shutdown(clock=lambda: 100.0) + + async def callback() -> None: + started.set() + try: + await release.wait() + except asyncio.CancelledError: + cancelling.set() + await release.wait() + finally: + finished.set() + + async def drain_tasks(timeout: float) -> None: + assert timeout == 0.0 + await cancelling.wait() + + async def flush_spend() -> None: + if cancelling.is_set(): + flushed.set() + + worker.ensure_initialized_and_enqueue(callback()) + await started.wait() + try: + with caplog.at_level(logging.WARNING, logger="LiteLLM"): + await litellm.proxy.proxy_server._flush_spend_around_logging_drain_on_shutdown( + flush_spend, worker=worker, clock=lambda: 130.0, drain_tasks=drain_tasks + ) + assert not finished.is_set(), "The cancellation-resistant callback must remain observable" + assert flushed.is_set() + assert "tasks still alive after cancellation" in caplog.text + finally: + release.set() + await worker.flush() + await worker.stop() + GracefulShutdownManager.reset() + + +@pytest.mark.asyncio +async def test_shutdown_preserves_persistence_reserve_after_partial_http_drain( + monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture +) -> None: + from litellm.litellm_core_utils.logging_worker import LoggingWorker + from litellm.proxy.shutdown.graceful_shutdown_manager import GracefulShutdownManager + + monkeypatch.setenv("GRACEFUL_SHUTDOWN_TIMEOUT", "30") + worker: Final = LoggingWorker() + persisted: Final = asyncio.Event() + drain_budgets: Final[asyncio.Queue[float]] = asyncio.Queue() + GracefulShutdownManager.start_shutdown(clock=lambda: 100.0) + + async def flush_spend() -> None: + persisted.set() + + async def drain_tasks(timeout: float) -> None: + drain_budgets.put_nowait(timeout) + + try: + with caplog.at_level(logging.INFO, logger="LiteLLM Proxy"): + await litellm.proxy.proxy_server._flush_spend_around_logging_drain_on_shutdown( + flush_spend, worker=worker, clock=lambda: 106.0, drain_tasks=drain_tasks + ) + assert "remaining_s=24.000 callback_timeout_s=14.000" in caplog.text + assert persisted.is_set() + assert tuple(drain_budgets.get_nowait() for _ in range(drain_budgets.qsize())) == (14.0, 5.0) + finally: + await worker.stop() + GracefulShutdownManager.reset() + + headers = {"Authorization": f"Bearer {token}"} example_completion_result = { @@ -4059,3 +4227,34 @@ def test_detailed_debug_logs_request_body_only_as_received_and_as_sent( assert len(body_dumps) == 2, [message[:120] for message in body_dumps] assert body_dumps[0].startswith("Request received by LiteLLM:") assert "POST Request Sent from LiteLLM" in body_dumps[1] + + +@pytest.mark.asyncio +async def test_shutdown_waits_for_enqueue_producers_before_stopping_worker() -> None: + from litellm.litellm_core_utils.logging_worker import LoggingWorker + from litellm.responses.streaming_iterator import spawn_logging_task + + worker: Final = LoggingWorker() + persisted: Final[asyncio.Queue[str]] = asyncio.Queue() + accounting: Final[asyncio.Queue[str]] = asyncio.Queue() + + async def callback() -> None: + accounting.put_nowait("completed request") + + async def producer() -> None: + await asyncio.sleep(0) + await asyncio.sleep(0) + worker.ensure_initialized_and_enqueue(callback()) + + async def flush_spend() -> None: + while not accounting.empty(): + persisted.put_nowait(accounting.get_nowait()) + + spawn_logging_task(asyncio.get_running_loop(), producer(), task_name="success enqueue") + try: + await litellm.proxy.proxy_server._flush_spend_around_logging_drain_on_shutdown(flush_spend, worker=worker) + assert persisted.qsize() == 1, "An enqueue producer must finish before callback draining stops" + assert persisted.get_nowait() == "completed request" + assert worker.pending_count == 0 + finally: + await worker.stop(discard_pending=True) diff --git a/tests/unit/proxy/test_proxy_token_counter.py b/tests/unit/proxy/test_proxy_token_counter.py index b21730c64f6..d0cf7be794a 100644 --- a/tests/unit/proxy/test_proxy_token_counter.py +++ b/tests/unit/proxy/test_proxy_token_counter.py @@ -279,6 +279,39 @@ async def test_token_counter_reads_an_oidc_api_key_only_when_it_calls_the_provid assert str(tmp_path) not in excinfo.value.message +@pytest.mark.asyncio +async def test_token_counter_answers_503_when_the_metadata_server_behind_an_oidc_google_api_key_fails( + monkeypatch: pytest.MonkeyPatch, +): + audience: Final = f"https://count.test/{uuid.uuid4().hex}" + monkeypatch.setattr( + litellm.proxy.proxy_server, + "llm_router", + Router( + model_list=[ + { + "model_name": "claude", + "litellm_params": {"model": "anthropic/claude-sonnet-5-5", "api_key": f"oidc/google/{audience}"}, + } + ] + ), + ) + + with respx.mock(assert_all_called=True) as upstream: + upstream.get(url__regex=r"/computeMetadata/v1/instance/service-accounts/default/identity").respond( + 500, text="metadata server unavailable" + ) + with pytest.raises(ProxyException) as excinfo: + await token_counter( + request=TokenCountRequest(model="claude", messages=[{"role": "user", "content": "hello"}]), + call_endpoint=True, + ) + + assert excinfo.value.code == "503" + assert "oidc/google secret reference could not be resolved" in excinfo.value.message + assert audience.removeprefix("https://") not in excinfo.value.message + + @pytest.mark.asyncio async def test_native_openai_responses_count_sends_the_token_an_oidc_api_key_points_to( tmp_path: Path, monkeypatch: pytest.MonkeyPatch diff --git a/tests/unit/proxy/test_route_llm_request.py b/tests/unit/proxy/test_route_llm_request.py index 9e31d3774f9..8f364e51e2b 100644 --- a/tests/unit/proxy/test_route_llm_request.py +++ b/tests/unit/proxy/test_route_llm_request.py @@ -28,6 +28,7 @@ from litellm.proxy.route_llm_request import ( JSON_OBJECT_ADAPTER, ProxyModelNotFoundError, UserConfigRouters, + endpoint_unsupported_by_model, route_request, user_config_routers, ) @@ -3027,3 +3028,27 @@ async def test_user_config_routers_discard_the_least_recently_used_router_past_c assert first_handler() is not None assert second_handler() is None routers.clear() + + +def test_fallback_probe_skips_only_a_loaded_model_that_refuses_the_route() -> None: + router: Final = litellm.Router( + model_list=[ + { + "model_name": "chat-fallback", + "litellm_params": {"model": "openai/gpt-5.4-nano", "api_key": "unit-provider-key"}, + }, + { + "model_name": "embed-only", + "litellm_params": {"model": "openai/text-embedding-3-small", "api_key": "unit-provider-key"}, + "model_info": {"enforce_supported_endpoints": True, "supported_endpoints": ["/v1/embeddings"]}, + }, + ], + num_retries=0, + ) + + def skipped(model: str) -> bool: + return endpoint_unsupported_by_model("acompletion", {"model": model}, router, None) + + assert skipped("embed-only") is True + assert skipped("chat-fallback") is False + assert skipped("added-on-another-worker") is False diff --git a/tests/unit/proxy/utils/proxy_logging/test_guardrail_pipeline.py b/tests/unit/proxy/utils/proxy_logging/test_guardrail_pipeline.py index acc1463b072..cfd4fe197be 100644 --- a/tests/unit/proxy/utils/proxy_logging/test_guardrail_pipeline.py +++ b/tests/unit/proxy/utils/proxy_logging/test_guardrail_pipeline.py @@ -41,6 +41,7 @@ from litellm.types.proxy.policy_engine.pipeline_types import ( GuardrailPipeline, PipelineStep, ) +from litellm.types.utils import TextChoices, TextCompletionResponse @pytest.fixture(autouse=True) @@ -1886,7 +1887,7 @@ def test_streamable_post_call_pipelines_keeps_hook_guardrails_and_drops_iterator @pytest.mark.parametrize( "request_route", - ["/v1/completions", "/v1beta/models/gemini-2.5-flash:streamGenerateContent", "/a2a/agent"], + ["/v1beta/models/gemini-2.5-flash:streamGenerateContent", "/a2a/agent"], ) def test_streamable_post_call_pipelines_keeps_legacy_hooks_off_routes_that_assemble_no_response( make_user_api_key_auth, monkeypatch, caplog, request_route @@ -2036,6 +2037,120 @@ async def test_streaming_iterator_hook_delivers_legacy_hook_rewrite_on_every_cho assert _warnings(caplog) == [] +def _text_completion_chunk(text: str, finish_reason: str | None = None) -> TextCompletionResponse: + return TextCompletionResponse( + id="cmpl-1", model="m", choices=[TextChoices(index=0, text=text, finish_reason=finish_reason)] + ) + + +def _rewritten_text_completion(response: Any) -> TextCompletionResponse: + return TextCompletionResponse( + id=response.id, + model=response.model, + choices=[TextChoices(index=0, text="[REWRITTEN] " + response.choices[0].text, finish_reason="stop")], + ) + + +@pytest.mark.asyncio +async def test_streaming_iterator_hook_delivers_legacy_hook_rewrite_on_a_text_completion_stream( + proxy_logging, make_user_api_key_auth, monkeypatch, caplog +): + seen: Dict[str, Any] = {} + monkeypatch.setattr(litellm, "callbacks", [_legacy_hook_stream_guardrail(seen, rewrite=_rewritten_text_completion)]) + monkeypatch.setattr("litellm.proxy.proxy_server.llm_router", None, raising=False) + data = {**_post_call_pipeline_data(stream=True), "prompt": "hi"} + chunks = [_text_completion_chunk("hello "), _text_completion_chunk("world"), _text_completion_chunk("", "stop")] + auth = make_user_api_key_auth(request_route="/v1/completions") + + with caplog.at_level(logging.WARNING, logger="LiteLLM Proxy"): + await proxy_logging.pre_call_hook( + user_api_key_dict=auth, data=data, call_type="text_completion", guardrails_only=True + ) + delivered = [ + item + async for item in proxy_logging.async_post_call_streaming_iterator_hook( + user_api_key_dict=auth, response=_async_chunk_iter(chunks), request_data=data + ) + ] + + assert isinstance(seen["response"], TextCompletionResponse) + assert seen["response"].choices[0].text == "hello world" + assert [(item.choices[0].text, item.choices[0].finish_reason) for item in delivered] == [ + ("[REWRITTEN] hello world", None), + ("", None), + ("", "stop"), + ] + assert data["metadata"]["applied_guardrails"] == ["gr-post"] + assert _warnings(caplog) == [] + + +class _FlatBlockGuardrail(CustomGuardrail): + async def apply_guardrail(self, inputs, request_data, input_type, logging_obj=None): + if any("kumquat" in text for text in inputs.get("texts") or []): + raise HTTPException(status_code=400, detail="flat guardrail blocked kumquat") + return inputs + + +@pytest.mark.asyncio +async def test_streaming_iterator_hook_delivers_a_flat_guardrail_block_on_a_text_completion_stream_under_a_pipeline( + proxy_logging, make_user_api_key_auth, monkeypatch +): + seen: Final[Dict[str, Any]] = {} + flat: Final = _FlatBlockGuardrail( + guardrail_name="flat-block", event_hook=GuardrailEventHooks.post_call, default_on=True + ) + monkeypatch.setattr(litellm, "callbacks", [flat, _live_scan_guardrail(seen)]) + monkeypatch.setattr("litellm.proxy.proxy_server.llm_router", None, raising=False) + data: Final = {**_post_call_pipeline_data(stream=True), "prompt": "hi"} + chunks: Final = [ + _text_completion_chunk("A kum"), + _text_completion_chunk("quat tart"), + _text_completion_chunk("", "stop"), + ] + auth: Final = make_user_api_key_auth(request_route="/v1/completions") + + await proxy_logging.pre_call_hook(user_api_key_dict=auth, data=data, call_type="text_completion", guardrails_only=True) + delivered: Final = [ + item + async for item in proxy_logging.async_post_call_streaming_iterator_hook( + user_api_key_dict=auth, response=_async_chunk_iter(chunks), request_data=data + ) + ] + + assert seen["texts"] == ["A kumquat tart"] + assert isinstance(delivered[-1], bytes) + assert json.loads(delivered[-1].decode().removeprefix("data: "))["error"]["message"] == ( + "flat guardrail blocked kumquat" + ) + + +@pytest.mark.asyncio +async def test_streaming_iterator_hook_live_pipeline_scans_what_a_disconnected_text_completion_client_received( + proxy_logging, make_user_api_key_auth, monkeypatch +): + seen: Final[Dict[str, Any]] = {} + monkeypatch.setattr(litellm, "callbacks", [_live_scan_guardrail(seen)]) + monkeypatch.setattr("litellm.proxy.proxy_server.llm_router", None, raising=False) + data: Final = {**_post_call_pipeline_data(step=_DETECT_ONLY_STEP, stream=True), "prompt": "hi"} + chunks: Final = [ + _text_completion_chunk("hello "), + _text_completion_chunk("world"), + _text_completion_chunk("", "stop"), + ] + auth: Final = make_user_api_key_auth(request_route="/v1/completions") + + await proxy_logging.pre_call_hook(user_api_key_dict=auth, data=data, call_type="text_completion", guardrails_only=True) + stream: Final = proxy_logging.async_post_call_streaming_iterator_hook( + user_api_key_dict=auth, response=_async_chunk_iter(chunks), request_data=data + ) + first: Final = await stream.__anext__() + await stream.aclose() + + assert first.choices[0].text == "hello " + assert seen["texts"] == ["hello "] + assert _post_call_statuses(data) == ["success"] + + @pytest.mark.asyncio async def test_streaming_iterator_hook_releases_stream_untouched_when_legacy_hook_returns_none( proxy_logging, make_user_api_key_auth, monkeypatch @@ -2615,6 +2730,45 @@ async def test_streaming_iterator_hook_pipeline_modify_response_emits_translated assert not any(item is chunk for item in delivered for chunk in chunks) +@pytest.mark.asyncio +async def test_streaming_iterator_hook_pipeline_modify_response_replaces_a_text_completion_stream( + proxy_logging, make_user_api_key_auth, monkeypatch +): + seen: Final[Dict[str, Any]] = {} + monkeypatch.setattr(litellm, "callbacks", [_unified_stream_guardrail(seen, block=True)]) + monkeypatch.setattr("litellm.proxy.proxy_server.llm_router", None, raising=False) + pipeline: Final = GuardrailPipeline( + mode="post_call", + steps=[ + PipelineStep( + guardrail="gr-post", + on_pass="allow", + on_fail="modify_response", + modify_response_message="content policy block", + ) + ], + ) + data: Final = {**_post_call_pipeline_data(stream=True), "prompt": "hi"} + data["metadata"]["_guardrail_pipelines"] = [("response-governance", pipeline)] + chunks: Final = [_text_completion_chunk("hello "), _text_completion_chunk("world"), _text_completion_chunk("", "stop")] + auth: Final = make_user_api_key_auth(request_route="/v1/completions") + + await proxy_logging.pre_call_hook(user_api_key_dict=auth, data=data, call_type="text_completion", guardrails_only=True) + delivered: Final = [ + item + async for item in proxy_logging.async_post_call_streaming_iterator_hook( + user_api_key_dict=auth, response=_async_chunk_iter(chunks), request_data=data + ) + ] + + frames: Final = [json.loads(item.decode().removeprefix("data: ")) for item in delivered] + assert seen["count"] == 1 + assert [(frame["choices"][0]["text"], frame["choices"][0].get("finish_reason")) for frame in frames] == [ + ("content policy block", None), + ("", "content_filter"), + ] + + @pytest.mark.asyncio async def test_streaming_iterator_hook_pipeline_delivers_text_rewrite_on_anthropic_sse( proxy_logging, make_user_api_key_auth, monkeypatch diff --git a/tests/unit/proxy/vector_store_endpoints/test_vector_store_endpoints.py b/tests/unit/proxy/vector_store_endpoints/test_vector_store_endpoints.py index 48642a9b50d..e128b5eb1f1 100644 --- a/tests/unit/proxy/vector_store_endpoints/test_vector_store_endpoints.py +++ b/tests/unit/proxy/vector_store_endpoints/test_vector_store_endpoints.py @@ -3066,6 +3066,58 @@ def test_vector_store_search_rejects_caller_embedding_selection_params(blocked_k assert blocked_key in str(response.json()) +@pytest.mark.parametrize( + ("provider", "expected_status", "expected_searches"), + [("opensearch", 400, 0), ("opensearch/x", 400, 0), ("openai", 200, 1)], +) +def test_vector_store_search_rejects_caller_opensearch_index_name_only_on_opensearch_stores( + provider, expected_status, expected_searches +): + """ + Regression: a caller must not pick the index an OpenSearch store searches, while + other providers keep ignoring the key as they did before OpenSearch support. + """ + from fastapi.testclient import TestClient + + from litellm.proxy.auth.user_api_key_auth import user_api_key_auth + from litellm.proxy.proxy_server import app + from litellm.types.vector_stores import VectorStoreSearchResponse + + store = { + "vector_store_id": "kb", + "custom_llm_provider": provider, + "litellm_params": {"vector_store_id": "kb", "custom_llm_provider": provider}, + } + registry = MagicMock() + registry.get_litellm_managed_vector_store_from_registry.return_value = store + fake_search = AsyncMock( + return_value=VectorStoreSearchResponse(object="vector_store.search_results.page", search_query="q", data=[]) + ) + mock_auth = UserAPIKeyAuth(user_id="test_internal_user", user_role=LitellmUserRoles.INTERNAL_USER.value) + original_overrides = app.dependency_overrides.copy() + app.dependency_overrides[user_api_key_auth] = lambda: mock_auth + try: + with ( + patch("litellm.vector_stores.main.asearch", new=fake_search), # test-quality-ok: the search boundary under test + patch.object(litellm, "vector_store_registry", registry), # test-quality-ok: seeds the store under test + patch( # test-quality-ok: store access is not under test, so the request reaches the search boundary + "litellm.proxy.vector_store_endpoints.utils.can_user_access_vector_store", + new=AsyncMock(return_value=True), + ), + ): + router = litellm.Router(model_list=[]) + with patch("litellm.proxy.proxy_server.llm_router", router): # test-quality-ok: router built over the fake search + response = TestClient(app).post( + "/v1/vector_stores/kb/search", + json={"query": "q", "opensearch_index_name": "another-teams-index"}, + ) + finally: + app.dependency_overrides = original_overrides + + assert response.status_code == expected_status, response.json() + assert fake_search.await_count == expected_searches + + class TestConfigOwnedVectorStores: """Stores declared under ``vector_store_registry`` in config.yaml are owned by the config file""" diff --git a/tests/unit/realtime_api/test_main.py b/tests/unit/realtime_api/test_main.py index 5478b807691..46777b32319 100644 --- a/tests/unit/realtime_api/test_main.py +++ b/tests/unit/realtime_api/test_main.py @@ -618,7 +618,11 @@ async def test_bearer_health_check_sends_the_secret_behind_an_oidc_credential( monkeypatch.setattr( litellm, "credential_list", - [CredentialItem(credential_name="voice-sa", credential_values={"api_key": f"oidc/file/{token}"}, credential_info={})], + [ + CredentialItem( + credential_name="voice-sa", credential_values={"api_key": f"oidc/file/{token}"}, credential_info={} + ) + ], ) connect: Final = _CapturingConnect() with patch("websockets.connect", connect): @@ -1447,3 +1451,98 @@ async def test_openai_realtime_health_check_sends_the_federated_bearer_for_a_key model_params={"model": "openai/gpt-realtime", **federated_deployment}, ) assert connect.kwargs["additional_headers"] == {"Authorization": "Bearer federated-bearer"} + + +@pytest.mark.asyncio +async def test_arealtime_routes_soniox_with_the_deployment_options(monkeypatch): + from litellm.llms.soniox.realtime.transformation import SonioxRealtimeConfig + + captured: dict[str, object] = {} + + async def mock_async_realtime(**kwargs): + captured.update(kwargs) + + monkeypatch.setattr(realtime_main.base_llm_http_handler, "async_realtime", mock_async_realtime) + await realtime_main._arealtime.__wrapped__( + model="soniox/stt-rt-v5", + websocket=MagicMock(), + litellm_logging_obj=FakeLogging(), + query_params={"model": "soniox/stt-rt-v5", "intent": "transcription"}, + api_key="deployment-key", + language_hints=["de"], + ) + + provider_config = captured["provider_config"] + assert isinstance(provider_config, SonioxRealtimeConfig) + assert captured["model"] == "stt-rt-v5" + assert captured["api_key"] == "deployment-key" + start_request = provider_config.transform_realtime_request( + json.dumps({"type": "input_audio_buffer.commit"}), "stt-rt-v5" + )[0] + assert json.loads(start_request)["language_hints"] == ["de"] + + +async def _serve_soniox(reply: str | None) -> tuple[object, list[object]]: + import websockets + + received: list[object] = [] + + async def handler(connection) -> None: + received.append(connection.request.headers["Authorization"]) + received.append(json.loads(await connection.recv())) + received.append(await connection.recv()) + received.append(await connection.recv()) + if reply is not None: + await connection.send(reply) + + return await websockets.serve(handler, "127.0.0.1", 0), received + + +@pytest.mark.asyncio +async def test_soniox_realtime_health_check_starts_and_ends_a_stream_on_the_deployment_websocket(): + server, received = await _serve_soniox(json.dumps({"tokens": [], "finished": True})) + port = next(iter(server.sockets)).getsockname()[1] + async with server: + healthy = await realtime_main.realtime_health_check( + model="stt-rt-v5", + custom_llm_provider="soniox", + api_key="soniox-key", + api_base=f"http://127.0.0.1:{port}", + model_params={"model": "soniox/stt-rt-v5", "language_hints": ["fa", "en"]}, + ) + + assert healthy is True + assert received[0] == "Bearer soniox-key" + assert received[1]["model"] == "stt-rt-v5" + assert received[1]["language_hints"] == ["fa", "en"] + assert received[2] == bytes(len(received[2])) + assert len(received[2]) > 0 + assert received[3] == "" + + +@pytest.mark.asyncio +async def test_soniox_realtime_health_check_fails_when_soniox_refuses_the_stream(): + from litellm.llms.soniox.common_utils import SonioxException + + server, _ = await _serve_soniox(json.dumps({"error_code": 401, "error_message": "Invalid API key."})) + port = next(iter(server.sockets)).getsockname()[1] + async with server: + with pytest.raises(SonioxException) as refused: + await realtime_main.realtime_health_check( + model="stt-rt-v5", custom_llm_provider="soniox", api_key="bad-key", api_base=f"ws://127.0.0.1:{port}" + ) + + assert refused.value.status_code == 401 + + +@pytest.mark.asyncio +async def test_soniox_realtime_health_check_fails_when_soniox_closes_without_finishing_the_stream(): + from litellm.llms.soniox.common_utils import SonioxException + + server, _ = await _serve_soniox(None) + port = next(iter(server.sockets)).getsockname()[1] + async with server: + with pytest.raises(SonioxException): + await realtime_main.realtime_health_check( + model="stt-rt-v5", custom_llm_provider="soniox", api_key="soniox-key", api_base=f"ws://127.0.0.1:{port}" + ) diff --git a/tests/unit/repositories/test_daily_activity_sql.py b/tests/unit/repositories/test_daily_activity_sql.py index c57db08e976..f8375b93dad 100644 --- a/tests/unit/repositories/test_daily_activity_sql.py +++ b/tests/unit/repositories/test_daily_activity_sql.py @@ -123,7 +123,6 @@ def test_key_page_sql_orders_exact_spend_and_binds_scope_before_page() -> None: 7, ) assert "SUM(spend::numeric) AS rank_spend" in query.sql - assert "ORDER BY rank_spend DESC, api_key" in query.sql assert "(SELECT COUNT(*) FROM ranked)::bigint AS total_api_keys" in query.sql @@ -329,7 +328,6 @@ def test_entity_rollup_bounds_keys_and_reuses_scope_filters() -> None: assert "api_key = ANY($3::text[])" in query.sql assert query.sql.count("api_key = ANY($3::text[])") == 4 assert query.sql.count("api_key <> $4") == 2 - assert query.sql.count("ORDER BY SUM(spend::numeric) DESC, api_key") == 1 assert "k.entity_id = e.entity_id" in query.sql assert "LIMIT $5" in query.sql assert query.params == ("2026-01-01", "2026-01-31", ["key-1", "key-2"], PTU_SENTINEL_API_KEY, 3) @@ -351,13 +349,11 @@ def test_search_query_escapes_pattern_metacharacters_and_binds_limit() -> None: assert 'LEFT JOIN "LiteLLM_UserTable" u ON u.user_id = d.user_id' in query.sql assert "d.key_alias ILIKE $3 ESCAPE" in query.sql assert "d.user_id ILIKE $3 ESCAPE" in query.sql - assert "api_key ILIKE $3 ESCAPE" in query.sql assert "v.key_alias ILIKE $3 ESCAPE" in query.sql assert "v.user_id ILIKE $3 ESCAPE" in query.sql assert "u.user_email ILIKE $3 ESCAPE" in query.sql assert query.sql.count("ILIKE $3 ESCAPE") == 7 assert "api_key <> $4" in query.sql - assert "ORDER BY SUM(spend::numeric) DESC, api_key" in query.sql assert "LIMIT $5" in query.sql assert query.params == ( "2026-01-01", @@ -374,12 +370,9 @@ def test_model_and_cache_key_queries_bind_filters_sentinel_and_limits() -> None: ) leakage_query = build_cache_leakage_keys_sql(_scope(entity_ids=None), limit=20) - assert "COALESCE(NULLIF(model_group, ''), model) = $3" in model_query.sql assert "api_key <> $4" in model_query.sql - assert "ORDER BY SUM(spend::numeric) DESC, api_key" in model_query.sql assert model_query.params == ("2026-01-01", "2026-01-31", "public-model", PTU_SENTINEL_API_KEY, 5) assert "HAVING SUM(prompt_tokens) - SUM(cache_read_input_tokens) > 0" in leakage_query.sql - assert "ORDER BY SUM(prompt_tokens) - SUM(cache_read_input_tokens) DESC, api_key" in leakage_query.sql assert leakage_query.params == ("2026-01-01", "2026-01-31", PTU_SENTINEL_API_KEY, 20) @@ -401,8 +394,14 @@ def test_query_builders_reject_nonpositive_limits(builder) -> None: ("export_type", "group_key", "key_filter", "joins"), [ (ExportType.DAILY, "''", "", ""), - (ExportType.DAILY_WITH_KEYS, "scoped.api_key", "api_key <> $3", 'LEFT JOIN "LiteLLM_VerificationToken"'), - (ExportType.DAILY_WITH_MODELS, "COALESCE(scoped.model, '')", "api_key <> $3", ""), + ( + ExportType.DAILY_WITH_KEYS, + "CASE WHEN api_key = '' THEN 'unattributed' " + "WHEN api_key = 'unattributed' OR api_key LIKE 'raw:%' THEN 'raw:' || api_key ELSE api_key END", + "api_key <> $3", + 'LEFT JOIN "LiteLLM_VerificationToken"', + ), + (ExportType.DAILY_WITH_MODELS, "COALESCE(NULLIF(model, ''), 'unknown-model')", "api_key <> $3", ""), ( ExportType.DAILY_WITH_USERS, "COALESCE(vt.user_id, dvt.user_id, '')", diff --git a/tests/unit/router_strategy/test_auto_router.py b/tests/unit/router_strategy/test_auto_router.py index 6cdee407ced..de775b9246f 100644 --- a/tests/unit/router_strategy/test_auto_router.py +++ b/tests/unit/router_strategy/test_auto_router.py @@ -398,6 +398,19 @@ def _auto_router(routelayer: Any, litellm_router_instance: Any = None, **kwargs: return auto_router +@pytest.mark.parametrize("routes", [{}, ""]) +def test_should_load_no_routes_from_an_empty_non_list_routes_value(routes: object) -> None: + auto_router: Final = AutoRouter( + model_name="my-auto-router", + auto_router_config=json.dumps({"routes": routes}), + default_model="fallback-model", + embedding_model="text-embedding-3-small", + litellm_router_instance=StubEmbeddingRouter(), + ) + + assert auto_router.loaded_routes == [] + + class TestAutoRouterAlwaysResolvesARoutableModel: """The hook returns the alias's default model instead of failing or leaking the alias downstream.""" diff --git a/tests/unit/router_strategy/test_budget_limiter_hotpath.py b/tests/unit/router_strategy/test_budget_limiter_hotpath.py index 10704a29347..c79ff6325d2 100644 --- a/tests/unit/router_strategy/test_budget_limiter_hotpath.py +++ b/tests/unit/router_strategy/test_budget_limiter_hotpath.py @@ -2,6 +2,7 @@ import asyncio import contextlib import gc import logging +import threading from types import SimpleNamespace from typing import Final from unittest.mock import AsyncMock, MagicMock @@ -301,6 +302,27 @@ def test_router_add_deployment_registers_deployment_budget(disable_budget_sync, assert config.max_budget == 0.000000000001 +def _leave_a_task_pending_on_a_closed_loop() -> None: + def park_a_task_then_close_the_loop() -> None: + loop: Final = asyncio.new_event_loop() + + async def park() -> None: + await loop.create_future() + + loop.create_task(park()) + loop.call_soon(loop.stop) + loop.run_forever() + loop.close() + + parker: Final = threading.Thread(target=park_a_task_then_close_the_loop) + parker.start() + parker.join() + + +def _non_asyncio_log_lines(caplog: pytest.LogCaptureFixture) -> list[str]: + return [record.getMessage() for record in caplog.records if record.name != "asyncio"] + + @pytest.mark.asyncio async def test_sync_refused_by_the_open_circuit_breaker_is_quiet_and_leaks_no_task(disable_budget_sync, caplog): """The budget sync runs every second, so an open breaker must not add an error line or an unretrieved task exception per cycle.""" @@ -322,11 +344,12 @@ async def test_sync_refused_by_the_open_circuit_breaker_is_quiet_and_leaks_no_ta with caplog.at_level(logging.ERROR): await limiter._sync_in_memory_spend_with_redis() await asyncio.sleep(0) + _leave_a_task_pending_on_a_closed_loop() gc.collect() finally: loop.set_exception_handler(None) - assert caplog.records == [] + assert _non_asyncio_log_lines(caplog) == [] unretrieved.assert_not_called() assert limiter.redis_increment_operation_queue == [ {"key": "provider_spend:openai:1d", "increment_value": 0.5, "ttl": 60} @@ -382,11 +405,12 @@ async def test_push_task_failure_is_logged_once_and_not_leaked(disable_budget_sy with caplog.at_level(logging.ERROR): await limiter._push_in_memory_increments_to_redis() await asyncio.sleep(0) + _leave_a_task_pending_on_a_closed_loop() gc.collect() finally: loop.set_exception_handler(None) - assert [record.getMessage() for record in caplog.records if record.name != "asyncio"] == [ + assert _non_asyncio_log_lines(caplog) == [ "Error syncing in-memory cache with Redis: Error 61 connecting to 127.0.0.1:6379" ] unretrieved.assert_not_called() diff --git a/tests/unit/router_utils/test_endpoint_eligibility.py b/tests/unit/router_utils/test_endpoint_eligibility.py new file mode 100644 index 00000000000..5e5d77254d0 --- /dev/null +++ b/tests/unit/router_utils/test_endpoint_eligibility.py @@ -0,0 +1,139 @@ +from collections.abc import Mapping +from typing import Final + +import pytest +from pydantic import TypeAdapter + +from litellm.exceptions import EndpointNotSupportedByModelError +from litellm.router_utils.endpoint_eligibility import ( + eligible_deployments, + endpoint_selection, + warn_on_unenforced_endpoint_flag, +) + +_DEPLOYMENT: Final = TypeAdapter(list[dict[str, object]]) + + +def _deployment(info: Mapping[str, object], identity: str = "embedding") -> dict[str, object]: + return _DEPLOYMENT.validate_python( + [ + { + "model_name": "shared", + "litellm_params": {"model": "openai/test-model"}, + "model_info": {"id": identity, **info}, + } + ] + )[0] + + +@pytest.mark.parametrize("call_type", ["acompletion", "anthropic_messages", "aresponses", "acreate_interaction"]) +def test_explicit_restriction_excludes_an_embedding_deployment_from_a_mixed_group(call_type: str) -> None: + embedding: Final = _deployment({"enforce_supported_endpoints": True, "supported_endpoints": ["/v1/embeddings"]}) + chat: Final = _deployment({"supported_endpoints": ["/v1/chat/completions"]}, "chat") + with endpoint_selection(call_type): + result: Final = eligible_deployments("shared", (embedding, chat)) + assert result == (chat,) + + +@pytest.mark.parametrize( + "info", + [ + {"mode": "audio_speech", "supported_endpoints": ["/v1/audio/speech"]}, + {"enforce_supported_endpoints": False, "supported_endpoints": ["/v1/embeddings"]}, + {"enforce_supported_endpoints": True}, + {"enforce_supported_endpoints": True, "supported_endpoints": None}, + {"enforce_supported_endpoints": True, "supported_endpoints": 7}, + {"enforce_supported_endpoints": True, "supported_endpoints": ["/v1/messages"]}, + ], +) +def test_advisory_or_incomplete_metadata_preserves_routing(info: Mapping[str, object]) -> None: + deployment: Final = _deployment(info) + with endpoint_selection("acompletion"): + result: Final = eligible_deployments("shared", (deployment,)) + assert result == (deployment,) + + +def test_a_single_endpoint_string_is_enforced_like_a_one_item_list() -> None: + deployment: Final = _deployment({"enforce_supported_endpoints": True, "supported_endpoints": "/v1/embeddings"}) + with endpoint_selection("acompletion"): + result: Final = eligible_deployments("shared", (deployment,)) + assert isinstance(result, EndpointNotSupportedByModelError) + assert result.message.endswith("Supported endpoints: /v1/embeddings") + + +def test_unsupported_error_explains_the_requested_operation_and_available_endpoints() -> None: + deployment: Final = _deployment({"enforce_supported_endpoints": True, "supported_endpoints": ["/v1/embeddings"]}) + with endpoint_selection("acreate_interaction"): + result: Final = eligible_deployments("shared", (deployment,)) + assert isinstance(result, EndpointNotSupportedByModelError) + assert result.status_code == 400 + assert "/v1beta/interactions" in result.message + assert "/v1/embeddings" in result.message + + +@pytest.mark.parametrize("call_type", ["aembedding", "aspeech", "_arealtime", "aget_responses"]) +def test_chat_restriction_does_not_block_other_operations(call_type: str) -> None: + deployment: Final = _deployment({"enforce_supported_endpoints": True, "supported_endpoints": ["/v1/embeddings"]}) + with endpoint_selection(call_type): + result: Final = eligible_deployments("shared", (deployment,)) + assert result == (deployment,) + + +def test_nested_non_chat_calls_do_not_inherit_the_outer_chat_restriction() -> None: + deployment: Final = _deployment({"enforce_supported_endpoints": True, "supported_endpoints": ["/v1/embeddings"]}) + with endpoint_selection("acompletion"): + with endpoint_selection("aembedding"): + embedding_result: Final = eligible_deployments("shared", (deployment,)) + chat_result: Final = eligible_deployments("shared", (deployment,)) + assert embedding_result == (deployment,) + assert isinstance(chat_result, EndpointNotSupportedByModelError) + + +@pytest.mark.parametrize( + "endpoint", + [ + "/messages", + "/interactions", + "/responses/compact", + "/openai/v1/responses/compact", + "/openai/v1/chat/completions", + "chat/completions", + "v1/responses", + " /v1/chat/completions ", + "/v1/chat/completions/", + "/V1/Chat/Completions", + "/v1beta/models/gemini-3-flash:generateContent", + ], +) +def test_chat_endpoint_declarations_keep_their_meaning_across_prefixes(endpoint: str) -> None: + deployment: Final = _deployment({"enforce_supported_endpoints": True, "supported_endpoints": [endpoint]}) + with endpoint_selection("acompletion"): + result: Final = eligible_deployments("shared", (deployment,)) + assert result == (deployment,) + + +@pytest.mark.parametrize( + "info", + [ + {"enforce_supported_endpoints": True}, + {"enforce_supported_endpoints": True, "supported_endpoints": ["/v1/embeddings", None]}, + {"enforce_supported_endpoints": "true", "supported_endpoints": 7}, + ], +) +def test_a_flag_without_a_usable_endpoint_list_is_reported(info: Mapping[str, object]) -> None: + warning: Final = warn_on_unenforced_endpoint_flag("embed-only", info) + assert warning is not None + assert "'embed-only'" in warning + + +@pytest.mark.parametrize( + "info", + [ + {"enforce_supported_endpoints": True, "supported_endpoints": ["/v1/embeddings"]}, + {"enforce_supported_endpoints": True, "supported_endpoints": "/v1/embeddings"}, + {"enforce_supported_endpoints": False}, + {"supported_endpoints": ["/v1/embeddings", None]}, + ], +) +def test_an_enforceable_or_unflagged_deployment_is_not_reported(info: Mapping[str, object]) -> None: + assert warn_on_unenforced_endpoint_flag("embed-only", info) is None diff --git a/tests/unit/router_utils/test_reasoning_effort_capability.py b/tests/unit/router_utils/test_reasoning_effort_capability.py index eaa0cfed561..ce4cb84addf 100644 --- a/tests/unit/router_utils/test_reasoning_effort_capability.py +++ b/tests/unit/router_utils/test_reasoning_effort_capability.py @@ -626,8 +626,9 @@ class TestResolveDeploymentReasoningEffort: _sent("turbo\nforged line", drop_params=True) assert [record.getMessage() for record in caplog.records] == [ - "reasoning_effort unrecognized sent as None (rounding up) to deployment deployment-1, " - "model group kimi-k3, model hosted_vllm/my-kimi" + "Dropping reasoning level unrecognized (drop_params=True) for deployment deployment-1, model group " + "kimi-k3, model hosted_vllm/my-kimi because its model_info.reasoning_effort_levels does not list that " + "level. List every level the deployment supports." ] def test_line_breaks_in_logged_names_cannot_forge_a_log_line(self, caplog: pytest.LogCaptureFixture) -> None: @@ -669,6 +670,27 @@ class TestResolveDeploymentReasoningEffort: "reasoning_effort_rounding": "up", } + def test_only_a_dropped_level_logs_a_warning(self, caplog: pytest.LogCaptureFixture) -> None: + verbose_logger.addHandler(caplog.handler) + verbose_logger.propagate = False + verbose_logger.setLevel(logging.INFO) + declared: Final = {"reasoning_effort_levels": ["low", "high"]} + + _sent("none", declared, drop_params=True) + _sent("medium") + _sent("default", declared, drop_params=True) + _sent("default", declared) + + assert [record.levelno for record in caplog.records] == [ + logging.WARNING, + logging.INFO, + logging.INFO, + logging.INFO, + ] + dropped: Final = caplog.records[0].getMessage() + assert "Dropping reasoning level none" in dropped + assert "model_info.reasoning_effort_levels" in dropped + class TestAzureGpt6SolAndLunaAdvertiseTheOpenAiLevels: @pytest.mark.parametrize( diff --git a/tests/unit/router_utils/test_router_health_check_routing.py b/tests/unit/router_utils/test_router_health_check_routing.py index 46ed679f746..b1498ea3687 100644 --- a/tests/unit/router_utils/test_router_health_check_routing.py +++ b/tests/unit/router_utils/test_router_health_check_routing.py @@ -53,10 +53,10 @@ class TestFilterHealthCheckUnhealthyDeployments: class FakeRouter: def __init__(self): - self.enable_health_check_routing = enable - self.health_state_cache = health_cache - self.allowed_fails_policy = None - self.background_health_check_model_groups = model_groups + self._core_enable_health_check_routing = enable + self._core_health_state_cache = health_cache + self._core_allowed_fails_policy = None + self._core_background_health_check_model_groups = model_groups # Import the actual method and bind it from litellm.router import Router @@ -169,10 +169,10 @@ class TestAsyncFilterHealthCheckUnhealthyDeployments: class FakeRouter: def __init__(self): - self.enable_health_check_routing = enable - self.health_state_cache = health_cache - self.allowed_fails_policy = None - self.background_health_check_model_groups = model_groups + self._core_enable_health_check_routing = enable + self._core_health_state_cache = health_cache + self._core_allowed_fails_policy = None + self._core_background_health_check_model_groups = model_groups fake = FakeRouter() fake._async_filter_health_check_unhealthy_deployments = ( diff --git a/tests/unit/secret_managers/test_secret_managers_main.py b/tests/unit/secret_managers/test_secret_managers_main.py index 862658d9169..a2e5b498f34 100644 --- a/tests/unit/secret_managers/test_secret_managers_main.py +++ b/tests/unit/secret_managers/test_secret_managers_main.py @@ -4,6 +4,7 @@ import logging import os import threading import time +import uuid from collections.abc import Callable from concurrent.futures import ThreadPoolExecutor from queue import SimpleQueue @@ -11,6 +12,7 @@ from typing import Final from unittest.mock import Mock, patch import pytest +import respx import litellm from litellm._logging import redact_internal_details_from_client_message @@ -37,7 +39,7 @@ class MockHTTPHandler: self.text = "mocked_token" self.json_data = {"value": "mocked_token"} - def get(self, url, params=None, headers=None): + def get(self, url, params=None, headers=None, timeout=None): # Store params for audience verification self.last_params = params logger.debug( @@ -49,6 +51,9 @@ class MockHTTPHandler: mock_response.json.return_value = self.json_data return mock_response + def close(self): + pass + @pytest.fixture def mock_oidc_cache(): @@ -229,6 +234,41 @@ def test_oidc_google_failure(): get_secret(secret_name) + +def test_oidc_google_fetches_the_token_from_the_metadata_host_gce_metadata_host_names( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setenv("GCE_METADATA_HOST", "127.0.0.1:8930") + audience: Final = f"https://vllm.example.test/{uuid.uuid4().hex}" + + with respx.mock(assert_all_called=True) as metadata: + metadata.get( + "http://127.0.0.1:8930/computeMetadata/v1/instance/service-accounts/default/identity", + params={"audience": audience}, + headers={"Metadata-Flavor": "Google"}, + ).respond(200, text="metadata-host-token") + token: Final = get_secret(f"oidc/google/{audience}") + + assert token == "metadata-host-token" + +def test_oidc_google_secret_outside_a_model_api_key_keeps_its_metadata_wait_when_gce_metadata_timeout_is_set( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setenv("GCE_METADATA_TIMEOUT", "1") + audience: Final = f"https://sts.example.test/{uuid.uuid4().hex}" + + with respx.mock(assert_all_called=True) as metadata: + identity: Final = metadata.get( + "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity", + params={"audience": audience}, + ).respond(200, text="web-identity-token") + token: Final = get_secret(f"oidc/google/{audience}") + waits: Final = identity.calls.last.request.extensions["timeout"] + + assert token == "web-identity-token" + assert waits == {"connect": 5.0, "read": 600.0, "write": 600.0, "pool": 600.0} + + def test_oidc_circleci_success(monkeypatch): monkeypatch.setenv("CIRCLE_OIDC_TOKEN", "circleci_token") diff --git a/tests/unit/test_anthropic_beta_gated_fields.py b/tests/unit/test_anthropic_beta_gated_fields.py new file mode 100644 index 00000000000..a2b384b4ac4 --- /dev/null +++ b/tests/unit/test_anthropic_beta_gated_fields.py @@ -0,0 +1,301 @@ +import logging +from typing import Final + +import pytest + +import litellm +from litellm.anthropic_beta_gated_fields import ( + ANTHROPIC_INLINE_TOOLS_BETA_HEADER, + ANTHROPIC_MID_CONVERSATION_SYSTEM_CLEAR_AT_BETA_HEADER, + ANTHROPIC_THINKING_BINDING_CONTROLS_BETA_HEADER, + BETA_GATED_FIELDS, + active_beta_values, + log_beta_gated_strips, + strip_beta_gated_native_request, + strip_beta_gated_request_fields, + strip_beta_gated_system_messages, + strip_beta_gated_thinking, +) +from litellm.types.llms.anthropic import ( + ANTHROPIC_MID_CONVERSATION_OUTPUT_CONFIG_BETA_HEADER, + ANTHROPIC_MID_CONVERSATION_TOOL_CHANGES_BETA_HEADER, + ANTHROPIC_THINKING_DISPLAY_UPDATES_BETA_HEADER, +) + +EVERY_BETA: Final = frozenset(BETA_GATED_FIELDS) +BOUND_THINKING: Final = { + "type": "adaptive", + "block_binding": {"prefix_mismatch_behavior": "error"}, + "display": "updates", +} + + +def _system_turn() -> dict[str, object]: + return { + "role": "system", + "clear_at": "end_turn", + "output_config": {"effort": "low"}, + "content": [ + {"type": "text", "text": "stay terse"}, + {"type": "tool_addition", "tools": [{"name": "lookup"}]}, + {"type": "tool_removal", "tool_names": ["lookup"]}, + ], + } + + +def test_thinking_with_every_beta_active_is_returned_as_is() -> None: + result: Final = strip_beta_gated_thinking(BOUND_THINKING, EVERY_BETA, drop_params=True) + assert result.thinking is BOUND_THINKING + assert result.stripped == () + + +def test_thinking_loses_only_the_field_whose_beta_is_absent() -> None: + without_binding: Final = strip_beta_gated_thinking( + BOUND_THINKING, EVERY_BETA - {ANTHROPIC_THINKING_BINDING_CONTROLS_BETA_HEADER}, drop_params=True + ) + assert without_binding.thinking == {"type": "adaptive", "display": "updates"} + assert without_binding.stripped == ("thinking.block_binding",) + + without_display: Final = strip_beta_gated_thinking( + BOUND_THINKING, EVERY_BETA - {ANTHROPIC_THINKING_DISPLAY_UPDATES_BETA_HEADER}, drop_params=True + ) + assert without_display.thinking == {"type": "adaptive", "block_binding": {"prefix_mismatch_behavior": "error"}} + assert without_display.stripped == ("thinking.display=updates",) + assert BOUND_THINKING == { + "type": "adaptive", + "block_binding": {"prefix_mismatch_behavior": "error"}, + "display": "updates", + }, "copy-on-write: the caller's thinking dict must survive for the fallback leg" + + +def test_thinking_display_other_than_updates_is_not_gated() -> None: + thinking: Final = {"type": "adaptive", "display": "summarized"} + result: Final = strip_beta_gated_thinking(thinking, frozenset(), drop_params=True) + assert result.thinking is thinking + + +@pytest.mark.parametrize("thinking", [None, "enabled", 12, ["adaptive"]]) +def test_non_mapping_thinking_passes_through(thinking: object) -> None: + result: Final = strip_beta_gated_thinking(thinking, frozenset(), drop_params=True) + assert result.thinking is thinking + assert result.stripped == () + + +def test_system_message_keeps_every_gated_field_when_the_betas_are_active() -> None: + messages: Final = [{"role": "user", "content": "hi"}, _system_turn()] + result: Final = strip_beta_gated_system_messages(messages, EVERY_BETA, drop_params=True) + assert result.messages is messages + assert result.stripped == () + + +def test_system_message_loses_exactly_the_fields_whose_betas_are_absent() -> None: + original: Final = _system_turn() + messages: Final = [{"role": "user", "content": "hi"}, original] + result: Final = strip_beta_gated_system_messages( + messages, + EVERY_BETA + - { + ANTHROPIC_MID_CONVERSATION_SYSTEM_CLEAR_AT_BETA_HEADER, + ANTHROPIC_MID_CONVERSATION_TOOL_CHANGES_BETA_HEADER, + ANTHROPIC_INLINE_TOOLS_BETA_HEADER, + }, + drop_params=True, + ) + assert result.messages == [ + {"role": "user", "content": "hi"}, + { + "role": "system", + "output_config": {"effort": "low"}, + "content": [{"type": "text", "text": "stay terse"}], + }, + ] + assert result.stripped == ( + "messages[].system.clear_at", + "messages[].system.content[].tool_addition|tool_removal", + ) + assert original == _system_turn(), "copy-on-write: the caller's system message must survive" + + +def test_per_turn_control_beta_also_unlocks_system_output_config() -> None: + messages: Final = [{"role": "system", "output_config": {"effort": "low"}, "content": "terse"}] + assert ( + strip_beta_gated_system_messages( + messages, frozenset({"per-turn-control-2026-07-01"}), drop_params=True + ).messages + is messages + ) + assert ( + strip_beta_gated_system_messages( + messages, frozenset({ANTHROPIC_MID_CONVERSATION_OUTPUT_CONFIG_BETA_HEADER}), drop_params=True + ).messages + is messages + ) + assert strip_beta_gated_system_messages(messages, frozenset(), drop_params=True).messages == [ + {"role": "system", "content": "terse"} + ] + + +def test_inline_tools_beta_also_unlocks_system_tool_changes() -> None: + messages: Final = [ + { + "role": "system", + "content": [{"type": "tool_removal", "tool_names": ["lookup"]}, {"type": "text", "text": "x"}], + } + ] + assert ( + strip_beta_gated_system_messages( + messages, frozenset({ANTHROPIC_INLINE_TOOLS_BETA_HEADER}), drop_params=True + ).messages + is messages + ) + assert ( + strip_beta_gated_system_messages( + messages, frozenset({ANTHROPIC_MID_CONVERSATION_TOOL_CHANGES_BETA_HEADER}), drop_params=True + ).messages + is messages + ) + assert strip_beta_gated_system_messages(messages, frozenset(), drop_params=True).messages == [ + {"role": "system", "content": [{"type": "text", "text": "x"}]} + ] + + +def test_system_block_with_unhashable_type_is_kept_for_the_provider_to_reject() -> None: + messages: Final = [{"role": "system", "content": [{"type": [], "text": "x"}]}] + result: Final = strip_beta_gated_system_messages(messages, frozenset(), drop_params=True) + assert result.messages is messages + assert result.stripped == () + + +def test_system_message_emptied_by_the_strip_is_dropped() -> None: + messages: Final = [ + {"role": "user", "content": "hi"}, + {"role": "system", "content": [{"type": "tool_removal", "tool_names": ["lookup"]}]}, + {"role": "assistant", "content": "ok"}, + ] + result: Final = strip_beta_gated_system_messages(messages, frozenset(), drop_params=True) + assert result.messages == [{"role": "user", "content": "hi"}, {"role": "assistant", "content": "ok"}] + assert result.stripped == ( + "messages[].system.content[].tool_addition|tool_removal", + "messages[].system (emptied by the strip)", + ) + + +def test_emptied_trailing_system_turn_after_an_assistant_turn_is_sent_as_is() -> None: + messages: Final = [ + {"role": "user", "content": "hi"}, + {"role": "assistant", "content": "ok"}, + {"role": "system", "content": [{"type": "tool_removal", "tool_names": ["lookup"]}]}, + ] + result: Final = strip_beta_gated_system_messages(messages, frozenset(), drop_params=True) + assert result.messages is messages + assert result.stripped == () + + +def test_emptied_system_turn_is_dropped_when_a_user_turn_still_ends_the_conversation() -> None: + messages: Final = [ + {"role": "user", "content": "hi"}, + {"role": "assistant", "content": "ok"}, + {"role": "system", "content": [{"type": "tool_removal", "tool_names": ["lookup"]}]}, + {"role": "user", "content": "go on"}, + ] + result: Final = strip_beta_gated_system_messages(messages, frozenset(), drop_params=True) + assert [message["role"] for message in result.messages] == ["user", "assistant", "user"] + + +def test_system_turn_emptied_by_the_strip_stays_as_a_directive_when_its_output_config_survives() -> None: + directive: Final = { + "role": "system", + "output_config": {"effort": "low"}, + "content": [{"type": "tool_removal", "tool_names": ["lookup"]}], + } + result: Final = strip_beta_gated_system_messages( + [directive], frozenset({ANTHROPIC_MID_CONVERSATION_OUTPUT_CONFIG_BETA_HEADER}), drop_params=True + ) + assert result.messages == [{"role": "system", "output_config": {"effort": "low"}, "content": []}] + assert result.stripped == ("messages[].system.content[].tool_addition|tool_removal",) + + +def test_system_turn_emptied_by_the_strip_is_dropped_when_its_output_config_is_gated_too() -> None: + directive: Final = { + "role": "system", + "output_config": {"effort": "low"}, + "content": [{"type": "tool_removal", "tool_names": ["lookup"]}], + } + result: Final = strip_beta_gated_system_messages([directive], frozenset(), drop_params=True) + assert result.messages == [] + assert result.stripped[-1] == "messages[].system (emptied by the strip)" + + +def test_request_strip_replaces_only_thinking_and_messages_and_keeps_the_dict_otherwise() -> None: + request: Final = { + "model": "claude", + "thinking": dict(BOUND_THINKING), + "messages": [_system_turn()], + "max_tokens": 5, + } + result: Final = strip_beta_gated_request_fields(request, frozenset(), drop_params=True) + assert result.request == { + "model": "claude", + "thinking": {"type": "adaptive"}, + "messages": [{"role": "system", "content": [{"type": "text", "text": "stay terse"}]}], + "max_tokens": 5, + } + assert result.request is not request + assert request["thinking"] == BOUND_THINKING + assert request["messages"] == [_system_turn()] + + +def test_request_strip_is_identity_when_nothing_is_gated() -> None: + request: Final = { + "thinking": {"type": "enabled", "budget_tokens": 1024}, + "messages": [{"role": "user", "content": "hi"}], + } + result: Final = strip_beta_gated_request_fields(request, frozenset(), drop_params=True) + assert result.request is request + assert result.stripped == () + + +@pytest.mark.parametrize("drop_params", [None, False, "false"]) +def test_without_drop_params_every_gated_field_is_sent_as_is( + drop_params: object, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setattr(litellm, "drop_params", False) + messages: Final = [{"role": "user", "content": "hi"}, _system_turn()] + request: Final = {"thinking": BOUND_THINKING, "messages": messages} + + assert strip_beta_gated_thinking(BOUND_THINKING, frozenset(), drop_params=drop_params).thinking is BOUND_THINKING + assert strip_beta_gated_system_messages(messages, frozenset(), drop_params=drop_params).messages is messages + assert strip_beta_gated_request_fields(request, frozenset(), drop_params=drop_params).request is request + native: Final = strip_beta_gated_native_request({}, {"thinking": BOUND_THINKING}, messages, drop_params=drop_params) + assert (native.thinking, list(native.messages), native.thinking_stripped, native.messages_stripped) == ( + BOUND_THINKING, + messages, + False, + False, + ) + + +def test_global_drop_params_strips_without_a_per_call_flag(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(litellm, "drop_params", True) + assert strip_beta_gated_thinking(BOUND_THINKING, frozenset(), drop_params=None).thinking == {"type": "adaptive"} + + +def test_active_beta_values_reads_the_header_case_insensitively_and_merges_body_betas() -> None: + assert active_beta_values({"Anthropic-Beta": "a-2026-01-01, b-2026-01-01 ,"}, ["c-2026-01-01", 7]) == frozenset( + {"a-2026-01-01", "b-2026-01-01", "c-2026-01-01"} + ) + assert active_beta_values({}) == frozenset() + + +def test_every_strip_is_logged_once_at_warning_with_the_fields_model_and_provider( + caplog: pytest.LogCaptureFixture, +) -> None: + with caplog.at_level(logging.WARNING, logger="LiteLLM"): + log_beta_gated_strips(("thinking.block_binding", "thinking.block_binding"), model="claude", provider="azure_ai") + log_beta_gated_strips((), model="claude", provider="azure_ai") + warnings: Final = [record for record in caplog.records if record.levelno == logging.WARNING] + assert len(warnings) == 1 + assert ( + "Dropped thinking.block_binding (unlocked by anthropic-beta thinking-binding-controls-2026-08-01) for " + "model=claude provider=azure_ai" in warnings[0].getMessage() + ) diff --git a/tests/unit/test_dashscope_image_generation.py b/tests/unit/test_dashscope_image_generation.py index 960807a033b..9c65399350c 100644 --- a/tests/unit/test_dashscope_image_generation.py +++ b/tests/unit/test_dashscope_image_generation.py @@ -5,21 +5,26 @@ qwen-image-3.0, qwen-image-3.0-pro). Run in docker: pytest tests/unit/test_dashscope_image_generation.py -v """ +from typing import Final from unittest.mock import MagicMock, patch import httpx import pytest +import respx from pydantic import ValidationError import litellm +from litellm.llms.base_llm.chat.transformation import BaseLLMException +from litellm.llms.dashscope.common_utils import ( + DASHSCOPE_DEFAULT_ORIGIN, + DASHSCOPE_IMAGE_PATH, +) from litellm.llms.dashscope.image_generation.transformation import ( - DashScopeImageGenerationConfig, DEFAULT_API_BASE, + DashScopeImageGenerationConfig, ) from litellm.types.utils import ImageResponse from litellm.utils import get_llm_provider -from litellm.llms.base_llm.chat.transformation import BaseLLMException - # --------------------------------------------------------------------------- # 1. Provider detection @@ -77,6 +82,47 @@ class TestDashScopeImageGenerationConfig: url = self.cfg.get_complete_url(chat_api_base, None, "qwen-image-3.0", {}, {}) assert url == DEFAULT_API_BASE + @pytest.mark.parametrize( + ("api_base", "expected"), + [ + ( + "https://dashscope.aliyuncs.com", + "https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation", + ), + ( + "https://llm-abc123.cn-beijing.maas.aliyuncs.com", + "https://llm-abc123.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation", + ), + ( + "https://llm-abc123.cn-beijing.maas.aliyuncs.com/apps/anthropic", + "https://llm-abc123.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation", + ), + ( + "https://dashscope-us.aliyuncs.com/api/v1/", + "https://dashscope-us.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation", + ), + ], + ) + def test_get_complete_url_derives_from_the_domain( + self, api_base: str, expected: str + ): + url: Final = self.cfg.get_complete_url(api_base, None, "qwen-image-3.0", {}, {}) + assert url == expected + + @pytest.mark.parametrize( + "endpoint_url", + [ + "https://dashscope.aliyuncs.com/api/v1/services/aigc/image-generation/generation", + "https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis", + ], + ) + def test_get_complete_url_keeps_a_full_dashscope_endpoint_url( + self, monkeypatch: pytest.MonkeyPatch, endpoint_url: str + ): + monkeypatch.setenv("DASHSCOPE_API_BASE_IMAGE", "https://images.example.com/generate") + url: Final = self.cfg.get_complete_url(endpoint_url, None, "qwen-image-3.0", {}, {}) + assert url == endpoint_url + def test_validate_environment_sets_auth_header(self): headers = self.cfg.validate_environment( headers={}, @@ -458,6 +504,81 @@ def test_litellm_image_generation_dashscope_end_to_end(model: str): assert body["parameters"]["size"] == "1024*1024" + +@pytest.mark.parametrize( + ("api_base", "chat_base_env", "image_base_env", "expected_url"), + [ + (None, "", "", DEFAULT_API_BASE), + ("https://dashscope.aliyuncs.com", "", "", f"{DASHSCOPE_DEFAULT_ORIGIN}{DASHSCOPE_IMAGE_PATH}"), + (None, "", "https://images.example.com/generate", "https://images.example.com/generate"), + ( + "https://dashscope.aliyuncs.com/compatible-mode/v1", + "", + "https://images.example.com/generate", + "https://images.example.com/generate", + ), + (None, "https://gateway.example.com/dashscope/images", "", "https://gateway.example.com/dashscope/images"), + (None, "https://dashscope-us.aliyuncs.com", "https://images.example.com/generate", "https://images.example.com/generate"), + ( + None, + f"https://dashscope-us.aliyuncs.com{DASHSCOPE_IMAGE_PATH}", + "https://images.example.com/generate", + f"https://dashscope-us.aliyuncs.com{DASHSCOPE_IMAGE_PATH}", + ), + ( + None, + "https://gateway.example.com/dashscope/images", + "https://images.example.com/generate", + "https://gateway.example.com/dashscope/images", + ), + ( + None, + "https://dashscope-us.aliyuncs.com/compatible-mode/v1", + "https://images.example.com/generate", + "https://images.example.com/generate", + ), + ( + "https://dashscope-us.aliyuncs.com", + "", + "https://images.example.com/generate", + f"https://dashscope-us.aliyuncs.com{DASHSCOPE_IMAGE_PATH}", + ), + ( + None, + "https://dashscope-us.aliyuncs.com", + "", + f"https://dashscope-us.aliyuncs.com{DASHSCOPE_IMAGE_PATH}", + ), + ( + "https://dashscope-intl.aliyuncs.com", + "https://gateway.example.com/dashscope/images", + "", + f"https://dashscope-intl.aliyuncs.com{DASHSCOPE_IMAGE_PATH}", + ), + ], +) +def test_image_generation_resolves_from_the_configured_base_not_the_chat_default( + monkeypatch: pytest.MonkeyPatch, + api_base: str | None, + chat_base_env: str, + image_base_env: str, + expected_url: str, +): + monkeypatch.setenv("DASHSCOPE_API_BASE", chat_base_env) + monkeypatch.setenv("DASHSCOPE_API_BASE_IMAGE", image_base_env) + image_url: Final = "https://dashscope-result.oss.aliyuncs.com/t.png" + + with respx.mock(assert_all_called=True) as upstream: + upstream.post(expected_url).respond( + 200, json={"output": {"choices": [{"message": {"content": [{"image": image_url}]}}]}} + ) + response: Final = litellm.image_generation( + model="dashscope/qwen-image-3.0", prompt="a puppy", api_key="sk-test-key", api_base=api_base + ) + + assert response.data[0].url == image_url + + def _transform_response(payload: object) -> ImageResponse: return DashScopeImageGenerationConfig().transform_image_generation_response( model="qwen-image-2.0", diff --git a/tests/unit/test_lint_workflow_diff_gates.py b/tests/unit/test_lint_workflow_diff_gates.py index 7b1d7802d53..5d3c0d80aa6 100644 --- a/tests/unit/test_lint_workflow_diff_gates.py +++ b/tests/unit/test_lint_workflow_diff_gates.py @@ -1,5 +1,6 @@ from __future__ import annotations +import fnmatch import re import shlex import subprocess @@ -7,6 +8,7 @@ from pathlib import Path from typing import Final import pytest +import yaml WORKFLOW: Final = Path(__file__).resolve().parents[2] / ".github" / "workflows" / "test-linting.yml" DIFF_GATE: Final = re.compile(r'git diff --name-only --diff-filter=\w+ "\$GATE_BASE_SHA" HEAD -- (.+?) \|') @@ -100,3 +102,101 @@ def test_e2e_harness_gate_fires_on_any_e2e_or_harness_file_its_dependency_manife (trigger, "tests/e2e/ui/spec.ts", "tests/e2e/ui/pkg/page.py", "elsewhere/pyproject.toml", "tests/e2e_other/module.py"), ) assert selected == frozenset({trigger}) + + +UI_API_TYPES_DETECT: Final[str] = next( + step["run"] + for step in yaml.safe_load(WORKFLOW.read_text())["jobs"]["ui-api-types"]["steps"] + if step.get("id") == "changes" +) +PRE_COMMIT_LINT: Final = WORKFLOW.parents[2] / "scripts" / "pre_commit_lint.sh" +TERRAFORM_PROVIDER_WORKFLOW: Final = WORKFLOW.with_name("test-terraform-provider.yml") + + +def _ui_api_types_relevant(tmp_path: Path, changed: str) -> bool: + _git(tmp_path, "init", "-q", "-b", "main") + _git(tmp_path, "config", "user.email", "t@t") + _git(tmp_path, "config", "user.name", "t") + _git(tmp_path, "commit", "-q", "--allow-empty", "-m", "base") + _git(tmp_path, "switch", "-q", "-c", "pr") + target: Final = tmp_path / changed + target.parent.mkdir(parents=True, exist_ok=True) + target.write_text("x = 1\n") + _git(tmp_path, "add", "-A") + _git(tmp_path, "commit", "-qm", "change") + _git(tmp_path, "switch", "-q", "main") + _git(tmp_path, "commit", "-q", "--allow-empty", "-m", "main moved") + _git(tmp_path, "merge", "-q", "--no-ff", "--no-edit", "pr") + output: Final = tmp_path / "github_output" + subprocess.run( + ["bash", "-c", UI_API_TYPES_DETECT], + cwd=tmp_path, + check=True, + capture_output=True, + env={"PATH": "/usr/bin:/bin", "GITHUB_OUTPUT": str(output)}, + ) + return output.read_text().strip() == "relevant=true" + + +@pytest.mark.parametrize( + "changed", + ( + "litellm/proxy/proxy_server.py", + "litellm/types/router.py", + "litellm/tracing/generated/models.py", + "litellm/router_strategy/complexity_router/config.py", + "litellm/models/credentials.py", + "enterprise/litellm_enterprise/types/proxy/audit_logging_endpoints.py", + "pyproject.toml", + "uv.lock", + "ui/litellm-dashboard/scripts/gen-api-types.mjs", + "ui/litellm-dashboard/package-lock.json", + "ui/litellm-dashboard/src/lib/http/schema.d.ts", + ), +) +def test_ui_api_types_check_runs_on_a_pull_request_touching_any_spec_input(tmp_path: Path, changed: str) -> None: + assert _ui_api_types_relevant(tmp_path, changed) + + +@pytest.mark.parametrize( + "changed", + ( + "litellm-rust/crates/traces/src/lib.rs", + "litellm-proxy-extras/litellm_proxy_extras/utils.py", + "tests/unit/test_router.py", + "ui/litellm-dashboard/src/app/page.tsx", + "docs/my-website/docs/proxy/config.md", + "litellm-dashboard/pyproject.toml", + ), +) +def test_ui_api_types_check_skips_a_pull_request_that_cannot_change_the_spec(tmp_path: Path, changed: str) -> None: + assert not _ui_api_types_relevant(tmp_path, changed) + + +def _first_single_quoted_capture(pattern: str, text: str) -> str: + match: Final = re.search(pattern, text, re.MULTILINE) + assert match is not None, pattern + return match.group(1) + + +def _tracked_paths_matching(extended_regex: str) -> frozenset[str]: + tracked: Final = _git(WORKFLOW.parents[2], "ls-files") + selected: Final = subprocess.run(["grep", "-E", extended_regex], input=tracked, capture_output=True, text=True) + return frozenset(selected.stdout.splitlines()) + + +def test_make_check_runs_the_api_type_sync_on_the_same_tracked_files_as_ci() -> None: + ci_trigger: Final = _first_single_quoted_capture(r"grep -Eq '([^']+)'", UI_API_TYPES_DETECT) + make_check_trigger: Final = _first_single_quoted_capture(r"^spec_pattern='([^']+)'$", PRE_COMMIT_LINT.read_text()) + ci_selected: Final = _tracked_paths_matching(ci_trigger) + assert "pyproject.toml" in ci_selected + assert _tracked_paths_matching(make_check_trigger) == ci_selected - {".github/workflows/test-linting.yml"} + + +def test_a_pull_request_changing_any_route_file_runs_the_terraform_provider_endpoint_audit() -> None: + triggers: Final = yaml.safe_load(TERRAFORM_PROVIDER_WORKFLOW.read_text())[True]["pull_request"]["paths"] + route_files: Final = _git( + WORKFLOW.parents[2], "grep", "-l", "-E", r"APIRouter\(", "--", "*.py", ":(exclude)tests/**" + ).splitlines() + assert route_files + assert [name for name in route_files if not any(fnmatch.fnmatch(name, pattern) for pattern in triggers)] == [] diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index e119fe133d5..e10352e0b3d 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -23,7 +23,7 @@ import respx from openai import APITimeoutError, AsyncOpenAI, OpenAI from openai._legacy_response import LegacyAPIResponse from openai.types.chat.chat_completion import ChatCompletion -from pydantic import TypeAdapter +from pydantic import BaseModel, ConfigDict, TypeAdapter import litellm from litellm import ( @@ -10200,6 +10200,309 @@ async def test_atranscription_ignores_a_non_string_base_url_when_api_base_is_set assert response.text == "four score" +_PDF_FILE_PART: Final = { + "type": "file", + "file": {"filename": "report.pdf", "file_data": "data:application/pdf;base64,JVBERi0xLjQK"}, +} +_SUMMARIZE_TEXT_PART: Final = {"type": "text", "text": "Summarize this."} + + +def _pdf_messages() -> list[dict[str, object]]: + return [{"role": "user", "content": [_SUMMARIZE_TEXT_PART, _PDF_FILE_PART]}] + + +class _SentChatRequest(BaseModel): + model_config = ConfigDict(frozen=True) + + messages: list[dict[str, object]] + + +def _sent_messages(respx_mock: respx.MockRouter) -> list[dict[str, object]]: + request: Final[httpx.Request] = respx_mock.calls[0].request + return _SentChatRequest.model_validate_json(request.content).messages + + +def _mock_pdf_chat_route(respx_mock: respx.MockRouter, api_base: str) -> respx.Route: + return respx_mock.post(url__regex=rf"{api_base}/chat/completions.*").mock( + return_value=httpx.Response( + status_code=200, + json={ + "id": "chatcmpl-pdf", + "object": "chat.completion", + "created": 1712697600, + "model": "qa-pdf-model", + "choices": [{"index": 0, "message": {"role": "assistant", "content": "ok"}, "finish_reason": "stop"}], + "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2}, + }, + ) + ) + + +def test_drop_params_drops_a_pdf_the_model_cannot_read(respx_mock: respx.MockRouter) -> None: + api_base: Final = "http://localhost:12347/v1" + _mock_pdf_chat_route(respx_mock, api_base) + + litellm.completion( + model="openai/qa-text-only-model", + messages=_pdf_messages(), + api_base=api_base, + api_key="fake_openai_api_key", + model_info={"supports_pdf_input": False}, + drop_params=True, + ) + + assert _sent_messages(respx_mock) == [{"role": "user", "content": [_SUMMARIZE_TEXT_PART]}] + + +@pytest.mark.parametrize( + "pdf_file", + [ + {"filename": "report.pdf", "file_data": "JVBERi0xLjQK"}, + {"file_id": "file-report", "format": "application/pdf"}, + {"file_id": "file-report", "filename": "Report.PDF"}, + ], + ids=["bare_base64_named_pdf", "file_id_with_pdf_format", "file_id_named_pdf"], +) +def test_drop_params_drops_a_pdf_file_part_without_a_data_url( + respx_mock: respx.MockRouter, pdf_file: dict[str, str] +) -> None: + api_base: Final = "http://localhost:12350/v1" + _mock_pdf_chat_route(respx_mock, api_base) + + litellm.completion( + model="openai/qa-text-only-model", + messages=[{"role": "user", "content": [_SUMMARIZE_TEXT_PART, {"type": "file", "file": pdf_file}]}], + api_base=api_base, + api_key="fake_openai_api_key", + model_info={"supports_pdf_input": False}, + drop_params=True, + ) + + assert _sent_messages(respx_mock) == [{"role": "user", "content": [_SUMMARIZE_TEXT_PART]}] + + +def test_drop_params_drops_a_user_turn_that_held_only_a_pdf(respx_mock: respx.MockRouter) -> None: + api_base: Final = "http://localhost:12352/v1" + _mock_pdf_chat_route(respx_mock, api_base) + + litellm.completion( + model="openai/qa-text-only-model", + messages=[ + {"role": "user", "content": [_PDF_FILE_PART]}, + {"role": "user", "content": [_SUMMARIZE_TEXT_PART]}, + ], + api_base=api_base, + api_key="fake_openai_api_key", + model_info={"supports_pdf_input": False}, + drop_params=True, + ) + + assert _sent_messages(respx_mock) == [{"role": "user", "content": [_SUMMARIZE_TEXT_PART]}] + + +def test_drop_params_keeps_roles_alternating_after_dropping_a_pdf_only_turn(respx_mock: respx.MockRouter) -> None: + api_base: Final = "http://localhost:12353/v1" + _mock_pdf_chat_route(respx_mock, api_base) + + litellm.completion( + model="openai/qa-text-only-model", + messages=[ + {"role": "user", "content": [_SUMMARIZE_TEXT_PART]}, + {"role": "assistant", "content": "Send the file."}, + {"role": "user", "content": [_PDF_FILE_PART]}, + {"role": "assistant", "content": "Got it."}, + {"role": "user", "content": [_SUMMARIZE_TEXT_PART]}, + ], + api_base=api_base, + api_key="fake_openai_api_key", + model_info={"supports_pdf_input": False}, + drop_params=True, + ensure_alternating_roles=True, + ) + + sent_roles: Final = [message["role"] for message in _sent_messages(respx_mock)] + assert sent_roles == ["user", "assistant", "user", "assistant", "user"] + + +def test_drop_params_keeps_an_image_data_url_whose_filename_says_pdf(respx_mock: respx.MockRouter) -> None: + api_base: Final = "http://localhost:12351/v1" + _mock_pdf_chat_route(respx_mock, api_base) + image_part: Final = { + "type": "file", + "file": {"filename": "scan.pdf", "file_data": "data:image/png;base64,iVBORw0KGgo="}, + } + + litellm.completion( + model="openai/qa-text-only-model", + messages=[{"role": "user", "content": [_SUMMARIZE_TEXT_PART, image_part]}], + api_base=api_base, + api_key="fake_openai_api_key", + model_info={"supports_pdf_input": False}, + drop_params=True, + ) + + assert _sent_messages(respx_mock) == [{"role": "user", "content": [_SUMMARIZE_TEXT_PART, image_part]}] + + +def test_pdf_to_a_model_that_cannot_read_it_is_rejected_without_drop_params(respx_mock: respx.MockRouter) -> None: + api_base: Final = "http://localhost:12348/v1" + route: Final = _mock_pdf_chat_route(respx_mock, api_base) + + with pytest.raises(litellm.BadRequestError, match="does not accept PDF input"): + litellm.completion( + model="openai/qa-text-only-model", + messages=_pdf_messages(), + api_base=api_base, + api_key="fake_openai_api_key", + model_info={"supports_pdf_input": False}, + ) + + assert not route.called + + +def test_pdf_to_a_model_with_unknown_pdf_support_is_sent_even_with_drop_params(respx_mock: respx.MockRouter) -> None: + api_base: Final = "http://localhost:12349/v1" + _mock_pdf_chat_route(respx_mock, api_base) + + litellm.completion( + model="openai/qa-unlisted-model", + messages=_pdf_messages(), + api_base=api_base, + api_key="fake_openai_api_key", + drop_params=True, + ) + + assert _sent_messages(respx_mock) == _pdf_messages() + + +def test_pdf_to_ollama_is_rejected_without_drop_params() -> None: + with pytest.raises(litellm.BadRequestError, match="does not accept PDF input"): + litellm.completion(model="ollama_chat/llama3.2", messages=_pdf_messages(), api_base="http://127.0.0.1:9") + + +def _azure_ai_model_without_pdf_input() -> str: + return next( + name + for name, entry in litellm.model_cost.items() + if entry.get("litellm_provider") == "azure_ai" and entry.get("supports_pdf_input") is False + ) + + +def test_pdf_to_a_deployment_whose_base_model_cannot_read_it_is_rejected() -> None: + with pytest.raises(litellm.BadRequestError, match="does not accept PDF input"): + litellm.completion( + model="azure_ai/qa-custom-deployment", + messages=_pdf_messages(), + api_base="http://127.0.0.1:9", + api_key="fake_azure_ai_api_key", + model_info={"base_model": _azure_ai_model_without_pdf_input()}, + ) + + +def test_drop_params_drops_a_pdf_a_deployments_base_model_cannot_read(respx_mock: respx.MockRouter) -> None: + api_base: Final = "http://localhost:12353" + _mock_pdf_chat_route(respx_mock, api_base) + + litellm.completion( + model="azure_ai/qa-custom-deployment", + messages=_pdf_messages(), + api_base=api_base, + api_key="fake_azure_ai_api_key", + model_info={"base_model": _azure_ai_model_without_pdf_input()}, + drop_params=True, + ) + + assert _sent_messages(respx_mock) == [{"role": "user", "content": _SUMMARIZE_TEXT_PART["text"]}] + + +def _azure_ai_model_with_pdf_input() -> str: + return next( + name + for name, entry in litellm.model_cost.items() + if entry.get("litellm_provider") == "azure_ai" and entry.get("supports_pdf_input") is True + ) + + +def test_pdf_to_a_deployment_whose_base_model_param_cannot_read_it_is_rejected() -> None: + with pytest.raises(litellm.BadRequestError, match="does not accept PDF input"): + litellm.completion( + model="azure_ai/qa-custom-deployment", + messages=_pdf_messages(), + api_base="http://127.0.0.1:9", + api_key="fake_azure_ai_api_key", + base_model=_azure_ai_model_without_pdf_input(), + ) + + +def test_drop_params_keeps_a_pdf_a_deployments_base_model_param_reads(respx_mock: respx.MockRouter) -> None: + api_base: Final = "http://localhost:12357" + _mock_pdf_chat_route(respx_mock, api_base) + + litellm.completion( + model="azure_ai/qa-custom-deployment", + messages=_pdf_messages(), + api_base=api_base, + api_key="fake_azure_ai_api_key", + base_model=_azure_ai_model_with_pdf_input(), + drop_params=True, + ) + + assert _sent_messages(respx_mock) == _pdf_messages() + + +def test_drop_params_rejects_a_request_whose_only_user_turn_was_a_pdf(respx_mock: respx.MockRouter) -> None: + api_base: Final = "http://localhost:12354/v1" + route: Final = _mock_pdf_chat_route(respx_mock, api_base) + + with pytest.raises(litellm.BadRequestError, match="no user message is left"): + litellm.completion( + model="openai/qa-text-only-model", + messages=[ + {"role": "system", "content": "You read documents."}, + {"role": "user", "content": [_PDF_FILE_PART]}, + ], + api_base=api_base, + api_key="fake_openai_api_key", + model_info={"supports_pdf_input": False}, + drop_params=True, + ) + + assert not route.called + + +def test_drop_params_drops_a_pdf_an_unlisted_azure_ai_model_never_declared_it_reads( + respx_mock: respx.MockRouter, +) -> None: + api_base: Final = "http://localhost:12355" + _mock_pdf_chat_route(respx_mock, api_base) + + litellm.completion( + model="azure_ai/qa-unlisted-deployment", + messages=_pdf_messages(), + api_base=api_base, + api_key="fake_azure_ai_api_key", + drop_params=True, + ) + + assert _sent_messages(respx_mock) == [{"role": "user", "content": _SUMMARIZE_TEXT_PART["text"]}] + + +def test_drop_params_keeps_a_pdf_an_azure_ai_deployment_declares_it_reads(respx_mock: respx.MockRouter) -> None: + api_base: Final = "http://localhost:12356" + _mock_pdf_chat_route(respx_mock, api_base) + + litellm.completion( + model="azure_ai/qa-unlisted-deployment", + messages=_pdf_messages(), + api_base=api_base, + api_key="fake_azure_ai_api_key", + model_info={"supports_pdf_input": True}, + drop_params=True, + ) + + assert _sent_messages(respx_mock) == _pdf_messages() + + @pytest.mark.parametrize("stream", (False, True)) @pytest.mark.parametrize("asynchronous", (False, True)) @pytest.mark.parametrize("profile,custom_price", ((None, False), ("us.openai.gpt-6-sol", False), (None, True))) diff --git a/tests/unit/test_pre_commit_lint.py b/tests/unit/test_pre_commit_lint.py index 7c9b98c1c94..ef84ab9fbc8 100644 --- a/tests/unit/test_pre_commit_lint.py +++ b/tests/unit/test_pre_commit_lint.py @@ -427,6 +427,34 @@ def test_run_ends_with_a_summary_of_ran_and_skipped_blocks(tmp_path: Path) -> No assert "check: FAIL" not in proc.stdout +@pytest.mark.parametrize( + "spec_input", + ( + "litellm/tracing/generated/models.py", + "litellm/models/credentials.py", + "enterprise/litellm_enterprise/types/proxy/audit_logging_endpoints.py", + "pyproject.toml", + "uv.lock", + ), +) +def test_a_staged_spec_input_outside_proxy_and_types_runs_the_api_type_sync(tmp_path: Path, spec_input: str) -> None: + repo, bin_dir = _sandbox(tmp_path) + _commit_all(repo, "base") + _stage_file(repo, spec_input, "x = 1\n") + proc = _run(repo, bin_dir, {}) + assert proc.returncode == 0, proc.stdout + proc.stderr + assert "ran: dashboard API-type sync (npm run gen:api)" in proc.stdout + + +def test_a_staged_rust_crate_change_skips_the_api_type_sync(tmp_path: Path) -> None: + repo, bin_dir = _sandbox(tmp_path) + _commit_all(repo, "base") + _stage_file(repo, "litellm-rust/crates/traces/src/lib.rs", "fn main() {}\n") + proc = _run(repo, bin_dir, {}) + assert proc.returncode == 0, proc.stdout + proc.stderr + assert "skipped: dashboard API-type sync (npm run gen:api)" in proc.stdout + + def test_staged_files_matching_no_check_print_an_explicit_noop_note_and_nonempty_log(tmp_path: Path) -> None: repo, bin_dir = _sandbox(tmp_path) _commit_all(repo, "base") diff --git a/tests/unit/test_private_usage_aliases.py b/tests/unit/test_private_usage_aliases.py index d0c5c8247d3..f2195512355 100644 --- a/tests/unit/test_private_usage_aliases.py +++ b/tests/unit/test_private_usage_aliases.py @@ -5242,10 +5242,11 @@ def test_public_aliases( resolved_owner: Final = _get_owner(module_path, owner_name) alias_owner: Final = _get_instance(resolved_owner, new_name) if use_instance else resolved_owner old_value, new_value = getattr(alias_owner, old_name), getattr(alias_owner, new_name) + # Router's private aliases wrap the public method so they skip a subclass override of it. if isinstance(old_value, MethodType) and isinstance(new_value, MethodType): - assert old_value.__func__ is new_value.__func__ + assert old_value.__func__ is new_value.__func__ or inspect.unwrap(old_value.__func__) is new_value.__func__ else: - assert old_value is new_value + assert old_value is new_value or inspect.unwrap(old_value) is new_value @pytest.mark.parametrize( diff --git a/tests/unit/test_router/test_router.py b/tests/unit/test_router/test_router.py index b55b3c5efc2..a0a7dd78193 100644 --- a/tests/unit/test_router/test_router.py +++ b/tests/unit/test_router/test_router.py @@ -2321,7 +2321,7 @@ def test_with_router_resolved_session_model(session, expected): def test_router_get_model_access_groups_team_only_models(): """ - Test that Router.get_model_access_groups returns the correct response for team-only models + Test that Router._core_get_model_access_groups returns the correct response for team-only models """ router = litellm.Router( model_list=[ @@ -2440,7 +2440,7 @@ def test_model_group_info_cost_from_db_model_info(): ) with patch.object( - router, "get_deployment_model_info", side_effect=Exception("not found") + router, "_core_get_deployment_model_info", side_effect=Exception("not found") ): result = router._cached_get_model_group_info("my-custom-model") assert result is not None @@ -2470,7 +2470,7 @@ def test_model_group_info_cost_none_when_db_model_info_has_no_cost(): ) with patch.object( - router, "get_deployment_model_info", side_effect=Exception("not found") + router, "_core_get_deployment_model_info", side_effect=Exception("not found") ): result = router._cached_get_model_group_info("my-custom-model-no-cost") assert result is not None @@ -2854,7 +2854,7 @@ def test_switch_routing_strategy_installs_lar1_then_restores_the_default_selecto }, ) assert router.routing_strategy == "lar1" - assert "async_get_available_deployment" in router.__dict__ + assert "_core_async_get_available_deployment" in router.__dict__ router._switch_routing_strategy("usage-based-routing-v2", {}) assert router.lowesttpm_logger_v2 is not None @@ -2925,7 +2925,7 @@ def test_model_group_info_with_stringified_cost_values(): return None with patch.object( - router, "get_deployment_model_info", side_effect=_model_info_with_str_costs + router, "_core_get_deployment_model_info", side_effect=_model_info_with_str_costs ): result = router._set_model_group_info( model_group="my-custom-model", @@ -2973,7 +2973,7 @@ def test_model_group_info_db_fallback_with_stringified_cost_values(): ) with patch.object( - router, "get_deployment_model_info", side_effect=Exception("not found") + router, "_core_get_deployment_model_info", side_effect=Exception("not found") ): result = router._set_model_group_info( model_group="my-custom-model", @@ -7828,7 +7828,7 @@ async def test_async_function_with_fallbacks_common_utils(): def test_should_include_deployment(): - """Test that Router.should_include_deployment returns the correct response""" + """Test that Router._core_should_include_deployment returns the correct response""" router = litellm.Router( model_list=[ { @@ -7883,7 +7883,7 @@ def test_pre_call_checks_skips_token_count_without_max_input_tokens(monkeypatch) ], enable_pre_call_checks=True, ) - monkeypatch.setattr(router, "get_router_model_info", lambda **kwargs: {}) + monkeypatch.setattr(router, "_core_get_router_model_info", lambda **kwargs: {}) calls = [] monkeypatch.setattr( @@ -7917,7 +7917,7 @@ def test_pre_call_checks_counts_once_and_filters_on_max_input_tokens(monkeypatch enable_pre_call_checks=True, ) monkeypatch.setattr( - router, "get_router_model_info", lambda **kwargs: {"max_input_tokens": 5} + router, "_core_get_router_model_info", lambda **kwargs: {"max_input_tokens": 5} ) calls = [] @@ -8031,7 +8031,7 @@ def test_pre_call_checks_uses_precounted_tokens(monkeypatch): enable_pre_call_checks=True, ) monkeypatch.setattr( - router, "get_router_model_info", lambda **kwargs: {"max_input_tokens": 5} + router, "_core_get_router_model_info", lambda **kwargs: {"max_input_tokens": 5} ) calls = [] @@ -8065,7 +8065,7 @@ async def test_async_get_healthy_deployments_counts_tokens_off_the_event_loop(mo enable_pre_call_checks=True, ) monkeypatch.setattr( - router, "get_router_model_info", lambda **kwargs: {"max_input_tokens": 1_000_000} + router, "_core_get_router_model_info", lambda **kwargs: {"max_input_tokens": 1_000_000} ) counting_threads = [] @@ -8115,7 +8115,7 @@ def test_pre_call_checks_need_token_count(monkeypatch, model_info, expected): ], enable_pre_call_checks=True, ) - monkeypatch.setattr(router, "get_router_model_info", lambda **kwargs: model_info) + monkeypatch.setattr(router, "_core_get_router_model_info", lambda **kwargs: model_info) deployments = [ {"litellm_params": {"model": "gpt-3.5-turbo"}, "model_info": {"id": "d1"}}, @@ -8141,7 +8141,7 @@ def test_deployment_max_input_tokens_survives_an_unmappable_deployment(monkeypat raise ValueError("This model isn't mapped yet.") return {"max_input_tokens": 100} - monkeypatch.setattr(router, "get_router_model_info", flaky_model_info) + monkeypatch.setattr(router, "_core_get_router_model_info", flaky_model_info) unmapped = {"litellm_params": {"model": "gpt-3.5-turbo"}, "model_info": {"id": "unmapped"}} mapped = {"litellm_params": {"model": "gpt-3.5-turbo"}, "model_info": {"id": "mapped"}} @@ -8163,7 +8163,7 @@ def test_pre_call_checks_does_not_recount_inline_after_an_off_loop_failure(monke enable_pre_call_checks=True, ) monkeypatch.setattr( - router, "get_router_model_info", lambda **kwargs: {"max_input_tokens": 5} + router, "_core_get_router_model_info", lambda **kwargs: {"max_input_tokens": 5} ) calls = [] @@ -8198,7 +8198,7 @@ async def test_async_get_healthy_deployments_never_recounts_on_the_loop(monkeypa enable_pre_call_checks=True, ) monkeypatch.setattr( - router, "get_router_model_info", lambda **kwargs: {"max_input_tokens": 5} + router, "_core_get_router_model_info", lambda **kwargs: {"max_input_tokens": 5} ) counting_threads = [] @@ -8235,7 +8235,7 @@ async def test_acount_pre_call_check_tokens_leaves_the_event_loop_free(monkeypat enable_pre_call_checks=True, ) monkeypatch.setattr( - router, "get_router_model_info", lambda **kwargs: {"max_input_tokens": 5} + router, "_core_get_router_model_info", lambda **kwargs: {"max_input_tokens": 5} ) deployments = [ @@ -8269,7 +8269,7 @@ async def test_acount_pre_call_check_tokens_skips_without_max_input_tokens(monke ], enable_pre_call_checks=True, ) - monkeypatch.setattr(router, "get_router_model_info", lambda **kwargs: {}) + monkeypatch.setattr(router, "_core_get_router_model_info", lambda **kwargs: {}) calls = [] monkeypatch.setattr( @@ -8303,7 +8303,7 @@ def test_pre_call_checks_counts_tokens_from_responses_input_string(monkeypatch): enable_pre_call_checks=True, ) monkeypatch.setattr( - router, "get_router_model_info", lambda **kwargs: {"max_input_tokens": 1} + router, "_core_get_router_model_info", lambda **kwargs: {"max_input_tokens": 1} ) deployments = [ @@ -8330,7 +8330,7 @@ def test_pre_call_checks_counts_tokens_from_responses_input_list(monkeypatch): enable_pre_call_checks=True, ) monkeypatch.setattr( - router, "get_router_model_info", lambda **kwargs: {"max_input_tokens": 1} + router, "_core_get_router_model_info", lambda **kwargs: {"max_input_tokens": 1} ) deployments = [ @@ -8374,7 +8374,7 @@ def test_pre_call_checks_counts_responses_instructions_tokens(monkeypatch): assert with_instructions_tokens > input_only_tokens monkeypatch.setattr( - router, "get_router_model_info", lambda **kwargs: {"max_input_tokens": input_only_tokens} + router, "_core_get_router_model_info", lambda **kwargs: {"max_input_tokens": input_only_tokens} ) with pytest.raises(litellm.ContextWindowExceededError): router._pre_call_checks( @@ -8445,7 +8445,7 @@ def test_pre_call_checks_counts_tool_definition_tokens(monkeypatch, prompt_kwarg messages=prompt_kwargs.get("messages"), input=prompt_kwargs.get("input") ) monkeypatch.setattr( - router, "get_router_model_info", lambda **kwargs: {"max_input_tokens": prompt_only_tokens} + router, "_core_get_router_model_info", lambda **kwargs: {"max_input_tokens": prompt_only_tokens} ) assert len(router._pre_call_checks(model="m", healthy_deployments=deployments, **prompt_kwargs)) == 1 @@ -8486,7 +8486,7 @@ def test_pre_call_checks_counts_anthropic_system_tokens(monkeypatch, system): messages = [{"role": "user", "content": "hi"}] messages_only_tokens = router._count_pre_call_check_tokens(messages=messages, input=None) - monkeypatch.setattr(router, "get_router_model_info", lambda **kwargs: {"max_input_tokens": messages_only_tokens}) + monkeypatch.setattr(router, "_core_get_router_model_info", lambda **kwargs: {"max_input_tokens": messages_only_tokens}) assert len(router._pre_call_checks(model="m", healthy_deployments=deployments, messages=messages)) == 1 with pytest.raises(litellm.ContextWindowExceededError): @@ -8605,7 +8605,7 @@ def test_pre_call_checks_no_messages_or_input_does_not_crash(monkeypatch): enable_pre_call_checks=True, ) monkeypatch.setattr( - router, "get_router_model_info", lambda **kwargs: {"max_input_tokens": 5} + router, "_core_get_router_model_info", lambda **kwargs: {"max_input_tokens": 5} ) counted: list[dict] = [] @@ -9952,7 +9952,7 @@ def test_deployment_usable_by_team_uses_dynamic_model_info_get(): def get(self, key: str) -> str | None: return "team-b" if key == "team_id" else None - assert litellm.Router.deployment_usable_by_team({"model_info": ModelInfo()}, "team-a") is False + assert litellm.Router._core_deployment_usable_by_team({"model_info": ModelInfo()}, "team-a") is False def test_get_deployment_credentials_with_provider_skips_other_team_wildcard(): @@ -11599,7 +11599,7 @@ def test_access_group_filter_empty_does_not_bypass_via_litellm_model_fallback( team_id=team_id, ) - monkeypatch.setattr(router, "get_model_access_groups", fake_get_model_access_groups) + monkeypatch.setattr(router, "_core_get_model_access_groups", fake_get_model_access_groups) scoped_key = UserAPIKeyAuth( api_key="hashed-key", @@ -11676,7 +11676,7 @@ def test_access_group_block_does_not_silently_use_default_fallback_model( team_id=team_id, ) - monkeypatch.setattr(router, "get_model_access_groups", fake_get_model_access_groups) + monkeypatch.setattr(router, "_core_get_model_access_groups", fake_get_model_access_groups) scoped_key = UserAPIKeyAuth( api_key="hashed-key", @@ -11745,7 +11745,7 @@ def test_access_group_block_via_litellm_model_branch_does_not_use_default_fallba team_id=team_id, ) - monkeypatch.setattr(router, "get_model_access_groups", fake_get_model_access_groups) + monkeypatch.setattr(router, "_core_get_model_access_groups", fake_get_model_access_groups) scoped_key = UserAPIKeyAuth( api_key="hashed-key", @@ -12246,7 +12246,7 @@ def test_get_deployment_credentials_with_provider_returns_none_for_blocked_deplo def test_is_deployment_blocked_static_helper_reflects_blocked_flag(): """ - Exercises Router.is_deployment_blocked so router_code_coverage.py (AST call graph) + Exercises Router._core_is_deployment_blocked so router_code_coverage.py (AST call graph) marks the helper as covered by router-named tests. """ import types @@ -12257,19 +12257,80 @@ def test_is_deployment_blocked_static_helper_reflects_blocked_flag(): blocked_dep = router.get_deployment("dep-0") unblocked_dep = router.get_deployment("dep-1") assert blocked_dep is not None and unblocked_dep is not None - assert litellm.Router.is_deployment_blocked(blocked_dep) is True - assert litellm.Router.is_deployment_blocked(unblocked_dep) is False + assert litellm.Router._core_is_deployment_blocked(blocked_dep) is True + assert litellm.Router._core_is_deployment_blocked(unblocked_dep) is False # No model_info on deployment object → treated as not blocked - assert litellm.Router.is_deployment_blocked(object()) is False + assert litellm.Router._core_is_deployment_blocked(object()) is False missing_blocked = types.SimpleNamespace() - assert litellm.Router.is_deployment_blocked(types.SimpleNamespace(model_info=missing_blocked)) is False + assert litellm.Router._core_is_deployment_blocked(types.SimpleNamespace(model_info=missing_blocked)) is False assert ( - litellm.Router.is_deployment_blocked(types.SimpleNamespace(model_info=types.SimpleNamespace(blocked=True))) + litellm.Router._core_is_deployment_blocked(types.SimpleNamespace(model_info=types.SimpleNamespace(blocked=True))) is True ) +def test_synchronous_text_completion_rejects_a_restricted_model_before_the_provider() -> None: + model: Final = "text-completion-openai/unit-text-model" + router: Final = Router( + model_list=[ + { + "model_name": model, + "litellm_params": { + "model": model, + "api_base": "https://sync-router.invalid/v1", + "api_key": "unit-provider-key", + }, + "model_info": {"enforce_supported_endpoints": True, "supported_endpoints": ["/v1/embeddings"]}, + } + ], + num_retries=0, + ) + with respx.mock(assert_all_called=False) as upstream: + provider: Final = upstream.post("https://sync-router.invalid/v1/completions").respond( + 200, + json={ + "id": "cmpl-unit", + "object": "text_completion", + "created": 1, + "model": "unit-text-model", + "choices": [{"text": "provider called", "index": 0, "finish_reason": "stop", "logprobs": None}], + "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2}, + }, + ) + with pytest.raises(litellm.BadRequestError, match="/v1/completions is not supported"): + router.text_completion(model=model, prompt="test") + assert provider.call_count == 0 + + +def test_routable_deployments_leave_out_models_that_refuse_the_call_type() -> None: + router: Final = Router( + model_list=[ + { + "model_name": "available", + "litellm_params": {"model": "openai/gpt-5.4-nano", "api_key": "unit-provider-key"}, + }, + { + "model_name": "embed-only", + "litellm_params": {"model": "openai/text-embedding-3-small", "api_key": "unit-provider-key"}, + "model_info": {"enforce_supported_endpoints": True, "supported_endpoints": ["/v1/embeddings"]}, + }, + ], + num_retries=0, + ) + + def routable(model: str, call_type: str | None) -> tuple[str, ...]: + return tuple( + deployment["model_name"] for deployment in router.get_routable_deployments(model, {}, call_type=call_type) + ) + + assert routable("unregistered-fallback", "acompletion") == () + assert routable("available", "acompletion") == ("available",) + assert routable("embed-only", "acompletion") == () + assert routable("embed-only", "aembedding") == ("embed-only",) + assert routable("embed-only", None) == ("embed-only",) + + def test_routing_groups_legacy_property_forwards_to_public_attribute(): router = _router_with_two_deployments([False, False]) @@ -14930,7 +14991,7 @@ def test_pre_call_checks_uses_deployment_model_when_model_info_lookup_raises(mon def _raise_unmapped(**kwargs): raise ValueError("This model isn't mapped yet") - monkeypatch.setattr(router, "get_router_model_info", _raise_unmapped) + monkeypatch.setattr(router, "_core_get_router_model_info", _raise_unmapped) seen: list[tuple] = [] original_get_supported_openai_params = litellm.get_supported_openai_params @@ -17121,7 +17182,7 @@ def test_model_group_info_intersects_supported_reasoning_efforts(): "supports_xhigh_reasoning_effort": False, } - with patch.object(router, "get_deployment_model_info", side_effect=_model_info): + with patch.object(router, "_core_get_deployment_model_info", side_effect=_model_info): result = router._set_model_group_info( model_group="smart-group", user_facing_model_group_name="smart-group", @@ -17165,7 +17226,7 @@ def test_model_group_info_reasoning_efforts_are_unknown_when_any_deployment_is_o } return {"key": model_name, "litellm_provider": "openai", "mode": None, "supports_reasoning": None} - with patch.object(router, "get_deployment_model_info", side_effect=_model_info): + with patch.object(router, "_core_get_deployment_model_info", side_effect=_model_info): result = router._set_model_group_info( model_group="smart-group", user_facing_model_group_name="smart-group", @@ -17300,7 +17361,7 @@ def test_model_group_info_reasoning_efforts_empty_on_a_mapped_non_reasoning_depl } return {"key": model_name, "litellm_provider": "openai", "mode": "chat", "supports_reasoning": None} - with patch.object(router, "get_deployment_model_info", side_effect=_model_info): + with patch.object(router, "_core_get_deployment_model_info", side_effect=_model_info): result = router._set_model_group_info( model_group="mixed-group", user_facing_model_group_name="mixed-group", @@ -17341,7 +17402,7 @@ def test_model_group_info_reasoning_efforts_ignore_a_value_declared_in_model_inf info["supported_reasoning_efforts"] = ("high",) return info - with patch.object(router, "get_deployment_model_info", side_effect=_model_info): + with patch.object(router, "_core_get_deployment_model_info", side_effect=_model_info): result = router._set_model_group_info( model_group="declared-group", user_facing_model_group_name="declared-group", @@ -17375,7 +17436,7 @@ def test_model_group_info_survives_a_junk_typed_operator_effort_value(): "supported_reasoning_efforts": "high", } - with patch.object(router, "get_deployment_model_info", side_effect=_model_info): + with patch.object(router, "_core_get_deployment_model_info", side_effect=_model_info): result = router._set_model_group_info( model_group="junk-declared-group", user_facing_model_group_name="junk-declared-group", @@ -18803,6 +18864,35 @@ async def test_anthropic_messages_stream_dropped_before_content_is_retried_withi assert [(attempted, budget) for _, attempted, budget in provider.calls] == [(0, 2), (1, 2)] +@pytest.mark.asyncio +async def test_anthropic_messages_same_group_retry_skips_an_embedding_only_deployment(): + """The retry of a /v1/messages stream dropped before content must not land on an enforced embedding-only + deployment of the same group, even when that deployment is the group's preferred order.""" + router: Final = Router( + model_list=[ + {"model_name": "glm", "litellm_params": {"model": "anthropic/glm-chat", "api_key": "sk-test", "order": 2}}, + { + "model_name": "glm", + "litellm_params": {"model": "anthropic/glm-embed", "api_key": "sk-test", "order": 1}, + "model_info": {"enforce_supported_endpoints": True, "supported_endpoints": ["/v1/embeddings"]}, + }, + ], + num_retries=1, + ) + provider: Final = _AnthropicMessagesScriptedProvider( + _anthropic_messages_dropped_before_content, _anthropic_messages_retried_stream + ) + + async def anthropic_messages(**kwargs: object) -> object: + return await provider(**kwargs) + + stream: Final = await _anthropic_messages_stream_through_router(router, anthropic_messages) + body: Final = [chunk async for chunk in stream] + + assert body == [_anthropic_messages_message_start_chunk(), _anthropic_messages_content_chunk("pong")] + assert [model for model, _, _ in provider.calls] == ["anthropic/glm-chat", "anthropic/glm-chat"] + + class _AnthropicMessagesRetryStashRecorder: def __init__(self) -> None: self.observed: tuple[Usage | None, ...] = () @@ -21632,7 +21722,7 @@ async def test_router_retry_policy_controls_upstream_attempt_count( "error_body,error_type", [ ({"message": "model is down", "type": "server_error"}, litellm.NotFoundError), - ({"message": "Response with id 'resp_x' not found.", "type": "invalid_request_error"}, litellm.BadRequestError), + ({"message": "Response with id 'resp_x' not found.", "type": "invalid_request_error"}, litellm.NotFoundError), ], ) @pytest.mark.parametrize( @@ -23903,7 +23993,7 @@ def test_a_failed_routing_read_prefetch_logs_the_request_model_without_its_line_ def fail_lookup(model_name: str | None = None, team_id: str | None = None) -> None: raise RuntimeError(f"no deployments for {model_name}") - monkeypatch.setattr(router, "get_model_list", fail_lookup) + monkeypatch.setattr(router, "_core_get_model_list", fail_lookup) caplog.clear() with caplog.at_level(logging.DEBUG, logger="LiteLLM Router"): @@ -25913,7 +26003,7 @@ async def test_router_acancel_batch_removes_unresolved_credential_name(): mock_response.status = "cancelled" with ( - patch.object(router, "get_deployment_credentials_with_provider", return_value=None), + patch.object(router, "_core_get_deployment_credentials_with_provider", return_value=None), patch.object(litellm, "acancel_batch", new_callable=AsyncMock) as mock_cancel, ): mock_cancel.return_value = mock_response @@ -29000,7 +29090,7 @@ def test_reset_custom_routing_strategy(): router.set_custom_routing_strategy(SpecialEndpointRoutingStrategy(router)) assert "get_available_deployment" in router.__dict__ - assert "async_get_available_deployment" in router.__dict__ + assert "_core_async_get_available_deployment" in router.__dict__ router._reset_custom_routing_strategy() @@ -31089,7 +31179,7 @@ def test_team_model_has_alternatives_when_a_delete_starts_after_its_deployment_l has_alternatives: Final = _read_during_held_write( lambda: router.team_model_has_alternatives("deployment-3"), lambda: router.delete_deployment("deployment-0"), - write_after=Router.get_deployment.__code__, + write_after=Router._core_get_deployment.__code__, ) assert has_alternatives is True @@ -31431,3 +31521,26 @@ def test_router_keeps_supported_fallback_shapes( router: Final = Router(model_list=list(_FALLBACK_VALIDATION_MODEL_LIST), **router_kwargs) assert router.fallbacks == expected_fallbacks + + +def test_router_warns_when_a_deployment_enforces_endpoints_without_a_list(caplog: pytest.LogCaptureFixture) -> None: + with caplog.at_level(logging.WARNING, logger="LiteLLM Router"): + router: Final = Router( + model_list=[ + { + "model_name": "embed-from-config", + "litellm_params": {"model": "openai/text-embedding-3-small", "api_key": "sk-fake"}, + "model_info": {"enforce_supported_endpoints": True}, + } + ] + ) + router.add_deployment( + Deployment( + model_name="embed-from-api", + litellm_params=LiteLLM_Params(model="openai/text-embedding-3-small", api_key="sk-fake"), + model_info=ModelInfo(enforce_supported_endpoints=True, supported_endpoints=["/v1/embeddings", 7]), + ) + ) + + assert "Deployment 'embed-from-config' sets enforce_supported_endpoints without" in caplog.text + assert "Deployment 'embed-from-api' sets enforce_supported_endpoints without" in caplog.text diff --git a/tests/unit/test_router/test_router_entry_points.py b/tests/unit/test_router/test_router_entry_points.py new file mode 100644 index 00000000000..2df5cbc9457 --- /dev/null +++ b/tests/unit/test_router/test_router_entry_points.py @@ -0,0 +1,829 @@ +""" +Router entry points and DeprecatedRouterMembers. + +Every member that moved into DeprecatedRouterMembers must return what its entry point returns, +warn once per access from outside the router, and count the access. Internal access (the router's +own modules) must stay silent. +""" + +import asyncio +import copy +import dataclasses +import warnings + +import httpx +import pytest + +import litellm +from litellm import Router +from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler +from litellm.router_api import SETTING_FIELDS, ChangeSet, RouterSettings +from litellm.router_deprecated import DEPRECATED_ROUTER_MEMBER_USES, DeprecatedRouterMembers +from litellm.types.router import Deployment + + +def _router() -> Router: + return Router( + model_list=[ + { + "model_name": "gpt", + "litellm_params": {"model": "openai/gpt-4o-mini", "api_key": "sk-a"}, + "model_info": {"id": "dep-a", "access_groups": ["grp"]}, + }, + { + "model_name": "gpt", + "litellm_params": {"model": "openai/gpt-4o", "api_key": "sk-b"}, + "model_info": {"id": "dep-b"}, + }, + { + "model_name": "team-internal", + "litellm_params": {"model": "openai/gpt-4o-mini", "api_key": "sk-t"}, + "model_info": {"id": "dep-team", "team_id": "team-1", "team_public_model_name": "team-gpt"}, + }, + { + "model_name": "openai/*", + "litellm_params": {"model": "openai/*", "api_key": "sk-w"}, + "model_info": {"id": "dep-wild"}, + }, + ], + model_group_alias={"alias-gpt": "gpt"}, + fallbacks=[{"gpt": ["team-internal"]}], + cooldown_time=7, + ) + + +@pytest.fixture +def router(): + r = _router() + yield r + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + r.retire() + + +# (old member, how to read it through the old name, how to read it through the entry point) +READS = [ + ("model_list", lambda r: r.model_list, lambda r: r.registry().all()), + ("model_names", lambda r: r.model_names, lambda r: r.registry().names(kind="group_set")), + ("deployment_names", lambda r: r.deployment_names, lambda r: r.registry().names(kind="deployment_names")), + ("team_public_model_names", lambda r: r.team_public_model_names, lambda r: r.registry().names(kind="team_public")), + ( + "model_name_to_deployment_indices", + lambda r: r.model_name_to_deployment_indices, + lambda r: r.registry().index("model_name"), + ), + ( + "team_model_to_deployment_indices", + lambda r: r.team_model_to_deployment_indices, + lambda r: r.registry().index("team_model"), + ), + ("model_group_alias", lambda r: r.model_group_alias, lambda r: r.registry().aliases()), + ("pattern_router", lambda r: r.pattern_router, lambda r: r.registry().patterns()), + ("team_pattern_routers", lambda r: r.team_pattern_routers, lambda r: r.registry().patterns(team=True)), + ("routing_groups", lambda r: r.routing_groups, lambda r: r.registry().routing_groups()), + ("auto_routers", lambda r: r.auto_routers, lambda r: r.registry().plugins("auto")), + ("complexity_routers", lambda r: r.complexity_routers, lambda r: r.registry().plugins("complexity")), + ("quality_routers", lambda r: r.quality_routers, lambda r: r.registry().plugins("quality")), + ("adaptive_routers", lambda r: r.adaptive_routers, lambda r: r.registry().plugins("adaptive")), + ("routing_plugins", lambda r: r.routing_plugins, lambda r: r.registry().plugins("routing")), + ( + "prompt_caching_pin_cache", + lambda r: r.prompt_caching_pin_cache, + lambda r: r.registry().plugins("prompt_caching_pins"), + ), + ("cache", lambda r: r.cache, lambda r: r.health().store), + ("cooldown_cache", lambda r: r.cooldown_cache, lambda r: r.health().cooldowns), + ("health_state_cache", lambda r: r.health_state_cache, lambda r: r.health().health_checks), + ("deployment_latency_map", lambda r: r.deployment_latency_map, lambda r: r.health().latency), + ("get_deployment", lambda r: r.get_deployment("dep-a"), lambda r: r.registry().deployment("dep-a")), + ("has_model_id", lambda r: r.has_model_id("dep-b"), lambda r: r.registry().has_deployment("dep-b")), + ("get_model_info", lambda r: r.get_model_info("dep-a"), lambda r: r.registry().deployment_dict("dep-a")), + ("get_model_list", lambda r: r.get_model_list("gpt"), lambda r: r.registry().deployments("gpt")), + ("get_model_list_all", lambda r: r.get_model_list(), lambda r: r.registry().deployments()), + ("get_model_list_alias", lambda r: r.get_model_list("alias-gpt"), lambda r: r.registry().deployments("alias-gpt")), + ( + "get_all_deployments", + lambda r: r.get_all_deployments("gpt"), + lambda r: r.registry().deployments("gpt", source="all"), + ), + ( + "config_deployments", + lambda r: list(r.config_deployments()), + lambda r: list(r.registry().deployments(source="config")), + ), + ( + "deployments_named", + lambda r: r.deployments_named("gpt"), + lambda r: r.registry().deployments("gpt", source="named"), + ), + ( + "get_model_list_from_model_alias", + lambda r: r.get_model_list_from_model_alias("alias-gpt"), + lambda r: r.registry().deployments("alias-gpt", source="alias"), + ), + ( + "get_deployment_by_model_group_name", + lambda r: r.get_deployment_by_model_group_name("gpt"), + lambda r: r.registry().deployments("gpt", source="first_in_group"), + ), + ("get_model_names", lambda r: r.get_model_names(), lambda r: r.registry().names()), + ( + "get_model_names_team", + lambda r: r.get_model_names(team_id="team-1"), + lambda r: r.registry().names(team_id="team-1"), + ), + ("get_model_ids", lambda r: r.get_model_ids("gpt"), lambda r: r.registry().names("gpt", kind="ids")), + ( + "get_model_ids_all", + lambda r: r.get_model_ids(exclude_team_models=True), + lambda r: r.registry().names(kind="ids", exclude_team_models=True), + ), + ( + "get_model_from_alias", + lambda r: r.get_model_from_alias("alias-gpt"), + lambda r: r.registry().resolve("alias-gpt", kind="alias"), + ), + ( + "is_recognized_model", + lambda r: r.is_recognized_model("gpt"), + lambda r: r.registry().resolve("gpt", kind="recognized"), + ), + ( + "routable_model_group", + lambda r: r.routable_model_group("alias-gpt"), + lambda r: r.registry().resolve("alias-gpt"), + ), + ( + "map_team_model", + lambda r: r.map_team_model("team-gpt", "team-1"), + lambda r: r.registry().resolve("team-gpt", kind="team_model", team_id="team-1"), + ), + ( + "get_model_group", + lambda r: r.get_model_group("dep-a"), + lambda r: r.registry().resolve("dep-a", kind="group_of_id"), + ), + ( + "get_search_tool_support", + lambda r: r.get_search_tool_support("gpt"), + lambda r: r.registry().resolve("gpt", kind="search_tool_support"), + ), + ("get_model_access_groups", lambda r: r.get_model_access_groups(), lambda r: r.registry().access_groups()), + ( + "get_deployment_credentials", + lambda r: r.get_deployment_credentials("dep-a"), + lambda r: r.registry().credentials("dep-a"), + ), + ( + "get_deployment_credentials_with_provider", + lambda r: r.get_deployment_credentials_with_provider("dep-a"), + lambda r: r.registry().credentials("dep-a", form="with_provider"), + ), + ( + "get_credential_deployment", + lambda r: r.get_credential_deployment("dep-a"), + lambda r: r.registry().credentials("dep-a", form="deployment"), + ), + ("get_configured_mode", lambda r: r.get_configured_mode("gpt"), lambda r: r.registry().group("gpt", field="mode")), + ( + "get_configured_token_limits", + lambda r: r.get_configured_token_limits("gpt"), + lambda r: r.registry().group("gpt", field="token_limits"), + ), + ( + "get_fully_blocked_model_names", + lambda r: r.get_fully_blocked_model_names(), + lambda r: r.health().fully_blocked_model_names(), + ), + ("get_settings", lambda r: r.get_settings(), lambda r: r.settings().as_dict()), + ("fallbacks", lambda r: r.fallbacks, lambda r: r.settings().fallbacks), + ("cooldown_time", lambda r: r.cooldown_time, lambda r: r.settings().cooldown_time), + ("routing_strategy", lambda r: r.routing_strategy, lambda r: r.settings().routing_strategy), +] + + +@pytest.mark.parametrize("name, old, new", READS, ids=[r[0] for r in READS]) +def test_deprecated_read_matches_entry_point(router, name, old, new): + with warnings.catch_warnings(record=True) as caught: + warnings.simplefilter("always") + old_value = old(router) + new_value = new(router) + if isinstance(old_value, (list, dict, set)) or hasattr(old_value, "__dict__"): + assert old_value == new_value + else: + assert old_value == new_value + assert any(issubclass(w.category, DeprecationWarning) for w in caught), name + + +def test_live_objects_not_copies(router): + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + assert router.model_list is router.registry().all() + assert router.fallbacks is router.settings().fallbacks + assert router.cache is router.health().store + assert router.model_group_alias is router.registry().aliases() + + +def test_assignment_goes_through_apply(router): + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + router.fallbacks = [{"gpt": ["openai/gpt-4.1"]}] + assert router.settings().fallbacks == [{"gpt": ["openai/gpt-4.1"]}] + router.search_tools = [{"search_tool_name": "s", "litellm_params": {"search_provider": "tavily"}}] + assert router.settings().search_tools[0]["search_tool_name"] == "s" + router.apply(ChangeSet(assign={"cooldown_time": 3})) + assert router.settings().cooldown_time == 3 + with pytest.raises(ValueError, match="not assignable router settings"): + router.apply(ChangeSet(assign={"not_a_setting": 1})) + + +def test_apply_upsert_and_delete_bump_version(router): + v0 = router.registry().version + added = Deployment( + model_name="new-group", + litellm_params={"model": "openai/gpt-4o-mini", "api_key": "sk-n"}, + model_info={"id": "dep-new"}, + ) + result = router.apply(ChangeSet(upsert=[added])) + assert result.version == v0 + 1 + assert router.registry().has_deployment("dep-new") + assert router.registry().deployments("new-group")[0]["model_info"]["id"] == "dep-new" + result = router.apply(ChangeSet(delete=["dep-new"])) + assert result.version == v0 + 2 + assert result.deleted[0] is not None + assert not router.registry().has_deployment("dep-new") + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + assert router.upsert_deployment(added) is added + assert router.upsert_deployment(added) is None # unchanged + assert router.delete_deployment("dep-new") is not None + + +def test_old_deployment_writes_also_advance_the_version(router): + v0 = router.registry().version + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + router.add_deployment( + Deployment( + model_name="added", + litellm_params={"model": "openai/gpt-4o-mini", "api_key": "sk-x"}, + model_info={"id": "dep-added"}, + ) + ) + v1 = router.registry().version + router.set_model_list([{"model_name": "only", "litellm_params": {"model": "openai/gpt-4o", "api_key": "sk-o"}}]) + assert v0 < v1 < router.registry().version + assert router.registry().names() == ["only"] + v2 = router.registry().version + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + router.model_list = [] + assert router.registry().version > v2 + + +def test_update_settings_through_apply(router): + router.apply(ChangeSet(settings={"cooldown_time": "11", "not_allowed": 1})) + assert router.settings().cooldown_time == 11 + + +def test_aapply_applies_changes_then_refreshes_model_info(monkeypatch): + monkeypatch.setattr(litellm, "model_cost", copy.deepcopy(litellm.model_cost)) + router = Router( + model_list=[ + { + "model_name": "local", + "litellm_params": { + "model": "hosted_vllm/local-model", + "api_base": "https://capacity.test/v1", + "api_key": "local-key", + }, + "model_info": {"id": "dep-local"}, + } + ] + ) + with pytest.raises(TypeError, match="needs aapply"): + router.apply(ChangeSet(refresh_model_info=True)) + + async def refresh() -> None: + handler = AsyncHTTPHandler() + await handler.client.aclose() + async with httpx.AsyncClient( + transport=httpx.MockTransport( + lambda request: httpx.Response(200, json={"data": [{"id": "local-model", "max_model_len": 4096}]}) + ) + ) as client: + handler.client = client + await router.aapply(ChangeSet(assign={"cooldown_time": 4}, refresh_model_info=True, refresh_client=handler)) + + asyncio.run(refresh()) + assert router.settings().cooldown_time == 4 + assert router.registry().group("local", field="token_limits") == (4096, 4096) + + +def test_settings_snapshot_is_reused_until_a_setting_is_written(router): + first = router.settings() + assert router.settings() is first + router.apply(ChangeSet(assign={"cooldown_time": 7})) + assert router.settings() is not first + assert router.settings().cooldown_time == 7 + router.apply(ChangeSet(settings={"cooldown_time": "8"})) + assert router.settings().cooldown_time == 8 + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + router.fallbacks = [{"gpt": ["team-gpt"]}] + assert router.settings().fallbacks == [{"gpt": ["team-gpt"]}] + router._core_fallbacks = [{"gpt": ["other"]}] # the router's own writes invalidate it too + assert router.settings().fallbacks == [{"gpt": ["other"]}] + router.settings().fallbacks.append({"x": ["y"]}) # the snapshot holds the router's own objects + assert router._core_fallbacks[-1] == {"x": ["y"]} + unrelated = router.settings() + router._core_model_list = list(router._core_model_list) + assert router.settings() is unrelated + + +def test_deleting_a_setting_shows_as_unset(router): + router.apply(ChangeSet(assign={"cooldown_time": 3})) + assert router.settings().is_set("cooldown_time") + del router._core_cooldown_time + assert router.settings().is_set("cooldown_time") is (hasattr(router, "_core_cooldown_time")) + + +def test_settings_snapshot_is_frozen(router): + snapshot = router.settings() + assert isinstance(snapshot, RouterSettings) + with pytest.raises(dataclasses.FrozenInstanceError): + snapshot.cooldown_time = 1 # type: ignore[misc] + assert set(SETTING_FIELDS) <= set(RouterSettings.__dataclass_fields__) + + +def test_unset_setting_raises_attribute_error_through_old_name(): + r = _router() + try: + del r.__dict__["_core_kubernetes_pod_discovery"] + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + assert getattr(r, "kubernetes_pod_discovery", "missing") == "missing" + assert r.settings().is_set("kubernetes_pod_discovery") is False + finally: + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + r.retire() + + +def test_unset_attribute_error_names_the_public_attribute(router): + # Reading (or deleting) an old attribute the router never set raises what reading it raised before. + router.__dict__.pop("_core_slack_alerting_logger", None) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + with pytest.raises(AttributeError) as read: + _ = router.slack_alerting_logger + with pytest.raises(AttributeError) as deleted: + del router.slack_alerting_logger + del router.__dict__["_core_kubernetes_pod_discovery"] + with pytest.raises(AttributeError) as setting: + _ = router.kubernetes_pod_discovery + for caught, name in ((read, "slack_alerting_logger"), (setting, "kubernetes_pod_discovery")): + assert str(caught.value) == f"'Router' object has no attribute '{name}'" + assert caught.value.name == name + assert caught.value.obj is router + # A failed del sets no name/obj in Python either. + assert str(deleted.value) == "'Router' object has no attribute 'slack_alerting_logger'" + + +def test_bind_to_request_logging_none_raises_as_before(router): + # The old member bound unconditionally, so None fails on the first callback registration. + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + with pytest.raises(AttributeError, match="add_dynamic_function_callbacks"): + router.bind_to_request_logging(None) + + +def test_select_picks_like_async_get_available_deployment(router): + picked = asyncio.run(router.select("gpt", {})) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + old = asyncio.run(router.async_get_available_deployment(model="gpt", request_kwargs={})) + assert picked["model_name"] == old["model_name"] == "gpt" + healthy = asyncio.run(router.select("gpt", {}, kind="healthy")) + assert {d["model_info"]["id"] for d in healthy} == {"dep-a", "dep-b"} + + +def test_counter_and_internal_silence(router): + before = DEPRECATED_ROUTER_MEMBER_USES["get_model_ids"] + with warnings.catch_warnings(record=True) as caught: + warnings.simplefilter("always") + router.get_model_ids() + # Router internals call the core names; a completion must not warn. + asyncio.run(router.acompletion(model="gpt", messages=[{"role": "user", "content": "hi"}], mock_response="ok")) + assert DEPRECATED_ROUTER_MEMBER_USES["get_model_ids"] == before + 1 + deprecations = [w for w in caught if issubclass(w.category, DeprecationWarning) and "Router." in str(w.message)] + assert [str(w.message).split(" ")[0] for w in deprecations] == ["Router.get_model_ids"] + assert deprecations[0].filename == __file__ + + +def test_every_deprecated_member_names_a_replacement(): + import inspect + + from litellm.router_deprecated import _DeprecatedAttribute + + for name, member in vars(DeprecatedRouterMembers).items(): + if name.startswith("__"): + continue + if isinstance(member, _DeprecatedAttribute): + assert member.replacement + continue + func = member.__func__ if isinstance(member, staticmethod) else member + if callable(func): + assert "_note(" in inspect.getsource(func), name + + +def test_generate_model_id_is_the_module_function(): + from litellm.router_api import deployment_id + + params = {"model": "openai/gpt-4o", "api_key": "k", "extra": {"a": 1}} + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + assert Router.generate_model_id("g", params) == deployment_id("g", params) + + +def test_subclass_override_of_an_old_member_is_still_called(): + calls = [] + + class Sub(litellm.Router): + async def async_pre_routing_hook( + self, model, request_kwargs, messages=None, input=None, specific_deployment=False + ): + calls.append(model) + return await super().async_pre_routing_hook(model, request_kwargs, messages, input, specific_deployment) + + r = Sub( + model_list=[ + { + "model_name": "m", + "litellm_params": {"model": "openai/gpt-4o-mini", "api_key": "sk-x", "mock_response": "ok"}, + } + ] + ) + response = asyncio.run(r.acompletion(model="m", messages=[{"role": "user", "content": "hi"}])) + assert response.choices[0].message.content == "ok" + assert calls == ["m"] + + +def test_direct_call_of_a_subclass_override_runs_it_once(): + calls = [] + + class Sub(litellm.Router): + def get_model_access_groups(self, model_name=None, model_access_group=None, team_id=None): + calls.append(model_name) + return super().get_model_access_groups(model_name, model_access_group, team_id) + + r = Sub(model_list=[{"model_name": "m", "litellm_params": {"model": "openai/gpt-4o-mini", "api_key": "sk-x"}}]) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + assert r.get_model_access_groups("m") == {} + assert calls == ["m"] + assert asyncio.run(asyncio.to_thread(r.registry().access_groups, model_name="m")) == {} + assert calls == ["m", "m"] + + +def test_subclass_staticmethod_override_is_still_consulted(): + calls = [] + + class Sub(litellm.Router): + @staticmethod + def is_deployment_blocked(deployment): + calls.append(deployment.model_info.id) + return True + + r = Sub( + model_list=[ + { + "model_name": "m", + "litellm_params": {"model": "openai/gpt-4o-mini", "api_key": "sk-x"}, + "model_info": {"id": "d1"}, + } + ] + ) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + assert r.get_deployment_credentials(model_id="d1") is None + assert calls == ["d1"] + + +def test_private_alias_skips_a_subclass_override_of_the_public_name(): + calls = [] + + class Sub(litellm.Router): + def get_all_deployments(self, *args, **kwargs): + calls.append("get_all_deployments") + return super().get_all_deployments(*args, **kwargs) + + r = Sub(model_list=[{"model_name": "m", "litellm_params": {"model": "openai/gpt-4o-mini", "api_key": "sk-x"}}]) + calls.clear() + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + assert len(r._get_all_deployments(model_name="m")) == 1 + assert calls == [] + + +def test_old_name_writes_and_deletes_warn(router): + with pytest.warns(DeprecationWarning, match=r"^Router\.fallbacks is deprecated"): + router.fallbacks = [] + with pytest.warns(DeprecationWarning, match=r"^Router\.fallbacks is deprecated"): + del router.fallbacks + + +def test_alias_attribute_writes_the_same_storage(router): + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + replacement = {} + router._routing_groups = replacement + assert router.routing_groups is replacement + assert router.registry().routing_groups() is replacement + + +def test_core_members_keep_their_old_names_in_errors(): + from litellm.router import _name_core_members_after_old_names + + _name_core_members_after_old_names() + + assert Router._core_get_deployment_model_info.__qualname__ == "Router.get_deployment_model_info" + assert Router._core_get_deployment.__qualname__ == "Router.get_deployment" + + +def test_wrong_arity_errors_still_name_router(router): + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + with pytest.raises(TypeError, match=r"^Router\.get_deployment\(\) takes 2 positional arguments"): + router.get_deployment("dep-a", "extra") # pyright: ignore[reportCallIssue] + with pytest.raises(TypeError, match=r"^Router\.get_available_guardrail\(\) missing 1 required"): + router.get_available_guardrail() # pyright: ignore[reportCallIssue] + with pytest.raises(TypeError, match=r"^litellm\.router\.Router\.has_model_id\(\) argument after \*\*"): + router.has_model_id(**None) # pyright: ignore[reportCallIssue, reportArgumentType] + with pytest.raises(TypeError, match=r"^Router\.is_deployment_blocked\(\) missing 1 required"): + router.is_deployment_blocked() # pyright: ignore[reportCallIssue] + with pytest.raises(TypeError, match=r"^Router\.get_deployment_model_info\(\) missing 2 required"): + router.cached_deployment_model_info() # pyright: ignore[reportCallIssue] + + +_SENTINEL = object() + +# (entry point call, Router core member it forwards to, the arguments the core member must get) +FORWARDS = [ + ( + lambda r: r.registry().deployment_model_info("dep-a", "gpt", cached=True), + "_core_cached_deployment_model_info", + (("dep-a", "gpt"), {}), + ), + ( + lambda r: r.registry().deployment_model_info("dep-a", "gpt"), + "_core_get_deployment_model_info", + ((), {"model_id": "dep-a", "model_name": "gpt"}), + ), + ( + lambda r: r.registry().router_model_info(None, "gpt", id="dep-a"), + "_core_get_router_model_info", + ((), {"deployment": None, "received_model_name": "gpt", "id": "dep-a"}), + ), + (lambda r: r.registry().discovered_model_info("dep-a"), "_core_get_discovered_model_info", (("dep-a",), {})), + ( + lambda r: r.registry().deployments("gpt", source="routing_groups"), + "_core_get_model_list_from_routing_groups", + ((), {"model_name": "gpt"}), + ), + ( + lambda r: r.registry().deployments("gpt", source="routed_group"), + "_core_get_model_list_of_routed_group", + (("gpt",), {}), + ), + ( + lambda r: r.registry().deployments("gpt", source="wildcard", team_id="t"), + "_core_wildcard_route_deployments", + (("gpt", "t"), {}), + ), + ( + lambda r: r.registry().deployments("gpt", source="routable", request={"a": 1}, call_type="aembedding"), + "_core_get_routable_deployments", + (("gpt", {"a": 1}, "aembedding"), {}), + ), + ( + lambda r: r.registry().deployments("gpt", source="for_request"), + "_core_deployments_for_request", + (("gpt", {}), {}), + ), + ( + lambda r: r.registry().includes({"model_name": "gpt"}, model_name="gpt", team_id="t"), + "_core_should_include_deployment", + ((), {"model_name": "gpt", "model": {"model_name": "gpt"}, "team_id": "t"}), + ), + ( + lambda r: r.registry().names("gpt", kind="litellm_models", team_id="t"), + "_core_resolved_litellm_models", + (("gpt",), {"team_id": "t"}), + ), + ( + lambda r: r.registry().names("gpt", kind="litellm_base_models"), + "_core_resolved_litellm_models", + (("gpt",), {"team_id": None, "use_base_model": True}), + ), + ( + lambda r: r.registry().names("gpt", kind="wildcard", team_id="t"), + "_core_wildcard_routed_models", + (("gpt", "t"), {}), + ), + ( + lambda r: r.registry().names("gpt", kind="route_candidates"), + "_core_get_candidate_model_ids_for_route", + (("gpt",), {"team_id": None}), + ), + ( + lambda r: r.registry().resolve("dep-a", kind="from_id", custom_llm_provider="openai"), + "_core_resolve_model_name_from_model_id", + (("dep-a",), {"custom_llm_provider": "openai"}), + ), + ( + lambda r: r.registry().resolve("rg", kind="routing_group_alternatives"), + "_core_routing_group_has_alternatives", + (("rg",), {}), + ), + ( + lambda r: r.registry().resolve("gpt", kind="served", served_model="gpt-4o"), + "_core_resolve_served_model_name", + (("gpt", "gpt-4o"), {}), + ), + ( + lambda r: r.registry().resolve("gpt", kind="upstream", team_id="t"), + "_core_get_routable_upstream_model", + (("gpt",), {"team_id": "t"}), + ), + ( + lambda r: r.registry().resolve("gpt", kind="search_tool_support", team_id="t"), + "_core_get_search_tool_support", + (("gpt",), {"team_id": "t"}), + ), + ( + lambda r: r.registry().resolve("grp", kind="wildcard_access_group"), + "_core_is_model_access_group_for_wildcard_route", + ((), {"model_access_group": "grp"}), + ), + ( + lambda r: r.registry().resolve("dep-a", kind="team_model_alternatives"), + "_core_team_model_has_alternatives", + (("dep-a",), {}), + ), + ( + lambda r: r.registry().group("gpt", include_hidden=True, team_id="t"), + "_core_get_model_group_info", + (("gpt",), {"include_hidden": True, "team_id": "t"}), + ), + (lambda r: r.registry().group("gpt", field="cached_info"), "_core_cached_model_group_info", (("gpt",), {})), + (lambda r: r.registry().group("gpt", field="display_name"), "_core_get_configured_display_name", (("gpt",), {})), + ( + lambda r: r.registry().group("gpt", field="service_tiers", team_id="t"), + "_core_get_configured_service_tiers", + (("gpt",), {"team_id": "t"}), + ), + (lambda r: r.registry().group("gpt", field="listing_info"), "_core_get_model_listing_info", (("gpt",), {})), + (lambda r: r.registry().plugins("budget_limiter"), "_core_get_router_deployment_budget_limiter", ((), {})), + (lambda r: r.settings().allowed_fails_for(ValueError("x")), "_core_get_allowed_fails_from_policy", None), + (lambda r: r.settings().guardrail("g"), "_core_get_available_guardrail", (("g",), {})), + (lambda r: r.health().team_ptu_ceiling("t", "gpt"), "_core_team_ptu_ceiling", (("t", "gpt"), {})), + ( + lambda r: r.health().split_priority("gpt", {"priority": 1}), + "_core_split_request_priority", + (("gpt", {"priority": 1}), {}), + ), +] + + +@pytest.mark.parametrize("call, core, expected", FORWARDS, ids=[f[1] for f in FORWARDS]) +def test_entry_point_forwards_to_its_core_member(router, monkeypatch, call, core, expected): + from unittest.mock import MagicMock + + mock = MagicMock(return_value=_SENTINEL) + monkeypatch.setattr(router, core, mock) + assert call(router) is _SENTINEL + if expected is not None: + assert mock.call_args == expected + else: + mock.assert_called_once() + + +ASYNC_FORWARDS = [ + (lambda r: r.health().fully_unhealthy_model_names(), "_core_async_get_fully_unhealthy_model_names", ((), {})), + (lambda r: r.health().usage("gpt"), "_core_get_model_group_usage", ((), {"model_group": "gpt"})), + ( + lambda r: r.health().usage("gpt", kind="remaining"), + "_core_get_remaining_model_group_usage", + ((), {"model_group": "gpt"}), + ), +] + + +@pytest.mark.parametrize("call, core, expected", ASYNC_FORWARDS, ids=[f[1] for f in ASYNC_FORWARDS]) +def test_async_entry_point_forwards_to_its_core_member(router, monkeypatch, call, core, expected): + from unittest.mock import AsyncMock + + mock = AsyncMock(return_value=_SENTINEL) + monkeypatch.setattr(router, core, mock) + assert asyncio.run(call(router)) is _SENTINEL + assert mock.call_args == expected + + +def test_registry_reads_without_a_core_forward(router): + registry = router.registry() + assert registry.zero_cost() is getattr(router, "_zero_cost_cache", None) + assert registry.includes(router.registry().deployment("dep-a").model_dump(), team_id=None) is True + + +@pytest.mark.parametrize( + "call, error", + [ + (lambda r: r.registry().deployments(source="named"), TypeError), + (lambda r: r.registry().deployments("gpt", source="nope"), ValueError), + (lambda r: r.registry().index("nope"), ValueError), + (lambda r: r.registry().names(kind="litellm_models"), TypeError), + (lambda r: r.registry().names("gpt", kind="nope"), ValueError), + (lambda r: r.registry().resolve("team-gpt", kind="team_model"), TypeError), + (lambda r: r.registry().resolve(None, kind="alias"), TypeError), + (lambda r: r.registry().resolve("gpt", kind="nope"), ValueError), + (lambda r: r.registry().group("gpt", field="nope"), ValueError), + (lambda r: r.registry().credentials("dep-a", form="nope"), ValueError), + (lambda r: r.registry().plugins("nope"), ValueError), + (lambda r: asyncio.run(r.health().usage("gpt", kind="nope")), ValueError), + (lambda r: RouterSettings().as_dict(), AttributeError), + ], +) +def test_entry_points_reject_unknown_kinds_and_missing_names(router, call, error): + with pytest.raises(error): + call(router) + + +def test_health_is_blocked_and_the_old_static_name_agree(): + paused = Deployment( + model_name="m", + litellm_params={"model": "openai/gpt-4o-mini"}, # pyright: ignore[reportArgumentType] + model_info={"id": "d1", "blocked": True}, # pyright: ignore[reportArgumentType] + ) + from litellm.router_api import RouterHealth + + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + assert Router.is_deployment_blocked(paused) is RouterHealth.is_blocked(paused) + + +def test_old_members_refuse_objects_that_are_not_routers(): + from litellm.router_deprecated import _as_router + + with pytest.raises(TypeError, match=r"is not a litellm\.Router"): + _as_router(object()) # pyright: ignore[reportArgumentType] + + +def test_direct_call_of_an_async_subclass_override_runs_it_once(): + calls = [] + + class Sub(litellm.Router): + async def async_pre_routing_hook( + self, model, request_kwargs, messages=None, input=None, specific_deployment=False + ): + calls.append(model) + return await super().async_pre_routing_hook(model, request_kwargs, messages, input, specific_deployment) + + r = Sub(model_list=[{"model_name": "m", "litellm_params": {"model": "openai/gpt-4o-mini", "api_key": "sk-x"}}]) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + asyncio.run(r.async_pre_routing_hook("m", {})) + assert calls == ["m"] + + +def test_routing_context_forwards_to_the_router(router, monkeypatch): + from unittest.mock import AsyncMock, MagicMock + + from litellm.router_api import RoutingContext + + ctx = RoutingContext(router) + assert ctx.registry().version == router.registry().version + assert ctx.settings() is router.settings() + assert isinstance(ctx.health(), type(router.health())) + for name in ( + "_pre_call_checks", + "_count_pre_call_check_tokens", + "_update_kwargs_before_fallbacks", + "_core_arm_routing_read_prefetch", + "_core_log_retry", + ): + monkeypatch.setattr(router, name, MagicMock(return_value=_SENTINEL)) + monkeypatch.setattr(router, "_core_async_pre_routing_hook", AsyncMock(return_value=_SENTINEL)) + assert ctx.pre_call_checks("gpt", []) is _SENTINEL + assert ctx.count_pre_call_check_tokens(None, "hi") is _SENTINEL + assert ctx.update_kwargs_before_fallbacks("gpt", {}) is _SENTINEL + assert ctx.arm_read_prefetch("gpt") is _SENTINEL + assert asyncio.run(ctx.pre_routing_hook("gpt", {})) is _SENTINEL + assert ctx.log_retry({}, ValueError("x")) is _SENTINEL + + +def test_attribute_errors_about_other_objects_pass_through_unchanged(router): + from litellm.router_deprecated import _public_attribute_error + + other = AttributeError("'dict' object has no attribute 'x'", name="x", obj={}) + assert _public_attribute_error(router, "_core_x", "x", other) is other diff --git a/tests/unit/test_router/test_router_helper_utils.py b/tests/unit/test_router/test_router_helper_utils.py index 513136ae150..5f1b6032ca8 100644 --- a/tests/unit/test_router/test_router_helper_utils.py +++ b/tests/unit/test_router/test_router_helper_utils.py @@ -1158,7 +1158,7 @@ async def test_set_response_headers_passes_through_post_increment_counters(model _hidden_params: dict = {} router = Router(model_list=model_list) - router.get_remaining_model_group_usage = AsyncMock( + router._core_get_remaining_model_group_usage = AsyncMock( return_value={ "x-ratelimit-remaining-tokens": 958, "x-ratelimit-limit-tokens": 1000, @@ -1539,7 +1539,7 @@ async def test_get_remaining_model_group_usage_merges_io_and_tpm_headers(model_l ) ) router.get_model_group_io_token_usage = AsyncMock(return_value=(100, 40)) - router.get_model_group_usage = AsyncMock(return_value=(500, 5)) + router._core_get_model_group_usage = AsyncMock(return_value=(500, 5)) headers = await router.get_remaining_model_group_usage("gpt-3.5-turbo") @@ -1566,7 +1566,7 @@ async def test_set_response_headers_native_input_token_header_does_not_suppress_ _hidden_params: dict = {} router = Router(model_list=model_list) - router.get_remaining_model_group_usage = AsyncMock( + router._core_get_remaining_model_group_usage = AsyncMock( return_value={ "x-ratelimit-remaining-tokens": 1000, "x-ratelimit-remaining-requests": 100, @@ -1596,7 +1596,7 @@ async def test_set_response_headers_native_token_header_does_not_suppress_io_hea _hidden_params: dict = {} router = Router(model_list=model_list) - router.get_remaining_model_group_usage = AsyncMock( + router._core_get_remaining_model_group_usage = AsyncMock( return_value={ "x-ratelimit-remaining-tokens": 1000, "x-ratelimit-remaining-requests": 100, @@ -1628,7 +1628,7 @@ async def test_set_response_headers_handles_missing_usage(model_list): _hidden_params: dict = {} router = Router(model_list=model_list) - router.get_remaining_model_group_usage = AsyncMock( + router._core_get_remaining_model_group_usage = AsyncMock( return_value={ "x-ratelimit-remaining-tokens": 1000, "x-ratelimit-remaining-requests": 100, @@ -1648,7 +1648,7 @@ async def test_set_response_headers_handles_missing_usage(model_list): async def test_set_response_headers_dict_anthropic_messages_response(model_list): """Anthropic /v1/messages returns a dict; IO rate-limit headers must attach.""" router = Router(model_list=model_list) - router.get_remaining_model_group_usage = AsyncMock( + router._core_get_remaining_model_group_usage = AsyncMock( return_value={ "x-ratelimit-limit-input-tokens": 25, "x-ratelimit-remaining-input-tokens": 20, @@ -1688,7 +1688,7 @@ async def test_set_response_headers_wraps_bare_async_generator(model_list): yield {"type": "message_stop"} router = Router(model_list=model_list) - router.get_remaining_model_group_usage = AsyncMock( + router._core_get_remaining_model_group_usage = AsyncMock( return_value={ "x-ratelimit-limit-input-tokens": 25, "x-ratelimit-remaining-input-tokens": 20, diff --git a/tests/unit/test_router_model_cost_isolation.py b/tests/unit/test_router_model_cost_isolation.py index 20b0055f729..91f0b2339c7 100644 --- a/tests/unit/test_router_model_cost_isolation.py +++ b/tests/unit/test_router_model_cost_isolation.py @@ -614,6 +614,63 @@ def test_capability_flag_stays_on_the_deployment_that_set_it(flag: str) -> None: _restore_model_cost_entries(model_keys) +def test_pdf_opt_out_on_one_deployment_does_not_block_its_sibling(respx_mock: respx.MockRouter) -> None: + api_base: Final = "http://localhost:12361/v1" + backend_model: Final = "openai/qa-shared-pdf-backend" + pdf_messages: Final = [ + { + "role": "user", + "content": [ + {"type": "text", "text": "Summarize this."}, + { + "type": "file", + "file": {"filename": "report.pdf", "file_data": "data:application/pdf;base64,JVBERi0xLjQK"}, + }, + ], + } + ] + route: Final = respx_mock.post(url__regex=rf"{api_base}/chat/completions.*").mock( + return_value=httpx.Response( + status_code=200, + json={ + "id": "chatcmpl-pdf", + "object": "chat.completion", + "created": 1712697600, + "model": "qa-shared-pdf-backend", + "choices": [{"index": 0, "message": {"role": "assistant", "content": "ok"}, "finish_reason": "stop"}], + "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2}, + }, + ) + ) + model_keys: Final = { + key: litellm.model_cost.get(key) for key in ("qa-pdf-marked", "qa-pdf-unmarked", backend_model) + } + try: + router: Final = Router( + model_list=[ + { + "model_name": "pdf-blocked", + "litellm_params": {"model": backend_model, "api_base": api_base, "api_key": "fake-key"}, + "model_info": {"id": "qa-pdf-marked", "supports_pdf_input": False}, + }, + { + "model_name": "pdf-allowed", + "litellm_params": {"model": backend_model, "api_base": api_base, "api_key": "fake-key"}, + "model_info": {"id": "qa-pdf-unmarked"}, + }, + ], + ) + + with pytest.raises(litellm.BadRequestError, match="does not accept PDF input"): + router.completion(model="pdf-blocked", messages=pdf_messages) + router.completion(model="pdf-allowed", messages=pdf_messages) + + assert route.call_count == 1 + assert json.loads(route.calls[0].request.content)["messages"] == pdf_messages + finally: + _restore_model_cost_entries(model_keys) + + def test_token_count_settings_stay_on_the_deployment_that_set_them() -> None: """A deployment's ``token_count_model`` and ``token_count_framing`` must not land on the shared ``{provider}/{model}`` key, or a sibling deployment of that model would count with them.""" diff --git a/tests/unit/test_router_silent_experiment.py b/tests/unit/test_router_silent_experiment.py index fd648ceb5d2..b696a61aa3c 100644 --- a/tests/unit/test_router_silent_experiment.py +++ b/tests/unit/test_router_silent_experiment.py @@ -1,9 +1,11 @@ import asyncio +import datetime import json +import threading import time -from collections.abc import Callable, Mapping +from collections.abc import Callable, Generator, Mapping from types import SimpleNamespace -from typing import Final +from typing import Final, Literal from unittest.mock import AsyncMock, MagicMock, patch import httpx @@ -22,9 +24,48 @@ class _RecordingLogger(CustomLogger): def __init__(self) -> None: super().__init__() self.success_kwargs: list[dict[str, object]] = [] + self.failure_kwargs: list[dict[str, object]] = [] + self._logged: Final = threading.Condition() - async def async_log_success_event(self, kwargs, response_obj, start_time, end_time): - self.success_kwargs.append(kwargs) + def _record(self, outcome: Literal["success", "failure"], kwargs: dict[str, object]) -> None: + with self._logged: + match outcome: + case "success": + self.success_kwargs.append(kwargs) + case "failure": + self.failure_kwargs.append(kwargs) + self._logged.notify_all() + + async def async_log_success_event( + self, + kwargs: dict[str, object], + response_obj: object, + start_time: datetime.datetime, + end_time: datetime.datetime, + ) -> None: + self._record("success", kwargs) + + async def async_log_failure_event( + self, + kwargs: dict[str, object], + response_obj: object, + start_time: datetime.datetime, + end_time: datetime.datetime, + ) -> None: + self._record("failure", kwargs) + + def wait_until(self, logged_enough: Callable[[], bool], timeout: float = 30.0) -> None: + with self._logged: + self._logged.wait_for(logged_enough, timeout=timeout) + + def judge_flags(self) -> dict[str, object]: + return { + call["litellm_params"]["metadata"]["judge_of"]: call["standard_logging_object"]["metadata"].get( + "is_silent_experiment" + ) + for call in self.success_kwargs + if "judge_of" in call["litellm_params"]["metadata"] + } def shadow_successes(self) -> list[dict[str, object]]: return [ @@ -34,6 +75,17 @@ class _RecordingLogger(CustomLogger): ] +class _SyncAndAsyncRecordingLogger(_RecordingLogger): + def log_success_event( + self, + kwargs: dict[str, object], + response_obj: object, + start_time: datetime.datetime, + end_time: datetime.datetime, + ) -> None: + self._record("success", kwargs) + + async def _settle_shared_logging_worker() -> None: try: await GLOBAL_LOGGING_WORKER.flush() @@ -41,15 +93,46 @@ async def _settle_shared_logging_worker() -> None: await GLOBAL_LOGGING_WORKER.stop() -@pytest.fixture -def recording_logger(): +class _JudgingLogger(_RecordingLogger): + async def _judge(self, kwargs: Mapping[str, object]) -> None: + metadata: Final = kwargs["litellm_params"]["metadata"] + if "judge_of" in metadata: + return + await litellm.acompletion( + model="openai/gpt-5.4-nano", + api_key="fake-key", + messages=[{"role": "user", "content": "score it"}], + mock_response="ok", + metadata={"judge_of": metadata["model_group"]}, + ) + + async def async_log_success_event( + self, + kwargs: dict[str, object], + response_obj: object, + start_time: datetime.datetime, + end_time: datetime.datetime, + ) -> None: + await super().async_log_success_event(kwargs, response_obj, start_time, end_time) + await self._judge(kwargs) + + async def async_log_failure_event( + self, + kwargs: dict[str, object], + response_obj: object, + start_time: datetime.datetime, + end_time: datetime.datetime, + ) -> None: + await self._judge(kwargs) + + +def _installed(logger: _RecordingLogger) -> Generator[_RecordingLogger]: settle_loop: Final = asyncio.new_event_loop() try: settle_loop.run_until_complete(_settle_shared_logging_worker()) finally: settle_loop.close() original_callbacks: Final = litellm.callbacks - logger: Final = _RecordingLogger() litellm.callbacks = [logger] try: yield logger @@ -57,22 +140,44 @@ def recording_logger(): litellm.callbacks = original_callbacks -async def _wait_for_shadow_successes(logger: _RecordingLogger, expected: int, timeout: float = 5.0) -> None: - deadline: Final = time.monotonic() + timeout - while len(logger.shadow_successes()) < expected and time.monotonic() < deadline: - await asyncio.sleep(0.05) +@pytest.fixture +def recording_logger(): + yield from _installed(_RecordingLogger()) -async def _wait_for_all_successes(logger: _RecordingLogger, expected: int, timeout: float = 5.0) -> None: - deadline: Final = time.monotonic() + timeout - while len(logger.success_kwargs) < expected and time.monotonic() < deadline: - await asyncio.sleep(0.05) +@pytest.fixture +def sync_and_async_recording_logger(): + yield from _installed(_SyncAndAsyncRecordingLogger()) -def _wait_for_shadow_successes_sync(logger: _RecordingLogger, expected: int, timeout: float = 5.0) -> None: - deadline: Final = time.monotonic() + timeout - while len(logger.shadow_successes()) < expected and time.monotonic() < deadline: - time.sleep(0.05) +@pytest.fixture +def judging_logger(): + yield from _installed(_JudgingLogger()) + + +async def _wait_for_shadow_successes(logger: _RecordingLogger, expected: int, timeout: float = 30.0) -> None: + await asyncio.to_thread(logger.wait_until, lambda: len(logger.shadow_successes()) >= expected, timeout) + + +async def _wait_for_all_successes(logger: _RecordingLogger, expected: int) -> None: + await asyncio.to_thread(logger.wait_until, lambda: len(logger.success_kwargs) >= expected) + + +async def _wait_for_judges(logger: _RecordingLogger, judged: set[str]) -> None: + await asyncio.to_thread(logger.wait_until, lambda: judged <= logger.judge_flags().keys()) + + +def _silent_flag_by_group(logger: _RecordingLogger) -> dict[str, object]: + return { + call["standard_logging_object"]["model_group"]: call["standard_logging_object"]["metadata"].get( + "is_silent_experiment" + ) + for call in logger.success_kwargs + } + + +def _wait_for_shadow_successes_sync(logger: _RecordingLogger, expected: int) -> None: + logger.wait_until(lambda: len(logger.shadow_successes()) >= expected) def _streaming_model_list(silent_model: object) -> list[dict[str, object]]: @@ -263,6 +368,197 @@ async def test_multiple_shadow_targets_fan_out_async(recording_logger): assert primary_successes[0]["litellm_params"]["metadata"]["model_group"] == "primary-model" +@pytest.mark.asyncio +async def test_standard_logging_payload_marks_only_the_mirrored_calls_as_silent_experiments( + recording_logger: _RecordingLogger, +) -> None: + router: Final = Router(model_list=_streaming_model_list(["shadow-a", "shadow-b"])) + response: Final = await router.acompletion( + model="primary-model", + messages=[{"role": "user", "content": "hi"}], + mock_response="pong", + ) + assert response.choices[0].message.content == "pong" + await _wait_for_all_successes(recording_logger, expected=3) + + payloads: Final = [call["standard_logging_object"] for call in recording_logger.success_kwargs] + assert sorted(payload["model_group"] for payload in payloads) == ["primary-model", "shadow-a", "shadow-b"] + metadata_by_group: Final = {payload["model_group"]: payload["metadata"] for payload in payloads} + assert metadata_by_group["shadow-a"]["is_silent_experiment"] is True + assert metadata_by_group["shadow-b"]["is_silent_experiment"] is True + assert "is_silent_experiment" not in metadata_by_group["primary-model"] + + +def test_standard_logging_payload_marks_only_the_mirrored_calls_on_the_sync_path( + sync_and_async_recording_logger: _SyncAndAsyncRecordingLogger, +) -> None: + router: Final = Router(model_list=_streaming_model_list(["shadow-a", "shadow-b"])) + response: Final = router.completion( + model="primary-model", + messages=[{"role": "user", "content": "hi"}], + mock_response="pong", + ) + assert response.choices[0].message.content == "pong" + sync_and_async_recording_logger.wait_until( + lambda: ( + _silent_flag_by_group(sync_and_async_recording_logger).keys() >= {"primary-model", "shadow-a", "shadow-b"} + ) + ) + + assert _silent_flag_by_group(sync_and_async_recording_logger) == { + "primary-model": None, + "shadow-a": True, + "shadow-b": True, + } + + +@pytest.mark.asyncio +async def test_a_failed_mirrored_call_reaches_failure_callbacks_marked_as_mirrored( + recording_logger: _RecordingLogger, +) -> None: + router: Final = Router( + model_list=[ + { + "model_name": "primary-model", + "litellm_params": { + "model": "openai/gpt-5.4-mini", + "api_key": "fake-key", + "silent_model": "shadow-a", + "mock_response": "pong", + }, + }, + { + "model_name": "shadow-a", + "litellm_params": { + "model": "openai/gpt-5.4-nano", + "api_key": "fake-key", + "mock_response": "litellm.InternalServerError", + }, + }, + ] + ) + await router.acompletion(model="primary-model", messages=[{"role": "user", "content": "hi"}]) + await asyncio.to_thread(recording_logger.wait_until, lambda: len(recording_logger.failure_kwargs) >= 1) + + assert { + ( + call["standard_logging_object"]["model_group"], + call["standard_logging_object"]["metadata"].get("is_silent_experiment"), + ) + for call in recording_logger.failure_kwargs + } == {("shadow-a", True)} + + +_SPEND_ROW_TIME: Final = datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc) + + +def _spend_row_is_silent_experiment(call: Mapping[str, object]) -> object: + from litellm.proxy.spend_tracking.spend_tracking_utils import get_logging_payload + + row: Final = get_logging_payload( + kwargs=dict(call), + response_obj=litellm.ModelResponse(id="chatcmpl-test", choices=[]), + start_time=_SPEND_ROW_TIME, + end_time=_SPEND_ROW_TIME, + ) + return json.loads(row["metadata"]).get("is_silent_experiment") + + +def _through_the_spend_collector(call: Mapping[str, object]) -> Mapping[str, object]: + from litellm.proxy.spend_tracking.spend_event import ( + SpendEvent, + build_spend_event, + decode_spend_event, + spend_event_callback_args, + ) + + line: Final = build_spend_event( + call, + litellm.ModelResponse(id="chatcmpl-test", choices=[]), + _SPEND_ROW_TIME, + _SPEND_ROW_TIME, + store_bodies=False, + ) + assert isinstance(line, bytes) + event: Final = decode_spend_event(line) + assert isinstance(event, SpendEvent) + return spend_event_callback_args(event).kwargs + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "spend_kwargs", + [pytest.param(lambda call: call, id="in_process"), pytest.param(_through_the_spend_collector, id="collector")], +) +async def test_spend_rows_mark_only_the_mirrored_calls_when_the_caller_sends_litellm_metadata( + recording_logger: _RecordingLogger, + spend_kwargs: Callable[[Mapping[str, object]], Mapping[str, object]], +) -> None: + router: Final = Router(model_list=_streaming_model_list(["shadow-a", "shadow-b"])) + await router.acompletion( + model="primary-model", + messages=[{"role": "user", "content": "hi"}], + mock_response="pong", + litellm_metadata={"user_api_key": "test-key"}, + ) + await _wait_for_all_successes(recording_logger, expected=3) + + silent_by_group: Final = { + call["standard_logging_object"]["model_group"]: _spend_row_is_silent_experiment(spend_kwargs(call)) + for call in recording_logger.success_kwargs + } + assert silent_by_group == {"primary-model": None, "shadow-a": True, "shadow-b": True} + + +@pytest.mark.asyncio +async def test_router_counts_a_mirrored_call_once_against_its_target_rate_limits( + recording_logger: _RecordingLogger, +) -> None: + router: Final = Router( + model_list=[ + { + "model_name": "primary-model", + "litellm_params": {"model": "openai/gpt-5.4-mini", "api_key": "fake-key", "silent_model": "shadow-b"}, + }, + { + "model_name": "shadow-b", + "litellm_params": {"model": "openai/gpt-5.4-nano", "api_key": "fake-key", "rpm": 100, "tpm": 100000}, + }, + ] + ) + await router.acompletion(model="primary-model", messages=[{"role": "user", "content": "hi"}], mock_response="pong") + await _wait_for_shadow_successes(recording_logger, expected=1) + await GLOBAL_LOGGING_WORKER.flush() + + shadow_tokens: Final = recording_logger.shadow_successes()[0]["standard_logging_object"]["total_tokens"] + assert await router.get_model_group_usage("shadow-b") == (shadow_tokens, 1) + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + ("bucket", "planted"), + [ + ("metadata", {"is_silent_experiment": True}), + ("litellm_metadata", {"is_silent_experiment": True}), + ], +) +async def test_a_caller_sent_silent_experiment_flag_does_not_mark_a_real_call_as_mirrored( + recording_logger: _RecordingLogger, bucket: str, planted: dict[str, object] +) -> None: + router: Final = Router(model_list=_streaming_model_list(["shadow-a", "shadow-b"])) + await router.acompletion( + model="shadow-b", + messages=[{"role": "user", "content": "hi"}], + mock_response="pong", + **{bucket: planted}, + ) + await _wait_for_all_successes(recording_logger, expected=1) + + call: Final = recording_logger.success_kwargs[0] + assert "is_silent_experiment" not in call["standard_logging_object"]["metadata"] + assert _spend_row_is_silent_experiment(call) is None + + def test_multiple_shadow_targets_fan_out_sync(recording_logger): router = Router(model_list=_streaming_model_list(["shadow-a", "shadow-b"])) response = router.completion( @@ -674,6 +970,8 @@ async def test_aembedding_with_silent_model_sends_provider_body_without_it( request_body: Final = json.loads(route.calls.last.request.read()) assert request_body == {"model": "embed-model", "input": ["black dresses"], "input_type": "query"} assert response.data[0]["embedding"] == [0.1, 0.2] + + @pytest.mark.parametrize("run_silent_experiment", SILENT_EXPERIMENT_RUNNERS) def test_silent_experiment_does_not_launch_from_a_shadow_request(run_silent_experiment): router = Router(model_list=_streaming_model_list(["shadow-a"])) @@ -686,3 +984,63 @@ def test_silent_experiment_does_not_launch_from_a_shadow_request(run_silent_expe ) acompletion.assert_not_awaited() + + +def _judged_router(shadow_outcome: str) -> Router: + return Router( + model_list=[ + { + "model_name": "primary-model", + "litellm_params": { + "model": "openai/gpt-5.4-mini", + "api_key": "fake-key", + "silent_model": "shadow-a", + "mock_response": "pong", + }, + }, + { + "model_name": "shadow-a", + "litellm_params": { + "model": "openai/gpt-5.4-nano", + "api_key": "fake-key", + "mock_response": shadow_outcome, + }, + }, + ] + ) + + +@pytest.mark.asyncio +@pytest.mark.parametrize("shadow_outcome", ["pong", "litellm.InternalServerError"]) +async def test_calls_a_callback_starts_while_logging_a_mirrored_call_are_not_marked_as_mirrored( + judging_logger: _JudgingLogger, shadow_outcome: str +) -> None: + router: Final = _judged_router(shadow_outcome) + await router.acompletion(model="primary-model", messages=[{"role": "user", "content": "hi"}]) + await _wait_for_judges(judging_logger, {"primary-model", "shadow-a"}) + + assert judging_logger.judge_flags() == {"primary-model": None, "shadow-a": None} + + +def _start_judge_call( + kwargs: Mapping[str, object], completion_response: object, start_time: object, end_time: object +) -> None: + litellm.completion( + model="openai/gpt-5.4-nano", + api_key="fake-key", + messages=[{"role": "user", "content": "score it"}], + mock_response="ok", + metadata={"judge_of": kwargs["litellm_params"]["metadata"]["model_group"]}, + ) + + +@pytest.mark.asyncio +async def test_calls_a_failure_function_callback_starts_for_a_mirrored_call_are_not_marked_as_mirrored( + sync_and_async_recording_logger: _SyncAndAsyncRecordingLogger, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setattr(litellm, "failure_callback", [_start_judge_call]) + router: Final = _judged_router("litellm.InternalServerError") + await router.acompletion(model="primary-model", messages=[{"role": "user", "content": "hi"}]) + await _wait_for_judges(sync_and_async_recording_logger, {"shadow-a"}) + + assert sync_and_async_recording_logger.judge_flags() == {"shadow-a": None} diff --git a/tests/unit/test_stream_chunk_builder_annotations.py b/tests/unit/test_stream_chunk_builder_annotations.py index 9c7ad4126b0..c710697b39c 100644 --- a/tests/unit/test_stream_chunk_builder_annotations.py +++ b/tests/unit/test_stream_chunk_builder_annotations.py @@ -6,6 +6,8 @@ annotation chunk, losing any annotations that arrived in later chunks. This fix merges annotations from ALL chunks. """ +from typing import Final + from litellm import stream_chunk_builder from litellm.types.utils import Delta, ModelResponseStream, StreamingChoices @@ -189,3 +191,46 @@ def test_stream_chunk_builder_no_annotations(): message = response["choices"][0]["message"] assert not hasattr(message, "annotations") or message.annotations is None + + +def test_stream_chunk_builder_moves_typed_provider_annotations_like_non_streaming() -> None: + url_citation: Final = { + "type": "url_citation", + "url_citation": {"url": "https://example.com", "title": "Example", "start_index": 0, "end_index": 5}, + } + file_annotation: Final = {"type": "file", "file": {"hash": "abc123", "name": "report.pdf"}} + chunks: Final = [ + ModelResponseStream( + id="chatcmpl-test", + created=1700000000, + model="test-model", + object="chat.completion.chunk", + choices=[ + StreamingChoices( + finish_reason=None, + index=0, + delta=Delta(content="Hello", role="assistant", provider_specific_fields={"region": "eu"}), + ) + ], + ), + ModelResponseStream( + id="chatcmpl-test", + created=1700000000, + model="test-model", + object="chat.completion.chunk", + choices=[ + StreamingChoices( + finish_reason="stop", + index=0, + delta=Delta(content=None, annotations=[url_citation, file_annotation]), + ) + ], + ), + ] + + response: Final = stream_chunk_builder(chunks=chunks) + assert response is not None + + message: Final = response["choices"][0]["message"] + assert message.annotations == [url_citation] + assert message.provider_specific_fields == {"region": "eu", "annotations": [file_annotation]} diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 2870016a5e9..f3b470044ae 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -5,14 +5,18 @@ import copy import contextlib import contextvars import io +import itertools import json import logging import os import queue import threading +import time +import uuid from collections.abc import Callable, Iterator, Mapping from concurrent.futures import Future, ThreadPoolExecutor from datetime import datetime, timedelta, timezone +from functools import partial from pathlib import Path, PurePath from typing import Final, SupportsIndex, cast from unittest import mock @@ -79,8 +83,10 @@ from litellm.types.utils import ( all_litellm_params, bedrock_batch_litellm_params, ) +from litellm.secret_managers.main import oidc_cache from litellm.types.videos.main import VideoObject from litellm.utils import ( + acredential_updates, _invalidate_model_cost_lowercase_map, add_custom_logger_callback_to_specific_event, check_valid_key, @@ -7320,7 +7326,6 @@ def test_unresolvable_oidc_api_key_fails_closed_naming_the_reason_without_its_ta ("reference", "kind"), [ ("oidc/env_path/MISTRAL_SA_TOKEN_PATH", "oidc/env_path"), - ("oidc/google/https://api.mistral.ai", "oidc/google"), ("oidc/github/mistral", "oidc/github"), ("oidc/env_MISTRAL_SA_TOKEN_PATH", "oidc/"), ("oidc/LIT-UNKNOWN-KIND/MISTRAL_SA_TOKEN_PATH", "oidc/"), @@ -7341,6 +7346,7 @@ def test_oidc_api_key_of_a_kind_api_key_cannot_resolve_fails_closed_as_an_authen assert not route.called assert excinfo.value.status_code == 401 assert f"{kind} secret reference" in excinfo.value.message + assert "oidc/google/" in excinfo.value.message assert reference.removeprefix(kind).lstrip("/") not in excinfo.value.message assert "MISTRAL_SA_TOKEN_PATH" not in excinfo.value.message @@ -7356,7 +7362,7 @@ async def test_router_fails_over_from_a_deployment_whose_oidc_api_key_kind_canno "model_name": "devstral", "litellm_params": { "model": "mistral/devstral-latest", - "api_key": "oidc/google/https://api.mistral.ai", + "api_key": "oidc/github/mistral", "order": 1, }, }, @@ -7395,6 +7401,372 @@ def test_an_oidc_api_key_sent_with_the_call_goes_upstream_as_written( assert sent == f"Bearer {reference}" +_GOOGLE_METADATA_IDENTITY: Final = ( + "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity" +) +_VLLM_BASE: Final = "https://vllm.example.test/v1" +_VLLM_CHAT_URL: Final = f"{_VLLM_BASE}/chat/completions" +_VLLM_MODEL: Final = "hosted_vllm/qwen3-0.6b" +_VLLM_REPLY: Final = {**_MISTRAL_REPLY, "model": "qwen3-0.6b"} +_CLOCK_START: Final = 1_900_000_000.0 +_GOOGLE_TOKEN_LIFETIME: Final = 3600 + + +def _jwt_segment(claims: Mapping[str, object]) -> str: + return base64.urlsafe_b64encode(json.dumps(claims).encode()).decode().rstrip("=") + + +def _google_id_token(audience: str, issued_at: float) -> str: + claims: Final = {"aud": audience, "iat": int(issued_at), "exp": int(issued_at) + _GOOGLE_TOKEN_LIFETIME} + return f"{_jwt_segment({'alg': 'RS256', 'typ': 'JWT'})}.{_jwt_segment(claims)}.signature" + + +def _google_reference() -> tuple[str, str]: + audience: Final = f"https://vllm.example.test/{uuid.uuid4().hex}" + return audience, f"oidc/google/{audience}" + + +def _metadata_identity(fetched_on: queue.SimpleQueue[int], request: httpx.Request) -> httpx.Response: + fetched_on.put(threading.get_ident()) + if request.headers.get("Metadata-Flavor") != "Google": + return httpx.Response(403) + return httpx.Response(200, text=_google_id_token(request.url.params["audience"], time.time())) + + +def _vllm_credential(api_key: str) -> CredentialItem: + return CredentialItem( + credential_name="vllm-sa", credential_values={"api_key": api_key, "api_base": _VLLM_BASE}, credential_info={} + ) + + +async def _ping_vllm_through(call_path: str, credential_name: str) -> None: + if call_path == "async": + await litellm.acompletion(model=_VLLM_MODEL, messages=_PING, litellm_credential_name=credential_name) + return + litellm.completion(model=_VLLM_MODEL, messages=_PING, litellm_credential_name=credential_name) + + +@pytest.mark.asyncio +@pytest.mark.parametrize("call_path", ["sync", "async"]) +async def test_named_credential_oidc_google_api_key_sends_the_metadata_token_and_refreshes_it_before_it_expires( + monkeypatch: pytest.MonkeyPatch, call_path: str +) -> None: + monkeypatch.setattr(litellm, "disable_aiohttp_transport", True) + monkeypatch.setattr(time, "time", lambda: _CLOCK_START) + audience, reference = _google_reference() + monkeypatch.setattr(litellm, "credential_list", [_vllm_credential(reference)]) + refreshed_at: Final = _CLOCK_START + _GOOGLE_TOKEN_LIFETIME - 59 + + with respx.mock(assert_all_called=True) as upstream: + metadata: Final = upstream.get(_GOOGLE_METADATA_IDENTITY).mock( + side_effect=partial(_metadata_identity, queue.SimpleQueue()) + ) + chat: Final = upstream.post(_VLLM_CHAT_URL).respond(200, json=_VLLM_REPLY) + await _ping_vllm_through(call_path, "vllm-sa") + await _ping_vllm_through(call_path, "vllm-sa") + fetches_within_lifetime: Final = metadata.call_count + monkeypatch.setattr(time, "time", lambda: refreshed_at) + await _ping_vllm_through(call_path, "vllm-sa") + sent: Final = [call.request.headers["Authorization"] for call in chat.calls] + audiences: Final = [call.request.url.params["audience"] for call in metadata.calls] + + first_token: Final = f"Bearer {_google_id_token(audience, _CLOCK_START)}" + assert sent == [first_token, first_token, f"Bearer {_google_id_token(audience, refreshed_at)}"] + assert (fetches_within_lifetime, audiences) == (1, [audience, audience]) + assert litellm.credential_list[0].credential_values["api_key"] == reference + + +@pytest.mark.asyncio +async def test_router_fetches_an_oidc_google_api_key_token_off_the_event_loop_and_keeps_the_reference( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setattr(litellm, "disable_aiohttp_transport", True) + monkeypatch.setattr(time, "time", lambda: _CLOCK_START) + audience, reference = _google_reference() + fetched_on: Final[queue.SimpleQueue[int]] = queue.SimpleQueue() + router: Final = litellm.Router( + model_list=[ + { + "model_name": "vllm", + "litellm_params": {"model": _VLLM_MODEL, "api_base": _VLLM_BASE, "api_key": reference}, + } + ] + ) + + try: + with respx.mock(assert_all_called=True) as upstream: + upstream.get(_GOOGLE_METADATA_IDENTITY).mock(side_effect=partial(_metadata_identity, fetched_on)) + chat: Final = upstream.post(_VLLM_CHAT_URL).respond(200, json=_VLLM_REPLY) + await router.acompletion(model="vllm", messages=_PING) + sent: Final = chat.calls.last.request.headers["Authorization"] + finally: + router.discard() + + assert sent == f"Bearer {_google_id_token(audience, _CLOCK_START)}" + assert fetched_on.qsize() == 1 + assert fetched_on.get_nowait() != threading.get_ident() + assert router.get_model_list(model_name="vllm")[0]["litellm_params"]["api_key"] == reference + + +@pytest.mark.asyncio +@pytest.mark.parametrize("call_path", ["sync", "async"]) +@pytest.mark.parametrize( + ("metadata_failure", "status_code"), + [ + (lambda: httpx.Response(500, text="metadata server unavailable"), 503), + (lambda: httpx.Response(403, text="Missing Metadata-Flavor:Google header"), 401), + (lambda: httpx.Response(200, text=""), 401), + (lambda: httpx.ConnectError("[Errno -2] Name or service not known"), 503), + (lambda: httpx.ReadTimeout("timed out"), 503), + (lambda: httpx.RemoteProtocolError("Server disconnected without sending a response."), 503), + ], + ids=["server error", "refused", "empty token", "not on GCP", "timeout", "disconnected"], +) +async def test_oidc_google_api_key_fails_closed_when_the_metadata_server_fails_and_recovers_on_the_next_call( + monkeypatch: pytest.MonkeyPatch, + caplog: pytest.LogCaptureFixture, + call_path: str, + metadata_failure: Callable[[], httpx.Response | httpx.HTTPError], + status_code: int, +) -> None: + monkeypatch.setattr(litellm, "disable_aiohttp_transport", True) + monkeypatch.setattr(time, "time", lambda: _CLOCK_START) + audience, reference = _google_reference() + monkeypatch.setattr(litellm, "credential_list", [_vllm_credential(reference)]) + recovered_token: Final = _google_id_token(audience, _CLOCK_START) + + with respx.mock(assert_all_called=True) as upstream: + upstream.get(_GOOGLE_METADATA_IDENTITY).mock( + side_effect=[metadata_failure(), httpx.Response(200, text=recovered_token)] + ) + chat: Final = upstream.post(_VLLM_CHAT_URL).respond(200, json=_VLLM_REPLY) + with caplog.at_level(logging.WARNING, logger="LiteLLM"), pytest.raises( + (litellm.AuthenticationError, litellm.ServiceUnavailableError) + ) as excinfo: + await _ping_vllm_through(call_path, "vllm-sa") + sent_while_failing: Final = chat.call_count + await _ping_vllm_through(call_path, "vllm-sa") + sent_after_recovery: Final = [call.request.headers["Authorization"] for call in chat.calls] + + assert excinfo.value.status_code == status_code + assert "api_key oidc/google secret reference could not be resolved" in excinfo.value.message + assert audience.removeprefix("https://") not in excinfo.value.message + assert reference in caplog.text + assert sent_while_failing == 0 + assert sent_after_recovery == [f"Bearer {recovered_token}"] + + +@pytest.mark.asyncio +async def test_an_oidc_google_api_key_token_cached_when_the_call_starts_is_sent_though_it_expires_mid_call( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setattr(time, "time", lambda: _CLOCK_START) + audience, reference = _google_reference() + cached_token: Final = _google_id_token(audience, _CLOCK_START) + oidc_cache.set_cache(key=reference, value=cached_token, ttl=60) + clock: Final = itertools.chain([_CLOCK_START + 60], itertools.repeat(_CLOCK_START + 61)) + monkeypatch.setattr(time, "time", lambda: next(clock)) + monkeypatch.setattr(litellm, "credential_list", [_vllm_credential(reference)]) + fetched_on: Final[queue.SimpleQueue[int]] = queue.SimpleQueue() + + with respx.mock(assert_all_called=False) as upstream: + upstream.get(_GOOGLE_METADATA_IDENTITY).mock(side_effect=partial(_metadata_identity, fetched_on)) + updates: Final = await acredential_updates({"model": _VLLM_MODEL, "litellm_credential_name": "vllm-sa"}) + + assert updates["api_key"] == cached_token + assert fetched_on.empty() + + +@pytest.mark.asyncio +async def test_a_pending_oidc_google_metadata_fetch_leaves_the_default_executor_free( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setattr(litellm, "disable_aiohttp_transport", True) + _, reference = _google_reference() + monkeypatch.setattr(litellm, "credential_list", [_vllm_credential(reference)]) + loop: Final = asyncio.get_running_loop() + loop.set_default_executor(ThreadPoolExecutor(max_workers=1)) + fetch_started: Final[queue.SimpleQueue[int]] = queue.SimpleQueue() + release: Final = threading.Event() + + def pending_metadata(_request: httpx.Request) -> httpx.Response: + fetch_started.put(threading.get_ident()) + release.wait(timeout=30) + return httpx.Response(500, text="metadata server unavailable") + + with ThreadPoolExecutor(max_workers=1) as observer, respx.mock(assert_all_called=False) as upstream: + upstream.get(_GOOGLE_METADATA_IDENTITY).mock(side_effect=pending_metadata) + chat: Final = upstream.post(_VLLM_CHAT_URL).respond(200, json=_VLLM_REPLY) + call: Final = asyncio.ensure_future(_ping_vllm_through("async", "vllm-sa")) + try: + await asyncio.wait_for(loop.run_in_executor(observer, fetch_started.get), timeout=30) + other_work: Final = await asyncio.wait_for(loop.run_in_executor(None, lambda: "done"), timeout=30) + finally: + release.set() + with pytest.raises(litellm.ServiceUnavailableError) as excinfo: + await call + + assert other_work == "done" + assert excinfo.value.status_code == 503 + assert chat.call_count == 0 + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "metadata_answer", + [lambda token: httpx.Response(200, text=token), lambda _token: httpx.Response(500, text="metadata server down")], + ids=["token", "outage"], +) +async def test_concurrent_calls_that_miss_the_oidc_google_token_cache_share_one_metadata_fetch( + monkeypatch: pytest.MonkeyPatch, metadata_answer: Callable[[str], httpx.Response] +) -> None: + monkeypatch.setattr(litellm, "disable_aiohttp_transport", True) + monkeypatch.setattr(time, "time", lambda: _CLOCK_START) + audience, reference = _google_reference() + monkeypatch.setattr(litellm, "credential_list", [_vllm_credential(reference)]) + token: Final = _google_id_token(audience, _CLOCK_START) + fetch_started: Final[queue.SimpleQueue[int]] = queue.SimpleQueue() + release: Final = threading.Event() + + def held_metadata(_request: httpx.Request) -> httpx.Response: + fetch_started.put(threading.get_ident()) + release.wait(timeout=30) + return metadata_answer(token) + + with ThreadPoolExecutor(max_workers=1) as observer, respx.mock(assert_all_called=False) as upstream: + metadata: Final = upstream.get(_GOOGLE_METADATA_IDENTITY).mock(side_effect=held_metadata) + chat: Final = upstream.post(_VLLM_CHAT_URL).respond(200, json=_VLLM_REPLY) + calls: Final = [asyncio.ensure_future(_ping_vllm_through("async", "vllm-sa")) for _ in range(24)] + loop: Final = asyncio.get_running_loop() + try: + await asyncio.wait_for(loop.run_in_executor(observer, fetch_started.get), timeout=30) + fetch_started_while_held: Final = await loop.run_in_executor( + observer, partial(_next_fetch_within, fetch_started, 0.5) + ) + finally: + release.set() + outcomes: Final = await asyncio.gather(*calls, return_exceptions=True) + sent: Final = [call.request.headers["Authorization"] for call in chat.calls] + + assert fetch_started_while_held is None + if metadata.calls[0].response.status_code == 200: + assert metadata.call_count == 1 + assert outcomes == [None] * 24 + assert sent == [f"Bearer {token}"] * 24 + else: + assert [getattr(outcome, "status_code", None) for outcome in outcomes] == [503] * 24 + assert sent == [] + + +def _next_fetch_within(fetches: queue.SimpleQueue[int], seconds: float) -> int | None: + try: + return fetches.get(timeout=seconds) + except queue.Empty: + return None + + +@pytest.mark.asyncio +async def test_a_call_an_oidc_google_api_key_may_not_go_to_is_refused_without_a_metadata_fetch( + monkeypatch: pytest.MonkeyPatch, +) -> None: + _, reference = _google_reference() + monkeypatch.setattr(litellm, "credential_list", [_vllm_credential(reference)]) + + with respx.mock(assert_all_called=False) as upstream: + metadata: Final = upstream.get(_GOOGLE_METADATA_IDENTITY).respond(500) + with pytest.raises(litellm.AuthenticationError) as excinfo: + await acredential_updates( + { + "model": _VLLM_MODEL, + "litellm_credential_name": "vllm-sa", + "api_base": "https://elsewhere.example.test/v1", + } + ) + + assert "only to that credential's own api_base" in excinfo.value.message + assert metadata.call_count == 0 + + +async def _pass_through_chat_call(call_path: str, reference: str) -> None: + pass_through: Final = {"method": "POST", "endpoint": "v1/chat/completions", "json": {"messages": _PING}} + if call_path == "sync": + litellm.llm_passthrough_route(model=_VLLM_MODEL, litellm_credential_name="vllm-sa", **pass_through) + return + router: Final = litellm.Router( + model_list=[ + { + "model_name": "vllm-model", + "litellm_params": {"model": _VLLM_MODEL, "api_base": _VLLM_BASE, "api_key": reference}, + } + ], + num_retries=0, + ) + await router.allm_passthrough_route(model="vllm-model", **pass_through) + + +@pytest.mark.asyncio +@pytest.mark.parametrize("call_path", ["sync", "async router"]) +async def test_a_pass_through_call_with_an_oidc_google_api_key_is_refused_without_a_metadata_fetch( + monkeypatch: pytest.MonkeyPatch, call_path: str +) -> None: + monkeypatch.setattr(litellm, "disable_aiohttp_transport", True) + monkeypatch.setattr(time, "time", lambda: _CLOCK_START) + audience, reference = _google_reference() + monkeypatch.setattr(litellm, "credential_list", [_vllm_credential(reference)]) + + with respx.mock(assert_all_called=False) as upstream: + metadata: Final = upstream.get(_GOOGLE_METADATA_IDENTITY).respond( + 200, text=_google_id_token(audience, _CLOCK_START) + ) + vllm: Final = upstream.route(host="vllm.example.test").respond(200, json=_VLLM_REPLY) + with pytest.raises(litellm.AuthenticationError) as excinfo: + await _pass_through_chat_call(call_path, reference) + + assert "pass-through routes do not fetch Google ID tokens" in excinfo.value.message + assert metadata.call_count == 0 + assert vllm.call_count == 0 + + +@pytest.mark.parametrize( + ("configured", "bound"), + [(None, 3.0), ("1.5", 1.5), ("three seconds", 3.0), ("0", 3.0), ("-2", 3.0)], + ids=["unset", "configured", "malformed", "zero", "negative"], +) +def test_oidc_google_api_key_metadata_fetch_waits_at_most_gce_metadata_timeout_seconds( + monkeypatch: pytest.MonkeyPatch, configured: str | None, bound: float +) -> None: + monkeypatch.setattr(litellm, "disable_aiohttp_transport", True) + if configured is None: + monkeypatch.delenv("GCE_METADATA_TIMEOUT", raising=False) + else: + monkeypatch.setenv("GCE_METADATA_TIMEOUT", configured) + monkeypatch.setattr(time, "time", lambda: _CLOCK_START) + audience, reference = _google_reference() + monkeypatch.setattr(litellm, "credential_list", [_vllm_credential(reference)]) + token: Final = _google_id_token(audience, _CLOCK_START) + + with respx.mock(assert_all_called=True) as upstream: + metadata: Final = upstream.get(_GOOGLE_METADATA_IDENTITY).respond(200, text=token) + chat: Final = upstream.post(_VLLM_CHAT_URL).respond(200, json=_VLLM_REPLY) + litellm.completion(model=_VLLM_MODEL, messages=_PING, litellm_credential_name="vllm-sa") + sent: Final = chat.calls.last.request.headers["Authorization"] + waits: Final = metadata.calls.last.request.extensions["timeout"] + + assert sent == f"Bearer {token}" + assert waits == {"connect": bound, "read": bound, "write": bound, "pool": bound} + + +def test_an_oidc_google_api_key_sent_with_the_call_goes_upstream_as_written_without_a_metadata_fetch() -> None: + _, reference = _google_reference() + + with respx.mock(assert_all_called=True) as upstream: + chat: Final = upstream.post(_VLLM_CHAT_URL).respond(200, json=_VLLM_REPLY) + litellm.completion(model=_VLLM_MODEL, api_base=_VLLM_BASE, messages=_PING, api_key=reference) + sent: Final = chat.calls.last.request.headers["Authorization"] + + assert sent == f"Bearer {reference}" + + @pytest.mark.asyncio @pytest.mark.parametrize( ("model_info", "expected"), @@ -10922,6 +11294,7 @@ def test_get_optional_params_forwards_a_tool_config_cache_point_to_a_downstream_ assert optional_params["extra_body"] == {"kept": True, "cache_control_injection_points": [point]} + @pytest.mark.parametrize( ("provider", "expected_warnings"), (("streamlake", 1), ("veniceai", 1), ("my-own-llm", 0)), @@ -10940,3 +11313,34 @@ def test_custom_provider_named_like_a_built_in_warns_once( warnings: Final = [r for r in caplog.records if f"provider '{provider}'" in r.getMessage()] assert len(warnings) == expected_warnings assert provider in litellm._custom_providers + + +@pytest.mark.asyncio +@pytest.mark.parametrize("deferred", (False, True)) +async def test_success_enqueue_is_tracked_without_starting_callbacks_synchronously(deferred: bool) -> None: + from litellm.litellm_core_utils.litellm_logging import Logging + from litellm.litellm_core_utils.logging_worker import GLOBAL_LOGGING_WORKER + from litellm.responses.streaming_iterator import drain_logging_tasks + from litellm.utils import _schedule_async_success_logging + + now: Final = datetime(2026, 1, 1, tzinfo=timezone.utc) + logging_obj: Final = Logging( + model="gpt-4o-mini", + messages=[], + stream=False, + call_type="acompletion", + start_time=now, + litellm_call_id="shutdown-enqueue", + function_id="shutdown-enqueue", + ) + logging_obj.defer_async_logging = deferred + _schedule_async_success_logging(logging_obj, ModelResponse(), now, now, False) + if deferred: + assert not logging_obj.async_success_log_enqueued + assert logging_obj.enqueue_deferred_logging is not None + logging_obj.enqueue_deferred_logging() + assert "has_logged_async_success" not in logging_obj.model_call_details + await drain_logging_tasks() + assert GLOBAL_LOGGING_WORKER.pending_count > 0, "Tracked enqueue must finish before worker draining starts" + await GLOBAL_LOGGING_WORKER.flush() + assert logging_obj.model_call_details["has_logged_async_success"] is True diff --git a/tests/unit/test_utils_get_optional_params.py b/tests/unit/test_utils_get_optional_params.py index 13a64577efb..05b776cb873 100644 --- a/tests/unit/test_utils_get_optional_params.py +++ b/tests/unit/test_utils_get_optional_params.py @@ -1,4 +1,5 @@ import json +import logging from typing import Final from unittest.mock import MagicMock, patch @@ -2097,3 +2098,146 @@ def test_supports_system_message_merges_system_prompt_into_user_message() -> Non ] assert map_system_message_pt(messages=messages) == [{"role": "user", "content": "Listen here! Hello there!"}] + + +def test_dropping_an_unsupported_reasoning_effort_logs_one_warning( + local_model_cost_map: None, caplog: pytest.LogCaptureFixture +) -> None: + with caplog.at_level(logging.WARNING, logger="LiteLLM"): + optional_params: Final = get_optional_params( + model="terra-ptu", custom_llm_provider="azure", reasoning_effort="none", drop_params=True + ) + + assert "reasoning_effort" not in optional_params + drop_warnings: Final = [ + record.getMessage() for record in caplog.records if "Dropping reasoning_effort" in record.getMessage() + ] + assert len(drop_warnings) == 1 + assert all(fragment in drop_warnings[0] for fragment in ("'none'", "terra-ptu", "model_info.base_model")) + + +@pytest.mark.parametrize("provider", ["ollama", "ollama_chat", "lemonade", "huggingface"]) +def test_dropping_thinking_for_an_unmapped_local_model_sends_no_request( + local_model_cost_map: None, caplog: pytest.LogCaptureFixture, monkeypatch: pytest.MonkeyPatch, provider: str +) -> None: + outbound: Final = respx.MockRouter(assert_all_mocked=False) + monkeypatch.setattr( + litellm, "module_level_client", HTTPHandler(client=httpx.Client(transport=httpx.MockTransport(outbound.handler))) + ) + + with caplog.at_level(logging.WARNING, logger="LiteLLM"): + optional_params: Final = get_optional_params( + model="unmapped-local-model", custom_llm_provider=provider, thinking={"type": "disabled"}, drop_params=True + ) + + assert "thinking" not in optional_params + assert len([record for record in caplog.records if "Dropping thinking" in record.getMessage()]) == 1 + assert not outbound.calls + + +def test_dropping_an_unsupported_thinking_logs_one_warning( + local_model_cost_map: None, caplog: pytest.LogCaptureFixture +) -> None: + with caplog.at_level(logging.WARNING, logger="LiteLLM"): + optional_params: Final = get_optional_params( + model="terra-ptu", custom_llm_provider="azure", thinking={"type": "disabled"}, drop_params=True + ) + + assert "thinking" not in optional_params + drop_warnings: Final = [ + record.getMessage() for record in caplog.records if "Dropping thinking" in record.getMessage() + ] + assert len(drop_warnings) == 1 + assert all(fragment in drop_warnings[0] for fragment in ("'disabled'", "terra-ptu", "model_info.base_model")) + + +@pytest.mark.parametrize( + ("reasoning_kwargs", "warns"), + [ + ({"reasoning_effort": "none"}, False), + ({"thinking": {"type": "disabled"}}, False), + ({"reasoning_effort": "high"}, True), + ({"thinking": {"type": "enabled", "budget_tokens": 1024}}, True), + ], +) +def test_reasoning_drop_warns_unless_a_mapped_non_reasoning_model_was_asked_for_none( + local_model_cost_map: None, + caplog: pytest.LogCaptureFixture, + reasoning_kwargs: dict[str, object], + warns: bool, +) -> None: + litellm.register_model( + { + "azure/plain-chat-test-deployment": { + "litellm_provider": "azure", + "mode": "chat", + "max_tokens": 1024, + "input_cost_per_token": 0.0, + "output_cost_per_token": 0.0, + "supports_reasoning": False, + } + } + ) + + with caplog.at_level(logging.WARNING, logger="LiteLLM"): + optional_params: Final = get_optional_params( + model="plain-chat-test-deployment", custom_llm_provider="azure", drop_params=True, **reasoning_kwargs + ) + + assert not set(reasoning_kwargs) & set(optional_params) + assert any("Dropping" in record.getMessage() for record in caplog.records) is warns + + +@pytest.mark.parametrize("reasoning_kwargs", [{"reasoning_effort": "none"}, {"thinking": {"type": "disabled"}}]) +def test_turning_reasoning_off_warns_when_only_custom_pricing_describes_the_model( + local_model_cost_map: None, caplog: pytest.LogCaptureFixture, reasoning_kwargs: dict[str, object] +) -> None: + litellm.register_model( + { + "azure/pricing-only-test-deployment": { + "litellm_provider": "azure", + "mode": "chat", + "input_cost_per_token": 0.000001, + "output_cost_per_token": 0.000002, + } + } + ) + + with caplog.at_level(logging.WARNING, logger="LiteLLM"): + optional_params: Final = get_optional_params( + model="pricing-only-test-deployment", custom_llm_provider="azure", drop_params=True, **reasoning_kwargs + ) + + assert not set(reasoning_kwargs) & set(optional_params) + drop_warnings: Final = [record.getMessage() for record in caplog.records if "Dropping" in record.getMessage()] + assert len(drop_warnings) == 1 + assert all(fragment in drop_warnings[0] for fragment in ("pricing-only-test-deployment", "model_info.base_model")) + + +def test_dropping_reasoning_for_a_catalog_reasoning_model_does_not_suggest_base_model( + local_model_cost_map: None, caplog: pytest.LogCaptureFixture +) -> None: + assert litellm.model_cost["azure/gpt-5.6-sol"].get("supports_reasoning") is True + with caplog.at_level(logging.WARNING, logger="LiteLLM"): + optional_params: Final = get_optional_params( + model="gpt-5.6-sol", custom_llm_provider="azure", thinking={"type": "disabled"}, drop_params=True + ) + + assert "thinking" not in optional_params + drop_warnings: Final = [ + record.getMessage() for record in caplog.records if "Dropping thinking" in record.getMessage() + ] + assert len(drop_warnings) == 1 + assert "model_info.base_model" not in drop_warnings[0] + + +def test_turning_reasoning_off_for_a_catalog_non_reasoning_model_drops_it_silently( + local_model_cost_map: None, caplog: pytest.LogCaptureFixture +) -> None: + with caplog.at_level(logging.WARNING, logger="LiteLLM"): + optional_params: Final = get_optional_params( + model="gpt-4o", custom_llm_provider="azure", reasoning_effort="none", drop_params=True + ) + + assert "reasoning_effort" not in optional_params + assert not [record for record in caplog.records if "Dropping" in record.getMessage()] diff --git a/tests/unit/vector_stores/test_main.py b/tests/unit/vector_stores/test_main.py index 25a378270b1..21b1eee3a3e 100644 --- a/tests/unit/vector_stores/test_main.py +++ b/tests/unit/vector_stores/test_main.py @@ -15,12 +15,13 @@ from unittest.mock import MagicMock, patch import httpx import pytest +import litellm import litellm.vector_stores.main as vector_stores_main from litellm.llms.base_llm.vector_store.transformation import ( RouterVectorStoreEmbeddingExecutor, ) from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler -from litellm.vector_stores.main import acreate, aupdate, search +from litellm.vector_stores.main import acreate, aretrieve, aupdate, retrieve, search MOCK_SEARCH_RESPONSE = { "object": "vector_store.search_results.page", @@ -200,3 +201,45 @@ async def test_direct_sdk_vector_store_writes_send_the_callers_metadata_unchange sent_metadata: Final = await _metadata_sent_to_provider(operation, callers_metadata) assert sent_metadata == [callers_metadata] + + +def _vector_store_not_found(request: httpx.Request) -> httpx.Response: + return httpx.Response( + 404, + json={ + "error": { + "message": "No vector store found with id 'vs_missing'.", + "type": "invalid_request_error", + "param": "vector_store_id", + "code": None, + } + }, + request=request, + ) + + +async def _retrieve_missing_openai_vector_store(is_async: bool) -> object: + transport: Final = httpx.MockTransport(_vector_store_not_found) + if is_async: + return await aretrieve( + vector_store_id="vs_missing", + custom_llm_provider="openai", + api_key="sk-test", + client=AsyncHTTPHandler(transport=transport), + ) + return retrieve( + vector_store_id="vs_missing", + custom_llm_provider="openai", + api_key="sk-test", + client=HTTPHandler(client=httpx.Client(transport=transport)), + ) + + +@pytest.mark.asyncio +@pytest.mark.parametrize("is_async", [True, False]) +async def test_retrieving_a_missing_openai_vector_store_raises_not_found(is_async: bool): + with pytest.raises(litellm.NotFoundError) as caught: + await _retrieve_missing_openai_vector_store(is_async) + + assert caught.value.status_code == 404 + assert "No vector store found with id 'vs_missing'." in caught.value.message diff --git a/ui/litellm-dashboard/AGENTS.md b/ui/litellm-dashboard/AGENTS.md index 3453a144e89..274dcff1f48 100644 --- a/ui/litellm-dashboard/AGENTS.md +++ b/ui/litellm-dashboard/AGENTS.md @@ -4,7 +4,7 @@ Never put LiteLLM tokens or API keys in `localStorage`. `localStorage` survives When you fix lint violations that are grandfathered in `eslint-suppressions.json`, run `eslint . --prune-suppressions` and commit the updated baseline so the gate ratchets down instead of leaving a stale suppression -`src/lib/http/schema.d.ts` is generated from the proxy's OpenAPI spec. Never hand-edit it. After changing a backend route or response model that the dashboard consumes, run `npm run gen:api` and commit the result (CI `lint / ui-api-types` enforces this) +`src/lib/http/schema.d.ts` is generated from the proxy's OpenAPI spec. Never hand-edit it. After changing any backend route or request or response model, or bumping a dependency, run `npm run gen:api` and commit the result, since the spec includes every route, not only the ones the dashboard calls (CI `lint / ui-api-types` enforces this) Tests come in three tiers, named by the standard definitions. `Foo.test.tsx` is a unit test: one module, collaborators replaced by doubles, no multi-component tree, and it should run in milliseconds. `Foo.integration.test.tsx` renders a real component tree with real children and only stubs the network boundary. It costs seconds per case, so it earns its place by proving wiring that a unit test cannot reach. Browser-level tests live in `tests/e2e/ui/` as Playwright specs against a live proxy diff --git a/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/CreateVectorStore.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/CreateVectorStore.test.tsx index ba3d1d0fb7d..f8e2feaa635 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/CreateVectorStore.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/CreateVectorStore.test.tsx @@ -18,6 +18,7 @@ vi.mock("@/components/vector_store_providers", () => ({ AZURE_OPENAI: "Azure OpenAI", S3Vectors: "AWS S3 Vectors", Valkey: "Valkey", + OpenSearch: "Amazon OpenSearch", }, vectorStoreProviderMap: { BEDROCK: "bedrock", @@ -25,6 +26,7 @@ vi.mock("@/components/vector_store_providers", () => ({ AZURE_OPENAI: "azure_openai", S3Vectors: "s3_vectors", Valkey: "valkey", + OpenSearch: "opensearch", }, vectorStoreProviderLogoMap: { "Amazon Bedrock": "https://example.com/bedrock.png", @@ -32,6 +34,7 @@ vi.mock("@/components/vector_store_providers", () => ({ "Azure OpenAI": "https://example.com/azure.png", "AWS S3 Vectors": "https://example.com/aws.png", Valkey: "https://example.com/valkey.svg", + "Amazon OpenSearch": "https://example.com/aws.svg", }, getProviderSpecificFields: vi.fn((provider: string) => { if (provider === "s3_vectors") { @@ -254,7 +257,7 @@ describe("CreateVectorStore", () => { expect(screen.getByText("Vector Store Created Successfully")).toBeInTheDocument(); }); - it("should exclude valkey from the provider dropdown since it has no RAG ingestion", async () => { + it("should exclude search-only valkey and opensearch from the provider dropdown since they have no RAG ingestion", async () => { render(); const providerSelect = screen.getByRole("combobox"); @@ -267,6 +270,7 @@ describe("CreateVectorStore", () => { expect(screen.getByText("AWS S3 Vectors")).toBeInTheDocument(); }); expect(screen.queryByText("Valkey")).not.toBeInTheDocument(); + expect(screen.queryByText("Amazon OpenSearch")).not.toBeInTheDocument(); }); it("should display S3 Vectors provider-specific fields when selected", async () => { diff --git a/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/CreateVectorStore.tsx b/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/CreateVectorStore.tsx index 2422e83355d..2d7375431cd 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/CreateVectorStore.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/CreateVectorStore.tsx @@ -36,7 +36,7 @@ const ACCEPTED_DOCUMENT_TYPES = [ const MAX_DOCUMENT_BYTES = 50 * 1024 * 1024; -const RAG_INGEST_UNSUPPORTED_PROVIDERS = new Set(["valkey"]); +const RAG_INGEST_UNSUPPORTED_PROVIDERS = new Set(["valkey", "opensearch"]); const providerItems = Object.entries(VectorStoreProviders) .filter(([providerEnum]) => !RAG_INGEST_UNSUPPORTED_PROVIDERS.has(vectorStoreProviderMap[providerEnum])) diff --git a/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/VectorStoreForm.integration.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/VectorStoreForm.integration.test.tsx index d7ec325653f..f571ec6b9a0 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/VectorStoreForm.integration.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/VectorStoreForm.integration.test.tsx @@ -1,4 +1,4 @@ -import { render, screen } from "@testing-library/react"; +import { fireEvent, render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { beforeEach, describe, expect, it, vi } from "vitest"; @@ -134,6 +134,70 @@ describe("VectorStoreForm submit payload", () => { expect(createdPayload().custom_llm_provider).toBe("milvus"); }); + it("registers an existing opensearch index with the proxy embedding model and default field names", async () => { + const user = setupUser(); + renderForm(); + + await chooseProvider(user, "Amazon OpenSearch"); + fireEvent.change(screen.getByPlaceholderText("my-knn-index (OpenSearch index name)"), { + target: { value: "litellm-qa-docs" }, + }); + fireEvent.change(screen.getByPlaceholderText("https://abc123.us-east-1.aoss.amazonaws.com"), { + target: { value: "https://yzatq55tpsuvlwozcal4.us-east-1.aoss.amazonaws.com" }, + }); + await chooseFromSelect(user, 1, "text-embedding-3-small"); + await submit(user); + + const expectedLitellmParams = { + api_base: "https://yzatq55tpsuvlwozcal4.us-east-1.aoss.amazonaws.com", + aws_region_name: undefined, + litellm_embedding_model: "text-embedding-3-small", + opensearch_embedding_field: "embedding", + opensearch_text_field: "text", + opensearch_index_name: undefined, + }; + + await vi.waitFor(() => expect(mockCreate).toHaveBeenCalledTimes(1)); + expect(createdPayload().custom_llm_provider).toBe("opensearch"); + expect(createdPayload().vector_store_id).toBe("litellm-qa-docs"); + expect(createdPayload().litellm_params).toStrictEqual(expectedLitellmParams); + }); + + it("registers an opensearch index under its own name when the vector store id differs", async () => { + const user = setupUser(); + renderForm(); + + await chooseProvider(user, "Amazon OpenSearch"); + fireEvent.change(screen.getByPlaceholderText("my-knn-index (OpenSearch index name)"), { + target: { value: "east-docs" }, + }); + fireEvent.change(screen.getByPlaceholderText("https://abc123.us-east-1.aoss.amazonaws.com"), { + target: { value: "https://yzatq55tpsuvlwozcal4.us-east-1.aoss.amazonaws.com" }, + }); + fireEvent.change(screen.getByPlaceholderText("my-knn-index"), { target: { value: "docs" } }); + await chooseFromSelect(user, 1, "text-embedding-3-small"); + await submit(user); + + await vi.waitFor(() => expect(mockCreate).toHaveBeenCalledTimes(1)); + expect(createdPayload().vector_store_id).toBe("east-docs"); + expect(createdPayload().litellm_params).toMatchObject({ opensearch_index_name: "docs" }); + }); + + it("blocks an opensearch submit that has no endpoint or embedding model", async () => { + const user = setupUser(); + renderForm(); + + await chooseProvider(user, "Amazon OpenSearch"); + fireEvent.change(screen.getByPlaceholderText("my-knn-index (OpenSearch index name)"), { + target: { value: "litellm-qa-docs" }, + }); + await submit(user); + + expect(await screen.findByText("Please input the opensearch endpoint")).toBeInTheDocument(); + expect(screen.getByText("Please select the embedding model")).toBeInTheDocument(); + expect(mockCreate).not.toHaveBeenCalled(); + }); + it("sends a provider field's seeded default even when the user never touches it", async () => { const user = setupUser(); renderForm(); diff --git a/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/VectorStoreForm.tsx b/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/VectorStoreForm.tsx index d82f56f3870..bf5b925eb16 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/VectorStoreForm.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/VectorStoreForm.tsx @@ -36,7 +36,7 @@ import { useZodForm } from "@/lib/forms/useZodForm"; import { SearchSelect } from "@/components/shared/SearchSelect"; import { credentialOptions } from "@/components/shared/credentialOptions"; -const EMBEDDING_MODEL_RENAME_PROVIDERS = new Set(["milvus", "valkey", "mongodb"]); +const EMBEDDING_MODEL_RENAME_PROVIDERS = new Set(["milvus", "valkey", "mongodb", "opensearch"]); export const buildVectorStoreLitellmParams = ( provider: string, @@ -83,6 +83,9 @@ const PROVIDER_FIELD_NAMES = [ "valkey_ssl", "valkey_text_field", "valkey_embedding_field", + "opensearch_embedding_field", + "opensearch_text_field", + "opensearch_index_name", ] as const; type ProviderFieldName = (typeof PROVIDER_FIELD_NAMES)[number]; @@ -119,6 +122,9 @@ const vectorStoreShape = { valkey_ssl: optionalText, valkey_text_field: optionalText, valkey_embedding_field: optionalText, + opensearch_embedding_field: optionalText, + opensearch_text_field: optionalText, + opensearch_index_name: optionalText, }; const vectorStoreSchema = z.object(vectorStoreShape).superRefine((values, ctx) => { @@ -143,6 +149,7 @@ const VECTOR_STORE_ID_PLACEHOLDERS: Record = { "vertex_ai/search_api": 'my-datastore_1234567890 (data store ID from Vertex AI / "Agent Search" console)', valkey: "my-search-index (FT index name in Valkey)", mongodb: "my-vector-index (MongoDB Vector Search index name)", + opensearch: "my-knn-index (OpenSearch index name)", }; const VERTEX_SEARCH_API_WITH_ENGINE_PLACEHOLDER = "Any identifier you'll use to reference this in LiteLLM"; @@ -159,6 +166,8 @@ const EMPTY_VALUES: VectorStoreFormValues = { valkey_ssl: "false", valkey_text_field: "text", valkey_embedding_field: "embedding", + opensearch_embedding_field: "embedding", + opensearch_text_field: "text", }; const labelWithHint = (label: string, hint: string): React.ReactNode => ( @@ -376,6 +385,35 @@ const VectorStoreForm: React.FC = ({ )} + {selectedProvider === "opensearch" && ( + + + OpenSearch Setup Required + +

+ LiteLLM searches a k-NN index you already have in an OpenSearch Serverless collection or an + OpenSearch Service domain. It does not create the index or upload documents. Before creating this + vector store, make sure: +

+
    +
  1. + The index has a knn_vector field holding each document's embedding. Enter the index name as + the Vector Store ID, or under Index Name when another endpoint already uses that ID +
  2. +
  3. + You know which embedding model created those embeddings. That model must be added to this proxy + under Models so you can pick it below. Using a different model returns wrong results +
  4. +
  5. + The proxy has AWS credentials that can read the index, either its own (environment variables or + an IAM role) or a saved credential picked under Existing Credentials. A Serverless collection + needs a data access policy for them, and a domain needs its access policy to allow them +
  6. +
+
+
+ )} + {selectedProvider === "vertex_rag_engine" && ( diff --git a/ui/litellm-dashboard/src/components/UsagePage/components/EntityUsage/TopKeyView.test.tsx b/ui/litellm-dashboard/src/components/UsagePage/components/EntityUsage/TopKeyView.test.tsx index a9c87471511..2b4b69ffe7b 100644 --- a/ui/litellm-dashboard/src/components/UsagePage/components/EntityUsage/TopKeyView.test.tsx +++ b/ui/litellm-dashboard/src/components/UsagePage/components/EntityUsage/TopKeyView.test.tsx @@ -493,6 +493,26 @@ describe("TopKeyView", () => { }); }); + it.each([ + ["raw:unattributed", "unattributed"], + ["raw:raw:unattributed", "raw:unattributed"], + ["raw:raw:raw:key", "raw:raw:key"], + ])("opens the stored key behind reporting ID %s", async (bucket, key) => { + mockKeyInfoV1Call.mockResolvedValue({ key }); + mockTransformKeyInfo.mockReturnValue({ token: key } as KeyResponse); + render( + , + ); + + fireEvent.click(screen.getByRole("button", { name: bucket })); + + expect(await screen.findByText(`Key Info View for ${key}`)).toBeInTheDocument(); + expect(mockKeyInfoV1Call).toHaveBeenCalledWith("test-token", key); + }); + it("should only look up keys that still exist in the database, from both the table and the chart", async () => { mockKeyInfoV1Call.mockResolvedValue({ key: "info" }); mockTransformKeyInfo.mockReturnValue({ transformed: "data" } as unknown as KeyResponse); @@ -503,6 +523,7 @@ describe("TopKeyView", () => { {...baseProps} topKeys={[ { api_key: "session-key", key_alias: null, user: "alice@example.com", key_exists: false, spend: 100 }, + { api_key: "unattributed", key_alias: "Unattributed", user: null, key_exists: false, spend: 25 }, { api_key: "stored-key", key_alias: "Stored", user: null, key_exists: true, spend: 50 }, ]} />, @@ -510,11 +531,14 @@ describe("TopKeyView", () => { expect(screen.getByRole("button", { name: "stored-key" })).toBeInTheDocument(); expect(screen.queryByRole("button", { name: "session-key" })).not.toBeInTheDocument(); + expect(screen.getByText("Unattributed")).toBeInTheDocument(); + expect(screen.queryByRole("button", { name: "unattributed" })).not.toBeInTheDocument(); + await user.click(screen.getByText("unattributed")); await user.click(screen.getByText("session-key")); await user.click(screen.getByRole("radio", { name: "Chart View" })); const bars = chartBars(container); - expect(bars).toHaveLength(2); + expect(bars).toHaveLength(3); bars.forEach((bar) => fireEvent.click(bar)); expect(await screen.findByText("Key Info View for stored-key")).toBeInTheDocument(); diff --git a/ui/litellm-dashboard/src/components/UsagePage/components/EntityUsage/TopKeyView.tsx b/ui/litellm-dashboard/src/components/UsagePage/components/EntityUsage/TopKeyView.tsx index 90a26919e01..291bcb34c73 100644 --- a/ui/litellm-dashboard/src/components/UsagePage/components/EntityUsage/TopKeyView.tsx +++ b/ui/litellm-dashboard/src/components/UsagePage/components/EntityUsage/TopKeyView.tsx @@ -11,6 +11,7 @@ import { transformKeyInfo } from "../../../key_team_helpers/transform_key_info"; import { keyInfoV1Call } from "../../../networking"; import KeyInfoView from "../../../templates/key_info_view"; import { TagUsage } from "../../types"; +import { storedKeyId } from "../../keyActivityData"; const TOP_KEYS_LIMITS = [5, 10, 25, 50] as const; const LIMIT_OPTIONS = TOP_KEYS_LIMITS.map((limit) => ({ value: String(limit), label: String(limit) })); @@ -70,11 +71,12 @@ const TopKeyView: React.FC = ({ topKeys, teams, showTags = fals if (!accessToken || !canOpenKeyInfo(item)) return; try { - const keyInfo = await keyInfoV1Call(accessToken, item.api_key); + const keyId = storedKeyId(item.api_key); + const keyInfo = await keyInfoV1Call(accessToken, keyId); const transformedKeyData = transformKeyInfo(keyInfo); setKeyData(transformedKeyData); - setSelectedKey(item.api_key); + setSelectedKey(keyId); setIsModalOpen(true); // Open modal when key is clicked } catch (error) { console.error("Error fetching key info:", error); diff --git a/ui/litellm-dashboard/src/components/UsagePage/keyActivityData.test.ts b/ui/litellm-dashboard/src/components/UsagePage/keyActivityData.test.ts index b35ea72896a..a5db5db14cc 100644 --- a/ui/litellm-dashboard/src/components/UsagePage/keyActivityData.test.ts +++ b/ui/litellm-dashboard/src/components/UsagePage/keyActivityData.test.ts @@ -7,7 +7,13 @@ import { type KeySpendActivityRow, toDailyData, } from "./dailyActivityApi"; -import { keyDetailFromResponse, mergeKeyActivityPages, overallUsageMetrics } from "./keyActivityData"; +import { + keyDetailFromResponse, + mergeKeyActivityPages, + overallUsageMetrics, + reportingKeyId, + storedKeyId, +} from "./keyActivityData"; const completeMetrics: components["schemas"]["SpendMetrics"] = { api_requests: 2, @@ -80,6 +86,50 @@ const pageRow = (api_key: string): KeySpendActivityRow => ({ }); describe("key activity data", () => { + it.each([ + ["", "unattributed"], + ["unattributed", "raw:unattributed"], + ["raw:key", "raw:raw:key"], + ["raw:raw:key", "raw:raw:raw:key"], + ["ordinary-hash", "ordinary-hash"], + [" ", " "], + ])("round trips stored key %s through reporting", (key, bucket) => { + expect(reportingKeyId(key)).toBe(bucket); + expect(storedKeyId(bucket)).toBe(key); + }); + + it("retains unattributed totals and unknown models in key details", () => { + const unattributed = { + metrics: completeMetrics, + metadata: { key_alias: "Unattributed", key_exists: false }, + }; + const response: DailyActivityAggregatedResponse = { + ...aggregatedResponse, + results: [ + { + date: "2026-09-27", + metrics: completeMetrics, + breakdown: { + api_keys: { unattributed }, + models: { + "unknown-model": { + metrics: completeMetrics, + metadata: {}, + api_key_breakdown: { unattributed }, + }, + }, + }, + }, + ], + }; + + const detail = keyDetailFromResponse(response, "unattributed", []); + expect(detail?.total_requests).toBe(2); + expect(detail?.total_spend).toBe(1.25); + expect(detail?.top_models.map((model) => model.model)).toEqual(["unknown-model"]); + expect(detail?.daily_data[0].metrics.api_requests).toBe(2); + }); + it("builds overall totals from metadata and daily data from complete daily metrics", () => { const summary = overallUsageMetrics( toDailyData(aggregatedResponse), diff --git a/ui/litellm-dashboard/src/components/UsagePage/keyActivityData.ts b/ui/litellm-dashboard/src/components/UsagePage/keyActivityData.ts index 4b2e8c573f2..2d2977058a5 100644 --- a/ui/litellm-dashboard/src/components/UsagePage/keyActivityData.ts +++ b/ui/litellm-dashboard/src/components/UsagePage/keyActivityData.ts @@ -4,6 +4,16 @@ import type { DailyActivityAggregatedResponse, DailyActivityMetadata, KeySpendAc import { toDailyData } from "./dailyActivityApi"; import type { ModelActivityData } from "./types"; +export const reportingKeyId = (key: string): string => { + if (!key) return "unattributed"; + return key === "unattributed" || key.startsWith("raw:") ? `raw:${key}` : key; +}; + +export const storedKeyId = (bucket: string): string => { + if (bucket === "unattributed") return ""; + return bucket.startsWith("raw:") ? bucket.slice(4) : bucket; +}; + export const overallUsageMetrics = ( results: ReturnType, metadata: DailyActivityMetadata, diff --git a/ui/litellm-dashboard/src/components/templates/KeySavingsTab.integration.test.tsx b/ui/litellm-dashboard/src/components/templates/KeySavingsTab.integration.test.tsx index 3567388de05..40ab0e8e77c 100644 --- a/ui/litellm-dashboard/src/components/templates/KeySavingsTab.integration.test.tsx +++ b/ui/litellm-dashboard/src/components/templates/KeySavingsTab.integration.test.tsx @@ -131,6 +131,18 @@ describe("KeySavingsTab", () => { expect(screen.getByTestId("key-savings-scope-note")).toHaveTextContent("Showing your own requests"); }); + it.each([ + ["unattributed", "raw:unattributed"], + ["raw:unattributed", "raw:raw:unattributed"], + ["raw:raw:key", "raw:raw:raw:key"], + ])("requests savings for the real key %s", (keyToken, bucket) => { + const hook = scopedRange().mockReturnValue(mockActivity()); + + renderTab({ keyToken, userRole: "Admin" }); + + expect(hook).toHaveBeenCalledWith("test-token", { userId: null, apiKey: bucket }, activity); + }); + it("reads the whole key for a proxy admin, with no scope note to contradict it", () => { const hook = scopedRange().mockReturnValue(mockActivity()); diff --git a/ui/litellm-dashboard/src/components/templates/KeySavingsTab.tsx b/ui/litellm-dashboard/src/components/templates/KeySavingsTab.tsx index d1395e0be51..03aced7358e 100644 --- a/ui/litellm-dashboard/src/components/templates/KeySavingsTab.tsx +++ b/ui/litellm-dashboard/src/components/templates/KeySavingsTab.tsx @@ -1,6 +1,7 @@ "use client"; import ScopedSavingsTab from "@/components/shared/ScopedSavingsTab"; +import { reportingKeyId } from "@/components/UsagePage/keyActivityData"; import { hasProxyWideSpendView, spendScopeUserId } from "@/utils/roles"; import type { ActivityDateRange } from "@/app/(dashboard)/cost-optimization/_components/useDailyActivityRange"; @@ -15,7 +16,7 @@ interface KeySavingsTabProps { const KeySavingsTab = ({ accessToken, keyToken, userId, userRole, activity }: KeySavingsTabProps) => ( { expect(byName("mongodb_num_candidates")).toMatchObject({ required: false }); }); + it("registers opensearch with its endpoint, optional region, proxy embedding model, and default field names", () => { + const fields = getProviderSpecificFields("opensearch"); + const byName = (name: string) => fields.find((field) => field.name === name); + + expect(getVectorStoreProviderLogoAndName("opensearch")).toEqual({ + logo: expect.stringContaining("aws"), + displayName: VectorStoreProviders.OpenSearch, + }); + expect(fields.map((field) => field.name)).toEqual([ + "api_base", + "aws_region_name", + "embedding_model", + "opensearch_embedding_field", + "opensearch_text_field", + "opensearch_index_name", + ]); + expect(byName("api_base")).toMatchObject({ required: true }); + expect(byName("aws_region_name")).toMatchObject({ required: false }); + expect(byName("embedding_model")).toMatchObject({ type: "select", required: true }); + expect(byName("embedding_model")).not.toHaveProperty("options"); + expect(byName("opensearch_embedding_field")).toMatchObject({ required: false, initialValue: "embedding" }); + expect(byName("opensearch_text_field")).toMatchObject({ required: false, initialValue: "text" }); + expect(byName("opensearch_index_name")).toMatchObject({ required: false }); + expect(byName("opensearch_index_name")).not.toHaveProperty("initialValue"); + }); + it("registers valkey in the provider, logo, and field maps", () => { expect(vectorStoreProviderMap.Valkey).toBe("valkey"); expect(vectorStoreProviderLogoMap[VectorStoreProviders.Valkey]).toContain("valkey"); diff --git a/ui/litellm-dashboard/src/components/vector_store_providers.tsx b/ui/litellm-dashboard/src/components/vector_store_providers.tsx index 79e711f6f98..e953d6e3db6 100644 --- a/ui/litellm-dashboard/src/components/vector_store_providers.tsx +++ b/ui/litellm-dashboard/src/components/vector_store_providers.tsx @@ -1,4 +1,5 @@ import { getProviderLogoAndName, Providers, providerLogoMap } from "@/components/provider_info_helpers"; +import awsLogo from "../../public/assets/logos/aws.svg"; import milvusLogo from "../../public/assets/logos/milvus.svg"; import mongodbLogo from "../../public/assets/logos/mongodb.svg"; import postgresqlLogo from "../../public/assets/logos/postgresql.svg"; @@ -8,6 +9,7 @@ import valkeyLogo from "../../public/assets/logos/valkey.svg"; export enum VectorStoreProviders { Bedrock = "Amazon Bedrock", S3Vectors = "Amazon S3 Vectors", + OpenSearch = "Amazon OpenSearch", PgVector = "PostgreSQL pgvector (LiteLLM Connector)", VertexRagEngine = "Vertex AI RAG Engine", VertexAiSearch = "Vertex AI Search", @@ -28,6 +30,7 @@ export const vectorStoreProviderMap: Record = { Milvus: "milvus", MongoDB: "mongodb", S3Vectors: "s3_vectors", + OpenSearch: "opensearch", Valkey: "valkey", }; @@ -41,6 +44,7 @@ export const vectorStoreProviderLogoMap: Record = { [VectorStoreProviders.Milvus]: milvusLogo.src, [VectorStoreProviders.MongoDB]: mongodbLogo.src, [VectorStoreProviders.S3Vectors]: s3VectorLogo.src, + [VectorStoreProviders.OpenSearch]: awsLogo.src, [VectorStoreProviders.Valkey]: valkeyLogo.src, }; @@ -313,6 +317,64 @@ export const vectorStoreProviderFields: Record initialValue: "embedding", }, ], + opensearch: [ + { + name: "api_base", + label: "OpenSearch Endpoint", + tooltip: + "The endpoint of your OpenSearch Serverless collection or OpenSearch Service domain, starting with https://", + placeholder: "https://abc123.us-east-1.aoss.amazonaws.com", + required: true, + type: "text", + }, + { + name: "aws_region_name", + label: "AWS Region (optional)", + tooltip: + "The AWS region of the collection or domain. Leave blank to use the region in the endpoint, and set it when the endpoint is a custom domain", + placeholder: "us-east-1", + required: false, + type: "text", + }, + { + name: "embedding_model", + label: "Embedding Model", + tooltip: + "The embedding model on this proxy that created the vectors already stored in your index. LiteLLM embeds every search query with it, so it must be the same model. Add it under Models first if it is not listed", + placeholder: "text-embedding-3-small", + required: true, + type: "select", + }, + { + name: "opensearch_embedding_field", + label: "Vector Field Name", + tooltip: + "The top-level knn_vector field in your index mapping that holds each document's embedding (default: embedding)", + placeholder: "embedding", + required: false, + type: "text", + initialValue: "embedding", + }, + { + name: "opensearch_text_field", + label: "Text Field", + tooltip: + "The top-level field in each document that holds its readable text. LiteLLM returns this text in search results (default: text)", + placeholder: "text", + required: false, + type: "text", + initialValue: "text", + }, + { + name: "opensearch_index_name", + label: "Index Name (optional)", + tooltip: + "The OpenSearch index to search. Leave blank to use the Vector Store ID, and set it to register same-named indexes from two endpoints under different IDs", + placeholder: "my-knn-index", + required: false, + type: "text", + }, + ], s3_vectors: [ { name: "vector_bucket_name", diff --git a/ui/litellm-dashboard/src/lib/http/schema.d.ts b/ui/litellm-dashboard/src/lib/http/schema.d.ts index a5ff5051ebf..ab3e4559e4b 100644 --- a/ui/litellm-dashboard/src/lib/http/schema.d.ts +++ b/ui/litellm-dashboard/src/lib/http/schema.d.ts @@ -50437,6 +50437,8 @@ export interface components { discoverable?: boolean | null; /** Enable Tag Filtering */ enable_tag_filtering?: boolean | null; + /** Enforce Supported Endpoints */ + enforce_supported_endpoints?: boolean | null; /** Explicit Cache Read Input Token Cost */ explicit_cache_read_input_token_cost?: number | null; /** Id */