mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-02 01:28:38 +00:00
* fix(anthropic): time-limit /v1/messages pump waits on a stalled client The upstream pump waited on client_detached with no timeout, both when the relay queue was full and when waiting for the relay to consume a queued upstream error. client_detached is only set by the relay generator's finally block, so a client that resets its connection while stalled could leave the pump parked forever, holding its request, logging object and collected chunks. Both waits now give up after ANTHROPIC_MESSAGES_STREAM_CLIENT_STALL_TIMEOUT_SECONDS (default 120), treat the client as gone, and fall through to the existing detached billing path. * fix(anthropic): bound the detached /v1/messages drain and end a relay whose pump gave up A provider that stalled after the client left kept the detached pump, the open provider stream and every chunk it had buffered for billing alive until the provider read timed out, if it ever did. Pumps that never read another chunk after the disconnect are not counted against the detached-drain cap, so nothing limited them A detached drain still reading ANTHROPIC_MESSAGES_DETACHED_STREAM_DRAIN_TIMEOUT_SECONDS (default 600) after the client left now bills what it read and closes the provider stream. The deadline starts when the relay closes or when the client stall timeout gives up on the client, so it also covers a read that was already stalled Once the stall timeout gives up, the pump stops feeding the relay. A client that resumed reading then waited on an empty queue forever. The relay now ends with the incomplete-stream error once the pump is gone. The stall timeout is read with get_env_float so fractional values parse Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(anthropic): end a stream cleanly when the pump gives up on its last put When the upstream had already finished and only the end-of-stream sentinel was left, a client that resumed after the stall timeout got every chunk through message_stop and then an extra incomplete-stream error event. The relay now only adds that error when the client has not already received a message_stop or error event The new test pins the case where the stall timeout fires on the final sentinel put, so the pump exits with the request billed and a resumed client still sees a clean, complete stream Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(anthropic): give a stalled /v1/messages client 600 s before the pump gives up on it The 120 s default could cut off a client that was only slow. 600 s matches the usual request_timeout, so a stalled client and a stalled provider get the same budget. SSE keepalive pings do not change this, because they block on the same stalled write. A stuck pump now lives at most about 20 minutes, which keeps only a handful alive at once Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(anthropic_messages): keep queued chunks when dropping an unconsumed upstream error _exception_left_unconsumed emptied the whole relay queue to check whether the provider error was still in it, so a client that resumed after the error wait expired lost every chunk buffered ahead of the error. Re-queue everything but the error, add a regression test for the resumed client, and type the new test helpers. --------- Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
143 lines
5.1 KiB
Python
143 lines
5.1 KiB
Python
import os
|
|
import re
|
|
from collections.abc import Iterator
|
|
|
|
# Define the base directory for the litellm repository and documentation path
|
|
repo_base = "./litellm" # Change this to your actual path
|
|
|
|
_GETENV_ARGS = r"""\(\s*['"]([^'"]+)['"]\s*(?:,\s*[^)]*)?\)"""
|
|
_GET_SECRET_ARGS = r"""\(\s*['"]([^'"]+)['"]\s*(?:,\s*[^)]*|,\s*default_value=[^)]*)?\)"""
|
|
|
|
ENV_KEY_PATTERNS: tuple[re.Pattern[str], ...] = (
|
|
re.compile(r"os\.getenv" + _GETENV_ARGS),
|
|
re.compile(r"(?<![\w.])(?:litellm\.(?:utils\.)?)?get_secret(?:_str|_bool)?" + _GET_SECRET_ARGS),
|
|
)
|
|
|
|
DOCS_BASE = "./docs/my-website/docs"
|
|
REFERENCE_TABLE_PATH = f"{DOCS_BASE}/proxy/config_settings.md"
|
|
DOCS_SUFFIXES = (".md", ".mdx")
|
|
DOCUMENTED_KEY_PATTERN = re.compile(r"\b[A-Z][A-Z0-9_]*\b")
|
|
|
|
# Terminal/environment detection variables that should not be documented
|
|
# These are internal variables used for terminal detection, not user-configurable settings
|
|
# Guard-only env vars: read solely to raise on invalid values; the only valid
|
|
# value is the default, so there is nothing meaningful to document.
|
|
EXCLUDED_GUARD_ONLY_VARS = {
|
|
"MAVVRIK_FOCUS_FREQUENCY",
|
|
}
|
|
|
|
# Temporary/internal rollout flags are intentionally not added to the public
|
|
# environment settings docs until the feature is ready for broad use.
|
|
EXCLUDED_ROLLOUT_FLAGS = {
|
|
"LITELLM_USE_RUST_OCR",
|
|
"LITELLM_RUST",
|
|
"LITELLM_BUILTIN_PASS_THROUGH_ROUTES_FIRST",
|
|
}
|
|
|
|
# Internal infrastructure tuning parameters for streaming/queue management
|
|
# These are advanced settings with sensible defaults that most users should not modify
|
|
EXCLUDED_INTERNAL_TUNING_VARS = {
|
|
"ANTHROPIC_MESSAGES_MAX_DETACHED_STREAM_DRAINS",
|
|
"ANTHROPIC_MESSAGES_DETACHED_STREAM_DRAIN_TIMEOUT_SECONDS",
|
|
"ANTHROPIC_MESSAGES_STREAM_CLIENT_STALL_TIMEOUT_SECONDS",
|
|
"ANTHROPIC_MESSAGES_STREAM_RELAY_QUEUE_MAXSIZE",
|
|
"DEFAULT_S3_MAX_ADAPTIVE_CONCURRENCY",
|
|
}
|
|
|
|
EXCLUDED_TERMINAL_VARS = {
|
|
"TERM",
|
|
"TERM_PROGRAM",
|
|
"TERM_PROGRAM_VERSION",
|
|
"TERM_SESSION_ID",
|
|
"VTE_VERSION",
|
|
"KITTY_WINDOW_ID",
|
|
"KONSOLE_VERSION",
|
|
"ITERM_PROFILE",
|
|
"ITERM_PROFILE_NAME",
|
|
"ITERM_SESSION_ID",
|
|
"WEZTERM_VERSION",
|
|
"WT_SESSION",
|
|
"GNOME_TERMINAL_SCREEN",
|
|
"ALACRITTY_SOCKET",
|
|
}
|
|
|
|
EXCLUDED_KEYS = frozenset(
|
|
EXCLUDED_TERMINAL_VARS | EXCLUDED_GUARD_ONLY_VARS | EXCLUDED_ROLLOUT_FLAGS | EXCLUDED_INTERNAL_TUNING_VARS
|
|
)
|
|
|
|
# Directories to skip (dependencies, venvs, caches) - only scan litellm source
|
|
SKIP_DIRS = {
|
|
".venv",
|
|
"venv",
|
|
"__pycache__",
|
|
".git",
|
|
"node_modules",
|
|
"site-packages",
|
|
".eggs",
|
|
"dist",
|
|
"build",
|
|
}
|
|
|
|
|
|
def extract_env_keys(source: str) -> frozenset[str]:
|
|
"""Return every documentable env var name read by the given Python source."""
|
|
return frozenset(
|
|
match for pattern in ENV_KEY_PATTERNS for match in pattern.findall(source) if match not in EXCLUDED_KEYS
|
|
)
|
|
|
|
|
|
def collect_env_keys(base_dir: str) -> frozenset[str]:
|
|
"""Return every documentable env var name read anywhere under ``base_dir``."""
|
|
return frozenset(
|
|
key for file_path in _files_with_suffix(base_dir, (".py",)) for key in extract_env_keys(_read_text(file_path))
|
|
)
|
|
|
|
|
|
def _files_with_suffix(base_dir: str, suffixes: tuple[str, ...]) -> Iterator[str]:
|
|
for root, dirs, files in os.walk(base_dir):
|
|
# Skip dependency/venv directories - prevents picking up env vars from installed packages
|
|
dirs[:] = [d for d in dirs if d not in SKIP_DIRS]
|
|
yield from (os.path.join(root, name) for name in files if name.endswith(suffixes))
|
|
|
|
|
|
def _read_text(file_path: str) -> str:
|
|
with open(file_path, "r", encoding="utf-8") as f:
|
|
return f.read()
|
|
|
|
|
|
def extract_documented_keys(docs_content: str) -> frozenset[str]:
|
|
"""Return every env-var-shaped name mentioned anywhere in a documentation page."""
|
|
return frozenset(DOCUMENTED_KEY_PATTERN.findall(docs_content))
|
|
|
|
|
|
def collect_documented_keys(docs_dir: str) -> frozenset[str]:
|
|
"""Return every env-var-shaped name mentioned on any page under ``docs_dir``."""
|
|
return frozenset(
|
|
key
|
|
for file_path in _files_with_suffix(docs_dir, DOCS_SUFFIXES)
|
|
for key in extract_documented_keys(_read_text(file_path))
|
|
)
|
|
|
|
|
|
def undocumented_env_keys(base_dir: str, docs_dir: str) -> frozenset[str]:
|
|
"""Return the env vars read under ``base_dir`` that no page under ``docs_dir`` mentions."""
|
|
return collect_env_keys(base_dir) - collect_documented_keys(docs_dir)
|
|
|
|
|
|
def main() -> None:
|
|
if not os.path.isdir(DOCS_BASE):
|
|
raise Exception(f"No documentation found at {DOCS_BASE}; check out BerriAI/litellm-docs into docs/my-website")
|
|
|
|
undocumented_keys = undocumented_env_keys(repo_base, DOCS_BASE)
|
|
if undocumented_keys:
|
|
raise Exception(
|
|
f"Environment variables read under {repo_base} but mentioned nowhere in the docs: "
|
|
f"{sorted(undocumented_keys)}"
|
|
f"\nDocument each one, either on the relevant provider page or as a row in the "
|
|
f"'environment variables - Reference' table in {REFERENCE_TABLE_PATH}"
|
|
)
|
|
print(f"Every environment variable read under {repo_base} is documented")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|