litellm/scripts/check_native_imports.py
narmstrong-berri 0ba4e85496
Some checks failed
Cost map pins recheck / rerun-cost-map-guard (push) Has been cancelled
LiteLLM Rust DB / db-queries (push) Has been cancelled
LiteLLM Rust / rust-lint (push) Has been cancelled
LiteLLM Rust / rust-test (push) Has been cancelled
LiteLLM Rust / rust-wheel (push) Has been cancelled
Terraform Provider / gofmt, vet, build, test (push) Has been cancelled
Terraform Provider / Provider endpoints vs proxy OpenAPI schema (push) Has been cancelled
refactor: make cross-API bridges call native executors instead of re-entering acompletion and aresponses
Each client surface (/v1/chat/completions, /v1/responses, /v1/messages) now
decides the provider's native API format once with litellm/native/route_plan.py,
owns logging for the request, and hands the translated request to a native
executor (execute_chat, execute_responses, execute_messages) instead of calling
back into a public entry point.

- Messages to Responses, Messages to chat, Responses to chat and chat to
  Responses bridges call the native executors
- Responses-mode models on providers with no Responses API stay on chat
- Bridges price the provider reply before translating it, keep its headers,
  map provider errors and run JSON-schema validation as re-entry did
- Bridged streams are logged once, under the client's call type, when the
  client stream ends
- execute_chat and the chat provider helpers move from main.py to
  litellm/native/, and a lint check keeps litellm.native from importing
  litellm.main
2026-10-30 01:54:38 +00:00

58 lines
1.8 KiB
Python

import ast
import sys
from collections.abc import Iterator
from pathlib import Path
from typing import Final
PACKAGE: Final = Path("litellm/native")
FORBIDDEN: Final = "litellm.main"
def _is_forbidden(module: str) -> bool:
return module == FORBIDDEN or module.startswith(f"{FORBIDDEN}.")
def _from_import_targets(package: tuple[str, ...], node: ast.ImportFrom) -> Iterator[str]:
base: Final = package[: len(package) - node.level + 1] if node.level else ()
module: Final = ".".join((*base, node.module) if node.module else base)
yield module
yield from (f"{module}.{alias.name}" for alias in node.names)
def _imported(package: tuple[str, ...], node: ast.AST) -> Iterator[str]:
match node:
case ast.Import(names=names):
yield from (alias.name for alias in names)
case ast.ImportFrom():
yield from _from_import_targets(package, node)
case ast.Attribute(value=ast.Name(id="litellm"), attr=attr):
yield f"litellm.{attr}"
case _:
return
def violations(path: Path, source: str) -> tuple[str, ...]:
package: Final = (
("litellm", "native", *path.relative_to(PACKAGE).parent.parts) if path.is_relative_to(PACKAGE) else ()
)
tree: Final = ast.parse(source, filename=str(path))
return tuple(
f"{path}:{node.lineno}: litellm.native must not depend on litellm.main"
for node in ast.walk(tree)
if any(_is_forbidden(target) for target in _imported(package, node))
)
def main() -> int:
findings: Final = tuple(
finding for path in sorted(PACKAGE.rglob("*.py")) for finding in violations(path, path.read_text())
)
if findings:
sys.stderr.write("\n".join(findings) + "\n")
return 1
sys.stdout.write("litellm.native imports: passed\n")
return 0
if __name__ == "__main__":
sys.exit(main())