Skip to main content

get_request_handler

Creates and returns an asynchronous request handler function that manages the lifecycle of a FastAPI endpoint, including dependency resolution, request body parsing, and response serialization. It handles various response types such as standard JSON, Server-Sent Events (SSE), and streaming JSONL while ensuring proper cleanup of resources via an internal exit stack.

def get_request_handler(
dependant: Dependant,
body_field: ModelField | None,
status_code: int | None,
response_class: type[Response] | DefaultPlaceholder,
response_field: ModelField | None,
response_model_include: IncEx | None,
response_model_exclude: IncEx | None,
response_model_by_alias: bool,
response_model_exclude_unset: bool,
response_model_exclude_defaults: bool,
response_model_exclude_none: bool,
dependency_overrides_provider: Any | None,
embed_body_fields: bool,
strict_content_type: bool | DefaultPlaceholder,
stream_item_field: ModelField | None,
is_json_stream: bool
) - > Callable[[Request], Coroutine[Any, Any, Response]]

Builds and returns an asynchronous request handler function that manages the full lifecycle of an HTTP request, including dependency injection, body parsing, and response serialization. It handles various response types such as standard JSON, Server-Sent Events (SSE), and JSONL streaming while ensuring proper cleanup of resources via an exit stack.

Parameters

NameTypeDescription
dependantDependantThe dependency graph and metadata for the endpoint, including the actual function to call and its requirements.
body_field`ModelFieldNone`
status_code`intNone`
response_class`type[Response]DefaultPlaceholder`
response_field`ModelFieldNone`
response_model_include`IncExNone`
response_model_exclude`IncExNone`
response_model_by_aliasboolWhether to use the field's alias name for serialization.
response_model_exclude_unsetboolWhether to exclude fields that were not explicitly set in the model.
response_model_exclude_defaultsboolWhether to exclude fields that have their default values.
response_model_exclude_noneboolWhether to exclude fields that have a value of None.
dependency_overrides_provider`AnyNone`
embed_body_fieldsboolWhether to embed single body fields in a JSON object instead of treating them as the root.
strict_content_type`boolDefaultPlaceholder`
stream_item_field`ModelFieldNone`
is_json_streamboolWhether the response should be streamed as newline-delimited JSON (JSONL).

Returns

TypeDescription
Callable[[Request], Coroutine[Any, Any, Response]]An asynchronous function that accepts a Request and returns a Response, suitable for use as a FastAPI route handler.