MCPcopy Create free account
hub / github.com/BerriAI/litellm / create_response

Function create_response

litellm/proxy/common_request_processing.py:1460–1593  ·  view source on GitHub ↗

Create streaming response, checking if the first chunk is an error. If the first chunk is an error, return a standard JSON error response. Otherwise, return StreamingResponse and stream all content. ``refresh_headers`` is consulted once the first chunk has been buffered, for ca

(
    generator: AsyncGenerator[str, None] | Coroutine[object, object, AsyncGenerator[str, None]],
    media_type: str,
    headers: Mapping[str, str],
    default_status_code: int = status.HTTP_200_OK,
    request: Request | None = None,
    refresh_headers: Callable[[], Awaitable[Mapping[str, str]]] | None = None,
    general_settings: Mapping[str, object] = _NO_GENERAL_SETTINGS,
    framing: FirstChunkSseFraming = OPENAI_FIRST_CHUNK_FRAMING,
)

Source from the content-addressed store, hash-verified

1458
1459
1460async def create_response(
1461 generator: AsyncGenerator[str, None] | Coroutine[object, object, AsyncGenerator[str, None]],
1462 media_type: str,
1463 headers: Mapping[str, str],
1464 default_status_code: int = status.HTTP_200_OK,
1465 request: Request | None = None,
1466 refresh_headers: Callable[[], Awaitable[Mapping[str, str]]] | None = None,
1467 general_settings: Mapping[str, object] = _NO_GENERAL_SETTINGS,
1468 framing: FirstChunkSseFraming = OPENAI_FIRST_CHUNK_FRAMING,
1469) -> StreamingResponse | JSONResponse:
1470 """
1471 Create streaming response, checking if the first chunk is an error.
1472 If the first chunk is an error, return a standard JSON error response.
1473 Otherwise, return StreamingResponse and stream all content.
1474
1475 ``refresh_headers`` is consulted once the first chunk has been buffered, for
1476 callers whose headers can only be known then. ``general_settings`` decides whether
1477 the first-chunk error body also carries the ``x-litellm-call-id`` header's value.
1478 """
1479 first_chunk_value: str | None = None
1480 final_status_code = default_status_code
1481
1482 try:
1483 # Handle coroutine that returns a generator
1484 if asyncio.iscoroutine(generator):
1485 generator = await generator
1486
1487 # Now get the first chunk from the actual generator
1488 first_chunk_value = await _buffer_first_chunk_honoring_disconnect(generator, request)
1489 resolved_headers: Final = await _resolve_stream_headers(headers, refresh_headers)
1490
1491 if isinstance(first_chunk_value, AnthropicErrorSseFrame):
1492 with contextlib.suppress(Exception):
1493 await generator.aclose()
1494 return JSONResponse(
1495 status_code=first_chunk_value.status_code,
1496 content=first_chunk_value.json_body(
1497 error_body_call_id(general_settings, resolved_headers.get(LITELLM_CALL_ID_HEADER))
1498 ),
1499 headers=resolved_headers,
1500 )
1501
1502 if first_chunk_value is not None:
1503 try:
1504 error_code_from_chunk: Final = await _parse_event_data_for_error(
1505 first_chunk_value, framing.data_payload
1506 )
1507 if error_code_from_chunk is not None and error_code_from_chunk >= framing.lowest_error_status:
1508 # First chunk is an error, stream hasn't really started yet
1509 # Should return standard JSON error response instead of SSE format
1510 final_status_code = error_code_from_chunk
1511 verbose_proxy_logger.debug(
1512 "Error detected in first stream chunk. Returning JSON error response with status code: %s",
1513 final_status_code,
1514 )
1515
1516 # Parse error content
1517 error_dict: Final = with_call_id(

Calls 15

error_body_call_idFunction · 0.90
with_call_idFunction · 0.90
_resolve_stream_headersFunction · 0.85
StreamingResponseClass · 0.85
empty_genFunction · 0.85
_sse_stream_headersFunction · 0.85
sse_error_payloadFunction · 0.85
error_gen_messageFunction · 0.85