mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-09 22:31:41 +00:00
fix(lint): Partly fix ANN401, S110, TID251, TRY300,UP028
This commit is contained in:
parent
91df9845ca
commit
069423ce00
4 changed files with 40 additions and 31 deletions
|
|
@ -1,7 +1,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from typing import TYPE_CHECKING, cast
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import httpx
|
||||
|
||||
|
|
@ -149,7 +149,7 @@ class GigaChatPassthroughConfig(BasePassthroughConfig):
|
|||
generic_chunk_has_all_required_fields,
|
||||
)
|
||||
from litellm.main import stream_chunk_builder
|
||||
from litellm.types.utils import GenericStreamingChunk, ModelResponseStream
|
||||
from litellm.types.utils import ModelResponseStream
|
||||
|
||||
all_translated_chunks = []
|
||||
|
||||
|
|
@ -179,8 +179,7 @@ class GigaChatPassthroughConfig(BasePassthroughConfig):
|
|||
|
||||
if isinstance(translated_chunk, dict) and generic_chunk_has_all_required_fields(translated_chunk):
|
||||
chunk_obj = convert_generic_chunk_to_model_response_stream(
|
||||
# cast-ok: validated TypedDict
|
||||
cast(GenericStreamingChunk, translated_chunk)
|
||||
translated_chunk # type: ignore[arg-type] # validated TypedDict
|
||||
)
|
||||
elif isinstance(translated_chunk, ModelResponseStream):
|
||||
chunk_obj = translated_chunk
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ from functools import partial
|
|||
from typing import Any, Final, cast
|
||||
|
||||
import httpx
|
||||
from httpx._types import CookieTypes, QueryParamTypes, RequestFiles
|
||||
from httpx._types import CookieTypes, QueryParamTypes, RequestContent, RequestFiles
|
||||
|
||||
from litellm._logging import verbose_logger
|
||||
from litellm.litellm_core_utils.get_llm_provider_logic import get_llm_provider
|
||||
|
|
@ -32,8 +32,7 @@ async def _as_async_generator(iterable: AsyncIterator[bytes]) -> AsyncGenerator[
|
|||
|
||||
|
||||
def _as_generator(iterable: Iterator[bytes]) -> Generator[bytes, Any, Any]:
|
||||
for chunk in iterable:
|
||||
yield chunk
|
||||
yield from iterable
|
||||
|
||||
|
||||
class AsyncPassthroughStreamingResponse(AsyncGenerator[Any, Any]):
|
||||
|
|
@ -88,7 +87,7 @@ class AsyncPassthroughStreamingResponse(AsyncGenerator[Any, Any]):
|
|||
except Exception: # noqa: BLE001 # Safe catch-all for cleanup logic
|
||||
try:
|
||||
await self._response.aclose()
|
||||
except Exception: # noqa: BLE001 # Safe catch-all for cleanup logic
|
||||
except Exception: # noqa: BLE001 S110 # Safe catch-all for cleanup logic
|
||||
pass
|
||||
raise
|
||||
return self
|
||||
|
|
@ -129,21 +128,27 @@ class AsyncPassthroughStreamingResponse(AsyncGenerator[Any, Any]):
|
|||
try:
|
||||
chunk = await anext(self._iterator)
|
||||
self._raw_bytes.append(chunk)
|
||||
return chunk
|
||||
except Exception: # noqa: BLE001 # Safe catch-all for cleanup logic
|
||||
self._start_flush()
|
||||
try:
|
||||
await self._response.aclose()
|
||||
except Exception: # noqa: BLE001 # Safe catch-all for cleanup logic
|
||||
except Exception: # noqa: BLE001 S110 # Safe catch-all for cleanup logic
|
||||
pass
|
||||
raise
|
||||
else:
|
||||
return chunk
|
||||
|
||||
async def asend(self, value: Any) -> bytes:
|
||||
async def asend(self, value: bytes) -> bytes:
|
||||
if not self._initialized:
|
||||
await self
|
||||
return await self._iterator.asend(value)
|
||||
|
||||
async def athrow(self, typ: Any, val: Any = None, tb: Any = None) -> bytes:
|
||||
async def athrow(
|
||||
self,
|
||||
typ: type[BaseException],
|
||||
val: BaseException | None = None,
|
||||
tb: type | None = None,
|
||||
) -> bytes:
|
||||
if not self._initialized:
|
||||
await self
|
||||
return await self._iterator.athrow(typ, val, tb)
|
||||
|
|
@ -154,7 +159,7 @@ class AsyncPassthroughStreamingResponse(AsyncGenerator[Any, Any]):
|
|||
if self._initialized:
|
||||
await self._iterator.aclose()
|
||||
await self._response.aclose()
|
||||
except Exception: # noqa: BLE001 # Safe catch-all for cleanup logic
|
||||
except Exception: # noqa: BLE001 S110 # Safe catch-all for cleanup logic
|
||||
pass
|
||||
|
||||
|
||||
|
|
@ -201,26 +206,32 @@ class PassthroughStreamingResponse(Generator[Any, Any, Any]):
|
|||
try:
|
||||
chunk = next(self._iterator)
|
||||
self._raw_bytes.append(chunk)
|
||||
return chunk
|
||||
except Exception: # noqa: BLE001 # Safe catch-all for cleanup logic
|
||||
self._start_flush()
|
||||
try:
|
||||
self._response.close()
|
||||
except Exception: # noqa: BLE001 # Safe catch-all for cleanup logic
|
||||
except Exception: # noqa: BLE001 S110 # Safe catch-all for cleanup logic
|
||||
pass
|
||||
raise
|
||||
else:
|
||||
return chunk
|
||||
|
||||
def send(self, value: Any) -> bytes:
|
||||
def send(self, value: bytes) -> bytes:
|
||||
return self._iterator.send(value)
|
||||
|
||||
def throw(self, typ: Any, val: Any = None, tb: Any = None) -> bytes:
|
||||
def throw(
|
||||
self,
|
||||
typ: type[BaseException],
|
||||
val: BaseException | None = None,
|
||||
tb: type | None = None,
|
||||
) -> bytes:
|
||||
return self._iterator.throw(typ, val, tb)
|
||||
|
||||
def close(self) -> None:
|
||||
self._start_flush()
|
||||
try:
|
||||
self._response.close()
|
||||
except Exception: # noqa: BLE001 # Safe catch-all for cleanup logic
|
||||
except Exception: # noqa: BLE001 S110 # Safe catch-all for cleanup logic
|
||||
pass
|
||||
|
||||
|
||||
|
|
@ -235,10 +246,10 @@ async def allm_passthrough_route(
|
|||
api_key: str | None = None,
|
||||
request_query_params: dict | None = None,
|
||||
request_headers: dict | None = None,
|
||||
content: Any | None = None,
|
||||
content: RequestContent | None = None,
|
||||
data: dict | None = None,
|
||||
files: RequestFiles | None = None,
|
||||
json: Any | None = None,
|
||||
json: object | None = None,
|
||||
params: QueryParamTypes | None = None,
|
||||
cookies: CookieTypes | None = None,
|
||||
client: HTTPHandler | AsyncHTTPHandler | None = None,
|
||||
|
|
@ -335,7 +346,7 @@ async def allm_passthrough_route(
|
|||
provider=LlmProviders(resolved_custom_llm_provider),
|
||||
model=model,
|
||||
)
|
||||
except Exception:
|
||||
except Exception: # noqa: BLE001 S110
|
||||
# If we can't get provider config, pass None
|
||||
pass
|
||||
|
||||
|
|
@ -360,10 +371,10 @@ def llm_passthrough_route(
|
|||
api_key: str | None = None,
|
||||
request_query_params: dict | None = None,
|
||||
request_headers: dict | None = None,
|
||||
content: Any | None = None,
|
||||
content: RequestContent | None = None,
|
||||
data: dict | None = None,
|
||||
files: RequestFiles | None = None,
|
||||
json: Any | None = None,
|
||||
json: object | None = None,
|
||||
params: QueryParamTypes | None = None,
|
||||
cookies: CookieTypes | None = None,
|
||||
client: HTTPHandler | AsyncHTTPHandler | None = None,
|
||||
|
|
|
|||
|
|
@ -1426,7 +1426,7 @@ class ProxyBaseLLMRequestProcessing:
|
|||
|
||||
@staticmethod
|
||||
def _merge_passthrough_streaming_headers(
|
||||
response_headers: Any | None,
|
||||
response_headers: httpx.Headers | dict | None,
|
||||
custom_headers: dict,
|
||||
) -> dict:
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -2888,13 +2888,6 @@ async def handle_gigachat_passthrough_router_model(
|
|||
user_api_base=user_api_base,
|
||||
version=version,
|
||||
)
|
||||
|
||||
if isinstance(result, StreamingResponse):
|
||||
if result.headers.get("Content-Type") is None:
|
||||
result.headers["Content-Type"] = "text/event-stream; charset=utf-8"
|
||||
return result
|
||||
|
||||
return result
|
||||
except Exception as e: # noqa: BLE001 # Safe catch-all for handle exception
|
||||
# Use common exception handling
|
||||
raise await base_llm_response_processor._handle_llm_api_exception(
|
||||
|
|
@ -2902,6 +2895,12 @@ async def handle_gigachat_passthrough_router_model(
|
|||
user_api_key_dict=user_api_key_dict,
|
||||
proxy_logging_obj=proxy_logging_obj,
|
||||
)
|
||||
else:
|
||||
if isinstance(result, StreamingResponse):
|
||||
if result.headers.get("Content-Type") is None:
|
||||
result.headers["Content-Type"] = "text/event-stream; charset=utf-8"
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@router.api_route(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue