Async token streaming, straight off the Anthropic SSE response.
(
self,
model: str,
messages: list[dict[str, Any]],
api_base: str | None = None,
custom_prompt_dict: dict[str, Any] | None = None,
model_response: ModelResponse | None = None,
print_verbose: Any = None,
encoding: Any = None,
logging_obj: Any = None,
optional_params: dict[str, Any] | None = None,
acompletion: bool | None = None,
timeout: float | None = None,
litellm_params: dict[str, Any] | None = None,
logger_fn: Any = None,
headers: dict[str, str] | None = None,
**kwargs: Any,
)
| 987 | return |
| 988 | |
| 989 | async def astreaming( |
| 990 | self, |
| 991 | model: str, |
| 992 | messages: list[dict[str, Any]], |
| 993 | api_base: str | None = None, |
| 994 | custom_prompt_dict: dict[str, Any] | None = None, |
| 995 | model_response: ModelResponse | None = None, |
| 996 | print_verbose: Any = None, |
| 997 | encoding: Any = None, |
| 998 | logging_obj: Any = None, |
| 999 | optional_params: dict[str, Any] | None = None, |
| 1000 | acompletion: bool | None = None, |
| 1001 | timeout: float | None = None, |
| 1002 | litellm_params: dict[str, Any] | None = None, |
| 1003 | logger_fn: Any = None, |
| 1004 | headers: dict[str, str] | None = None, |
| 1005 | **kwargs: Any, |
| 1006 | ) -> AsyncIterator[dict[str, Any]]: |
| 1007 | """Async token streaming, straight off the Anthropic SSE response.""" |
| 1008 | body_str, api_url, _ = self._build_anthropic_request( |
| 1009 | model, messages, optional_params, api_base, stream=True |
| 1010 | ) |
| 1011 | async with async_client(timeout=timeout or 600) as client: |
| 1012 | for force_refresh in (False, True): |
| 1013 | req_headers = _build_headers(get_access_token(force_refresh=force_refresh)) |
| 1014 | async with client.stream( |
| 1015 | "POST", |
| 1016 | f"{api_url}/v1/messages?beta=true", |
| 1017 | content=body_str, |
| 1018 | headers=req_headers, |
| 1019 | ) as resp: |
| 1020 | if resp.status_code == 401 and not force_refresh: |
| 1021 | await resp.aread() |
| 1022 | continue |
| 1023 | await _araise_for_stream_status(resp, model) |
| 1024 | # The SSE parser is a pure sync generator over decoded lines; |
| 1025 | # drive it by feeding one line at a time so nothing buffers. |
| 1026 | feed = _AnthropicSseAccumulator() |
| 1027 | async for line in resp.aiter_lines(): |
| 1028 | for chunk in feed.push(line): |
| 1029 | yield chunk |
| 1030 | for chunk in feed.close(): |
| 1031 | yield chunk |
| 1032 | return |
| 1033 | |
| 1034 | |
| 1035 | def _raise_for_stream_status(resp: httpx.Response, model: str) -> None: |
nothing calls this directly
no test coverage detected