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
| Name | Type | Description |
|---|---|---|
| dependant | Dependant | The dependency graph and metadata for the endpoint, including the actual function to call and its requirements. |
| 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 | Whether to use the field's alias name for serialization. |
| response_model_exclude_unset | bool | Whether to exclude fields that were not explicitly set in the model. |
| response_model_exclude_defaults | bool | Whether to exclude fields that have their default values. |
| response_model_exclude_none | bool | Whether to exclude fields that have a value of None. |
| dependency_overrides_provider | `Any | None` |
| embed_body_fields | bool | Whether to embed single body fields in a JSON object instead of treating them as the root. |
| strict_content_type | `bool | DefaultPlaceholder` |
| stream_item_field | `ModelField | None` |
| is_json_stream | bool | Whether the response should be streamed as newline-delimited JSON (JSONL). |
Returns
| Type | Description |
|---|---|
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. |