From 7dba50da07163b1a211c8e4fa0c72a2ad0e8271e Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 23 May 2026 09:45:57 -0700 Subject: [PATCH] fix(mypy): narrow model_name to str in cost-injection branch The hoisted cost_injection_active flag in chunk_processor encodes the `bool(model_name)` requirement but mypy can't track that invariant through the local, so the per-chunk `_process_chunk_with_cost_injection( chunk, model_name)` calls flagged Optional[str] vs str. Pin a typed non-None local inside the cost-injection branch so mypy narrows correctly without changing runtime behavior. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../proxy/pass_through_endpoints/streaming_handler.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/litellm/proxy/pass_through_endpoints/streaming_handler.py b/litellm/proxy/pass_through_endpoints/streaming_handler.py index 6ad35ce5381..235a38b75f9 100644 --- a/litellm/proxy/pass_through_endpoints/streaming_handler.py +++ b/litellm/proxy/pass_through_endpoints/streaming_handler.py @@ -61,18 +61,23 @@ class PassThroughStreamingHandler: raw_bytes.append(chunk) yield chunk else: + # ``cost_injection_active`` already requires ``model_name`` to + # be truthy; pin to a typed local so mypy narrows ``Optional[str]`` + # -> ``str`` for the per-chunk call site. + assert model_name is not None + resolved_model_name: str = model_name async for chunk in response.aiter_bytes(): raw_bytes.append(chunk) if endpoint_type == EndpointType.VERTEX_AI: if "streamRawPredict" in url_route or "rawPredict" in url_route: modified_chunk = ProxyBaseLLMRequestProcessing._process_chunk_with_cost_injection( - chunk, model_name + chunk, resolved_model_name ) if modified_chunk is not None: chunk = modified_chunk else: # EndpointType.ANTHROPIC modified_chunk = ProxyBaseLLMRequestProcessing._process_chunk_with_cost_injection( - chunk, model_name + chunk, resolved_model_name ) if modified_chunk is not None: chunk = modified_chunk