← eval5_submission_v1s

fastapi_14964

resolved RESOLVED LOOP PASS · 21 tool calls · 201 s · fastapi/fastapi

Task input

🗑️ Deprecate `ORJSONResponse` and `UJSONResponse`

🗑️ Deprecate `ORJSONResponse` and `UJSONResponse`

Now that there's better performance by default, with response models: https://github.com/fastapi/fastapi/pull/14962

This removes `ujson` and `orjson` from the `"fastapi[all]"` extras.

To use these responses, `ujson` or `orjson` need to be explicitly installed.

Tool calls (22)

#ToolArgumentsResult
1run_command
{
 "command": "grep -rn \"ORJSONResponse\" fastapi | head -20"
}
{"status": "ok", "stdout": "fastapi/applications.py:369: from fastapi.responses import ORJSONResponse\nfastapi/applications.py:371: app = FastAPI(default_respon
full
{"status": "ok", "stdout": "fastapi/applications.py:369:                from fastapi.responses import ORJSONResponse\nfastapi/applications.py:371:                app = FastAPI(default_response_class=ORJSONResponse)\nfastapi/applications.py:1461:                from fastapi.responses import ORJSONResponse\nfastapi/applications.py:1469:                    default_response_class=ORJSONResponse,\nfastapi/responses.py:36:class ORJSONResponse(JSONResponse):\nfastapi/responses.py:45:        assert orjson is not None, \"orjson must be installed to use ORJSONResponse\"\n", "stderr": "", "exit_code": 0}
2run_command
{
 "command": "grep -rn \"UJSONResponse\" fastapi | head -20"
}
{"status": "ok", "stdout": "fastapi/responses.py:23:class UJSONResponse(JSONResponse):\nfastapi/responses.py:32: assert ujson is not None, \"ujson must be insta
full
{"status": "ok", "stdout": "fastapi/responses.py:23:class UJSONResponse(JSONResponse):\nfastapi/responses.py:32:        assert ujson is not None, \"ujson must be installed to use UJSONResponse\"\n", "stderr": "", "exit_code": 0}
3-5 ×3read_file
{
 "filepath": "fastapi/responses.py",
 "start_line": "20",
 "end_line": "50"
}
{"status": "error", "error_type": "FileReadError", "error_message": "'>' not supported between instances of 'int' and 'str'"}
full
{"status": "error", "error_type": "FileReadError", "error_message": "'>' not supported between instances of 'int' and 'str'"}
6read_file
{
 "filepath": "fastapi/responses.py"
}
{"status": "ok", "filepath": "fastapi/responses.py", "content": "from typing import Any\n\nfrom starlette.responses import FileResponse as FileResponse # noqa\n
full
{"status": "ok", "filepath": "fastapi/responses.py", "content": "from typing import Any\n\nfrom starlette.responses import FileResponse as FileResponse  # noqa\nfrom starlette.responses import HTMLResponse as HTMLResponse  # noqa\nfrom starlette.responses import JSONResponse as JSONResponse  # noqa\nfrom starlette.responses import PlainTextResponse as PlainTextResponse  # noqa\nfrom starlette.responses import RedirectResponse as RedirectResponse  # noqa\nfrom starlette.responses import Response as Response  # noqa\nfrom starlette.responses import StreamingResponse as StreamingResponse  # noqa\n\ntry:\n    import ujson\nexcept ImportError:  # pragma: nocover\n    ujson = None  # type: ignore\n\n\ntry:\n    import orjson\nexcept ImportError:  # pragma: nocover\n    orjson = None  # type: ignore\n\n\nclass UJSONResponse(JSONResponse):\n    \"\"\"\n    JSON response using the high-performance ujson library to serialize data to JSON.\n\n    Read more about it in the\n    [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/).\n    \"\"\"\n\n    def render(self, content: Any) -> bytes:\n        assert ujson is not None, \"ujson must be installed to use UJSONResponse\"\n        return ujson.dumps(content, ensure_ascii=False).encode(\"utf-8\")\n\n\nclass ORJSONResponse(JSONResponse):\n    \"\"\"\n    JSON response using the high-performance orjson library to serialize data to JSON.\n\n    Read more about it in the\n    [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/).\n    \"\"\"\n\n    def render(self, content: Any) -> bytes:\n        assert orjson is not None, \"orjson must be installed to use ORJSONResponse\"\n        return orjson.dumps(\n            content, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY\n        )", "start_line": 1, "end_line": 48, "total_lines": 48, "is_truncated": false}
7run_command
{
 "command": "grep -rn \"warnings\" fastapi | head -20"
}
{"status": "ok", "stdout": "fastapi/_compat/shared.py:3:import warnings\nfastapi/_compat/shared.py:182: with warnings.catch_warnings():\nfastapi/_compat/shared.
full
{"status": "ok", "stdout": "fastapi/_compat/shared.py:3:import warnings\nfastapi/_compat/shared.py:182:        with warnings.catch_warnings():\nfastapi/_compat/shared.py:183:            warnings.simplefilter(\"ignore\", UserWarning)\nfastapi/_compat/shared.py:194:        with warnings.catch_warnings():\nfastapi/_compat/shared.py:195:            warnings.simplefilter(\"ignore\", UserWarning)\nfastapi/_compat/v2.py:2:import warnings\nfastapi/_compat/v2.py:129:        with warnings.catch_warnings():\nfastapi/_compat/v2.py:135:                from pydantic.warnings import UnsupportedFieldAttributeWarning\nfastapi/_compat/v2.py:137:                warnings.simplefilter(\nfastapi/exceptions.py:255:    Ref: https://sethmlarson.dev/deprecations-via-warnings-dont-work-for-python-libraries\nfastapi/utils.py:2:import warnings\nfastapi/utils.py:83:    warnings.warn(\nfastapi/params.py:1:import warnings\nfastapi/params.py:76:            warnings.warn(\nfastapi/params.py:106:            warnings.warn(\nfastapi/params.py:521:            warnings.warn(\nfastapi/params.py:551:            warnings.warn(\nfastapi/openapi/utils.py:4:import warnings\nfastapi/openapi/utils.py:218:    warnings.warn(\nfastapi/openapi/utils.py:254:        warnings.warn(message, stacklevel=1)\n", "stderr": "", "exit_code": 0}
8read_file
{
 "filepath": "fastapi/utils.py"
}
{"status": "ok", "filepath": "fastapi/utils.py", "content": "import re\nimport warnings\nfrom typing import (\n TYPE_CHECKING,\n Any,\n Literal,\n)\n\nimport fa
full
{"status": "ok", "filepath": "fastapi/utils.py", "content": "import re\nimport warnings\nfrom typing import (\n    TYPE_CHECKING,\n    Any,\n    Literal,\n)\n\nimport fastapi\nfrom fastapi._compat import (\n    ModelField,\n    PydanticSchemaGenerationError,\n    Undefined,\n    annotation_is_pydantic_v1,\n)\nfrom fastapi.datastructures import DefaultPlaceholder, DefaultType\nfrom fastapi.exceptions import FastAPIDeprecationWarning, PydanticV1NotSupportedError\nfrom pydantic.fields import FieldInfo\n\nfrom ._compat import v2\n\nif TYPE_CHECKING:  # pragma: nocover\n    from .routing import APIRoute\n\n\ndef is_body_allowed_for_status_code(status_code: int | str | None) -> bool:\n    if status_code is None:\n        return True\n    # Ref: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#patterned-fields-1\n    if status_code in {\n        \"default\",\n        \"1XX\",\n        \"2XX\",\n        \"3XX\",\n        \"4XX\",\n        \"5XX\",\n    }:\n        return True\n    current_status_code = int(status_code)\n    return not (current_status_code < 200 or current_status_code in {204, 205, 304})\n\n\ndef get_path_param_names(path: str) -> set[str]:\n    return set(re.findall(\"{(.*?)}\", path))\n\n\n_invalid_args_message = (\n    \"Invalid args for response field! Hint: \"\n    \"check that {type_} is a valid Pydantic field type. \"\n    \"If you are using a return type annotation that is not a valid Pydantic \"\n    \"field (e.g. Union[Response, dict, None]) you can disable generating the \"\n    \"response model from the type annotation with the path operation decorator \"\n    \"parameter response_model=None. Read more: \"\n    \"https://fastapi.tiangolo.com/tutorial/response-model/\"\n)\n\n\ndef create_model_field(\n    name: str,\n    type_: Any,\n    default: Any | None = Undefined,\n    field_info: FieldInfo | None = None,\n    alias: str | None = None,\n    mode: Literal[\"validation\", \"serialization\"] = \"validation\",\n) -> Model
9read_file
{
 "filepath": "fastapi/exceptions.py"
}
{"status": "ok", "filepath": "fastapi/exceptions.py", "content": "from collections.abc import Mapping, Sequence\nfrom typing import Annotated, Any, TypedDict\n\
full
{"status": "ok", "filepath": "fastapi/exceptions.py", "content": "from collections.abc import Mapping, Sequence\nfrom typing import Annotated, Any, TypedDict\n\nfrom annotated_doc import Doc\nfrom pydantic import BaseModel, create_model\nfrom starlette.exceptions import HTTPException as StarletteHTTPException\nfrom starlette.exceptions import WebSocketException as StarletteWebSocketException\n\n\nclass EndpointContext(TypedDict, total=False):\n    function: str\n    path: str\n    file: str\n    line: int\n\n\nclass HTTPException(StarletteHTTPException):\n    \"\"\"\n    An HTTP exception you can raise in your own code to show errors to the client.\n\n    This is for client errors, invalid authentication, invalid data, etc. Not for server\n    errors in your code.\n\n    Read more about it in the\n    [FastAPI docs for Handling Errors](https://fastapi.tiangolo.com/tutorial/handling-errors/).\n\n    ## Example\n\n    ```python\n    from fastapi import FastAPI, HTTPException\n\n    app = FastAPI()\n\n    items = {\"foo\": \"The Foo Wrestlers\"}\n\n\n    @app.get(\"/items/{item_id}\")\n    async def read_item(item_id: str):\n        if item_id not in items:\n            raise HTTPException(status_code=404, detail=\"Item not found\")\n        return {\"item\": items[item_id]}\n    ```\n    \"\"\"\n\n    def __init__(\n        self,\n        status_code: Annotated[\n            int,\n            Doc(\n                \"\"\"\n                HTTP status code to send to the client.\n\n                Read more about it in the\n                [FastAPI docs for Handling Errors](https://fastapi.tiangolo.com/tutorial/handling-errors/#use-httpexception)\n                \"\"\"\n            ),\n        ],\n        detail: Annotated[\n            Any,\n            Doc(\n                \"\"\"\n                Any data to be sent to the client in the `detail` key of the JSON\n                response.\n\n                Read more about it in the\n                [FastAPI docs for
10read_file
{
 "filepath": "fastapi/exceptions.py",
 "start_line": "200"
}
{"status": "error", "error_type": "FileReadError", "error_message": "'>' not supported between instances of 'int' and 'str'"}
full
{"status": "error", "error_type": "FileReadError", "error_message": "'>' not supported between instances of 'int' and 'str'"}
11read_file
{
 "filepath": "fastapi/exceptions.py",
 "start_line": "200",
 "end_line": "260"
}
{"status": "error", "error_type": "FileReadError", "error_message": "'>' not supported between instances of 'int' and 'str'"}
full
{"status": "error", "error_type": "FileReadError", "error_message": "'>' not supported between instances of 'int' and 'str'"}
12read_file
{
 "filepath": "fastapi/exceptions.py"
}
{"status": "ok", "filepath": "fastapi/exceptions.py", "content": "from collections.abc import Mapping, Sequence\nfrom typing import Annotated, Any, TypedDict\n\
full
{"status": "ok", "filepath": "fastapi/exceptions.py", "content": "from collections.abc import Mapping, Sequence\nfrom typing import Annotated, Any, TypedDict\n\nfrom annotated_doc import Doc\nfrom pydantic import BaseModel, create_model\nfrom starlette.exceptions import HTTPException as StarletteHTTPException\nfrom starlette.exceptions import WebSocketException as StarletteWebSocketException\n\n\nclass EndpointContext(TypedDict, total=False):\n    function: str\n    path: str\n    file: str\n    line: int\n\n\nclass HTTPException(StarletteHTTPException):\n    \"\"\"\n    An HTTP exception you can raise in your own code to show errors to the client.\n\n    This is for client errors, invalid authentication, invalid data, etc. Not for server\n    errors in your code.\n\n    Read more about it in the\n    [FastAPI docs for Handling Errors](https://fastapi.tiangolo.com/tutorial/handling-errors/).\n\n    ## Example\n\n    ```python\n    from fastapi import FastAPI, HTTPException\n\n    app = FastAPI()\n\n    items = {\"foo\": \"The Foo Wrestlers\"}\n\n\n    @app.get(\"/items/{item_id}\")\n    async def read_item(item_id: str):\n        if item_id not in items:\n            raise HTTPException(status_code=404, detail=\"Item not found\")\n        return {\"item\": items[item_id]}\n    ```\n    \"\"\"\n\n    def __init__(\n        self,\n        status_code: Annotated[\n            int,\n            Doc(\n                \"\"\"\n                HTTP status code to send to the client.\n\n                Read more about it in the\n                [FastAPI docs for Handling Errors](https://fastapi.tiangolo.com/tutorial/handling-errors/#use-httpexception)\n                \"\"\"\n            ),\n        ],\n        detail: Annotated[\n            Any,\n            Doc(\n                \"\"\"\n                Any data to be sent to the client in the `detail` key of the JSON\n                response.\n\n                Read more about it in the\n                [FastAPI docs for
13run_command
{
 "command": "grep -n \"FastAPIDeprecationWarning\" fastapi/exceptions.py"
}
{"status": "ok", "stdout": "252:class FastAPIDeprecationWarning(UserWarning):\n", "stderr": "", "exit_code": 0}
full
{"status": "ok", "stdout": "252:class FastAPIDeprecationWarning(UserWarning):\n", "stderr": "", "exit_code": 0}
14read_file
{
 "end_line": "255",
 "filepath": "fastapi/exceptions.py",
 "start_line": "250"
}
{"status": "error", "error_type": "FileReadError", "error_message": "'>' not supported between instances of 'int' and 'str'"}
full
{"status": "error", "error_type": "FileReadError", "error_message": "'>' not supported between instances of 'int' and 'str'"}
15read_file
{
 "filepath": "fastapi/exceptions.py"
}
{"status": "ok", "filepath": "fastapi/exceptions.py", "content": "from collections.abc import Mapping, Sequence\nfrom typing import Annotated, Any, TypedDict\n\
full
{"status": "ok", "filepath": "fastapi/exceptions.py", "content": "from collections.abc import Mapping, Sequence\nfrom typing import Annotated, Any, TypedDict\n\nfrom annotated_doc import Doc\nfrom pydantic import BaseModel, create_model\nfrom starlette.exceptions import HTTPException as StarletteHTTPException\nfrom starlette.exceptions import WebSocketException as StarletteWebSocketException\n\n\nclass EndpointContext(TypedDict, total=False):\n    function: str\n    path: str\n    file: str\n    line: int\n\n\nclass HTTPException(StarletteHTTPException):\n    \"\"\"\n    An HTTP exception you can raise in your own code to show errors to the client.\n\n    This is for client errors, invalid authentication, invalid data, etc. Not for server\n    errors in your code.\n\n    Read more about it in the\n    [FastAPI docs for Handling Errors](https://fastapi.tiangolo.com/tutorial/handling-errors/).\n\n    ## Example\n\n    ```python\n    from fastapi import FastAPI, HTTPException\n\n    app = FastAPI()\n\n    items = {\"foo\": \"The Foo Wrestlers\"}\n\n\n    @app.get(\"/items/{item_id}\")\n    async def read_item(item_id: str):\n        if item_id not in items:\n            raise HTTPException(status_code=404, detail=\"Item not found\")\n        return {\"item\": items[item_id]}\n    ```\n    \"\"\"\n\n    def __init__(\n        self,\n        status_code: Annotated[\n            int,\n            Doc(\n                \"\"\"\n                HTTP status code to send to the client.\n\n                Read more about it in the\n                [FastAPI docs for Handling Errors](https://fastapi.tiangolo.com/tutorial/handling-errors/#use-httpexception)\n                \"\"\"\n            ),\n        ],\n        detail: Annotated[\n            Any,\n            Doc(\n                \"\"\"\n                Any data to be sent to the client in the `detail` key of the JSON\n                response.\n\n                Read more about it in the\n                [FastAPI docs for
16run_command
{
 "command": "tail -n 60 fastapi/exceptions.py"
}
{"status": "ok", "stdout": " return \"\"\n\n context = f'\\n File \"{self.endpoint_file}\", line {self.endpoint_line}, in {self.endpoint_function}'\n if self.en
full
{"status": "ok", "stdout": "            return \"\"\n\n        context = f'\\n  File \"{self.endpoint_file}\", line {self.endpoint_line}, in {self.endpoint_function}'\n        if self.endpoint_path:\n            context += f\"\\n    {self.endpoint_path}\"\n        return context\n\n    def __str__(self) -> str:\n        message = f\"{len(self._errors)} validation error{'s' if len(self._errors) != 1 else ''}:\\n\"\n        for err in self._errors:\n            message += f\"  {err}\\n\"\n        message += self._format_endpoint_context()\n        return message.rstrip()\n\n\nclass RequestValidationError(ValidationException):\n    def __init__(\n        self,\n        errors: Sequence[Any],\n        *,\n        body: Any = None,\n        endpoint_ctx: EndpointContext | None = None,\n    ) -> None:\n        super().__init__(errors, endpoint_ctx=endpoint_ctx)\n        self.body = body\n\n\nclass WebSocketRequestValidationError(ValidationException):\n    def __init__(\n        self,\n        errors: Sequence[Any],\n        *,\n        endpoint_ctx: EndpointContext | None = None,\n    ) -> None:\n        super().__init__(errors, endpoint_ctx=endpoint_ctx)\n\n\nclass ResponseValidationError(ValidationException):\n    def __init__(\n        self,\n        errors: Sequence[Any],\n        *,\n        body: Any = None,\n        endpoint_ctx: EndpointContext | None = None,\n    ) -> None:\n        super().__init__(errors, endpoint_ctx=endpoint_ctx)\n        self.body = body\n\n\nclass PydanticV1NotSupportedError(FastAPIError):\n    \"\"\"\n    A pydantic.v1 model is used, which is no longer supported.\n    \"\"\"\n\n\nclass FastAPIDeprecationWarning(UserWarning):\n    \"\"\"\n    A custom deprecation warning as DeprecationWarning is ignored\n    Ref: https://sethmlarson.dev/deprecations-via-warnings-dont-work-for-python-libraries\n    \"\"\"\n", "stderr": "", "exit_code": 0}
17edit_file
{
 "filepath": "fastapi/responses.py",
 "new_string": "from typing import Any\n\nfrom starlette.responses import FileResponse as FileResponse  # noqa\nfrom starlette.responses import HTMLResponse as HTMLResponse  # noqa\nfrom starlette.responses import JSONResponse as JSONResponse  # noqa\nfrom starlette.responses import PlainTextResponse as PlainTextResponse  # noqa\nfrom starlette.responses import RedirectResponse as RedirectResponse  # noqa\nfrom starlette.responses import Response as Response  # noqa\nfrom starlette.responses import StreamingResponse as StreamingResponse  # noqa\n\nimport warnings\nfrom fastapi.exceptions import FastAPIDeprecationWarning\n\ntry:\n    import ujson\nexcept ImportError:  # pragma: nocover\n    ujson = None  # type: ignore\n\n\ntry:\n    import orjson\nexcept ImportError:  # pragma: nocover\n    orjson = None  # type: ignore\n\n\nclass UJSONResponse(JSONResponse):\n    \\\"\\\"\\\"\n    JSON response using the high-performance ujson library to serialize data to JSON.\n\n    Read more about it in the\n    [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/).\n    \\\"\\\"\\\"\n\n    def render(self, content: Any) -> bytes:\n        warnings.warn(\n            message=\\\"UJSONResponse is deprecated. Use JSONResponse instead.\\\",\n            category=FastAPIDeprecationWarning,\n            stacklevel=2,\n        )\n        assert ujson is not None, \\\"ujson must be installed to use UJSONResponse\\\"\n        return ujson.dumps(content, ensure_ascii=False).encode(\\\"utf-8\\\")\n\n\nclass ORJSONResponse(JSONResponse):\n    \\\"\\\"\\\"\n    JSON response using the high-performance orjson library to serialize data to JSON.\n\n    Read more about it in the\n    [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/).\n    \\\"\\\"\\\"\n\n    def render(self, content: Any) -> bytes:\n        warnings.warn(\n            message=\\\"ORJSONResponse is deprecated. Use JSONResponse instead.\\\",\n            category=FastAPIDeprecationWarning,\n            stacklevel=2,\n        )\n        assert orjson is not None, \\\"orjson must be installed to use ORJSONResponse\\\"\n        return orjson.dumps(\n            content, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY\n        )",
 "old_string": "from typing import Any\n\nfrom starlette.responses import FileResponse as FileResponse  # noqa\nfrom starlette.responses import HTMLResponse as HTMLResponse  # noqa\nfrom starlette.responses import JSONResponse as JSONResponse  # noqa\nfrom starlette.responses import PlainTextResponse as PlainTextResponse  # noqa\nfrom starlette.responses import RedirectResponse as RedirectResponse  # noqa\nfrom starlette.responses import Response as Response  # noqa\nfrom starlette.responses import StreamingResponse as StreamingResponse  # noqa\n\ntry:\n    import ujson\nexcept ImportError:  # pragma: nocover\n    ujson = None  # type: ignore\n\n\ntry:\n    import orjson\nexcept ImportError:  # pragma: nocover\n    orjson = None  # type: ignore\n\n\nclass UJSONResponse(JSONResponse):\n    \\\"\\\"\\\"\n    JSON response using the high-performance ujson library to serialize data to JSON.\n\n    Read more about it in the\n    [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/).\n    \\\"\\\"\\\"\n\n    def render(self, content: Any) -> bytes:\n        assert ujson is not None, \\\"ujson must be installed to use UJSONResponse\\\"\n        return ujson.dumps(content, ensure_ascii=False).encode(\\\"utf-8\\\")\n\n\nclass ORJSONResponse(JSONResponse):\n    \\\"\\\"\\\"\n    JSON response using the high-performance orjson library to serialize data to JSON.\n\n    Read more about it in the\n    [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/).\n    \\\"\\\"
{"status": "error", "error_type": "FileEditError", "error_message": "Failed to replace: old_string not found. Ensure you're not escaping content incorrectly and
full
{"status": "error", "error_type": "FileEditError", "error_message": "Failed to replace: old_string not found. Ensure you're not escaping content incorrectly and check whitespace, indentation, and context."}
18read_file
{
 "filepath": "fastapi/responses.py"
}
{"status": "ok", "filepath": "fastapi/responses.py", "content": "from typing import Any\n\nfrom starlette.responses import FileResponse as FileResponse # noqa\n
full
{"status": "ok", "filepath": "fastapi/responses.py", "content": "from typing import Any\n\nfrom starlette.responses import FileResponse as FileResponse  # noqa\nfrom starlette.responses import HTMLResponse as HTMLResponse  # noqa\nfrom starlette.responses import JSONResponse as JSONResponse  # noqa\nfrom starlette.responses import PlainTextResponse as PlainTextResponse  # noqa\nfrom starlette.responses import RedirectResponse as RedirectResponse  # noqa\nfrom starlette.responses import Response as Response  # noqa\nfrom starlette.responses import StreamingResponse as StreamingResponse  # noqa\n\ntry:\n    import ujson\nexcept ImportError:  # pragma: nocover\n    ujson = None  # type: ignore\n\n\ntry:\n    import orjson\nexcept ImportError:  # pragma: nocover\n    orjson = None  # type: ignore\n\n\nclass UJSONResponse(JSONResponse):\n    \"\"\"\n    JSON response using the high-performance ujson library to serialize data to JSON.\n\n    Read more about it in the\n    [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/).\n    \"\"\"\n\n    def render(self, content: Any) -> bytes:\n        assert ujson is not None, \"ujson must be installed to use UJSONResponse\"\n        return ujson.dumps(content, ensure_ascii=False).encode(\"utf-8\")\n\n\nclass ORJSONResponse(JSONResponse):\n    \"\"\"\n    JSON response using the high-performance orjson library to serialize data to JSON.\n\n    Read more about it in the\n    [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/).\n    \"\"\"\n\n    def render(self, content: Any) -> bytes:\n        assert orjson is not None, \"orjson must be installed to use ORJSONResponse\"\n        return orjson.dumps(\n            content, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY\n        )", "start_line": 1, "end_line": 48, "total_lines": 48, "is_truncated": false}
19edit_file
{
 "filepath": "fastapi/responses.py",
 "new_string": "    def render(self, content: Any) -> bytes:\n        warnings.warn(\n            message=\"UJSONResponse is deprecated. Use JSONResponse instead.\",\n            category=FastAPIDeprecationWarning,\n            stacklevel=2,\n        )\n        assert ujson is not None, \"ujson must be installed to use UJSONResponse\"\n        return ujson.dumps(content, ensure_ascii=False).encode(\"utf-8\")",
 "old_string": "    def render(self, content: Any) -> bytes:\n        assert ujson is not None, \"ujson must be installed to use UJSONResponse\"\n        return ujson.dumps(content, ensure_ascii=False).encode(\"utf-8\")"
}
{"status": "ok", "filepath": "fastapi/responses.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/fastapi/responses.py\n+++ b/fastapi/responses.py\n@@
full
{"status": "ok", "filepath": "fastapi/responses.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/fastapi/responses.py\n+++ b/fastapi/responses.py\n@@ -29,6 +29,11 @@\n     \"\"\"\n \n     def render(self, content: Any) -> bytes:\n+        warnings.warn(\n+            message=\"UJSONResponse is deprecated. Use JSONResponse instead.\",\n+            category=FastAPIDeprecationWarning,\n+            stacklevel=2,\n+        )\n         assert ujson is not None, \"ujson must be installed to use UJSONResponse\"\n         return ujson.dumps(content, ensure_ascii=False).encode(\"utf-8\")\n \n", "is_truncated": false}
20edit_file
{
 "filepath": "fastapi/responses.py",
 "new_string": "    def render(self, content: Any) -> bytes:\n        warnings.warn(\n            message=\"ORJSONResponse is deprecated. Use JSONResponse instead.\",\n            category=FastAPIDeprecationWarning,\n            stacklevel=2,\n        )\n        assert orjson is not None, \"orjson must be installed to use ORJSONResponse\"\n        return orjson.dumps(\n            content, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY\n        )",
 "old_string": "    def render(self, content: Any) -> bytes:\n        assert orjson is not None, \"orjson must be installed to use ORJSONResponse\"\n        return orjson.dumps(\n            content, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY\n        )"
}
{"status": "ok", "filepath": "fastapi/responses.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/fastapi/responses.py\n+++ b/fastapi/responses.py\n@@
full
{"status": "ok", "filepath": "fastapi/responses.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/fastapi/responses.py\n+++ b/fastapi/responses.py\n@@ -47,6 +47,11 @@\n     \"\"\"\n \n     def render(self, content: Any) -> bytes:\n+        warnings.warn(\n+            message=\"ORJSONResponse is deprecated. Use JSONResponse instead.\",\n+            category=FastAPIDeprecationWarning,\n+            stacklevel=2,\n+        )\n         assert orjson is not None, \"orjson must be installed to use ORJSONResponse\"\n         return orjson.dumps(\n             content, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY\n", "is_truncated": false}
21edit_file
{
 "filepath": "fastapi/responses.py",
 "new_string": "from starlette.responses import StreamingResponse as StreamingResponse  # noqa\n\nimport warnings\nfrom fastapi.exceptions import FastAPIDeprecationWarning\n\ntry:",
 "old_string": "from starlette.responses import StreamingResponse as StreamingResponse  # noqa\n\ntry:"
}
{"status": "ok", "filepath": "fastapi/responses.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/fastapi/responses.py\n+++ b/fastapi/responses.py\n@@
full
{"status": "ok", "filepath": "fastapi/responses.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/fastapi/responses.py\n+++ b/fastapi/responses.py\n@@ -7,6 +7,9 @@\n from starlette.responses import RedirectResponse as RedirectResponse  # noqa\n from starlette.responses import Response as Response  # noqa\n from starlette.responses import StreamingResponse as StreamingResponse  # noqa\n+\n+import warnings\n+from fastapi.exceptions import FastAPIDeprecationWarning\n \n try:\n     import ujson\n", "is_truncated": false}
22submit_patch
{}
{"status": "ok", "patch_size": 1479, "files_changed": 1}
full
{"status": "ok", "patch_size": 1479, "files_changed": 1}

Patch

diff --git a/fastapi/responses.py b/fastapi/responses.py
index 6c8db6f3..4348a5cf 100644
--- a/fastapi/responses.py
+++ b/fastapi/responses.py
@@ -8,6 +8,9 @@ from starlette.responses import RedirectResponse as RedirectResponse  # noqa
 from starlette.responses import Response as Response  # noqa
 from starlette.responses import StreamingResponse as StreamingResponse  # noqa
 
+import warnings
+from fastapi.exceptions import FastAPIDeprecationWarning
+
 try:
     import ujson
 except ImportError:  # pragma: nocover
@@ -29,6 +32,11 @@ class UJSONResponse(JSONResponse):
     """
 
     def render(self, content: Any) -> bytes:
+        warnings.warn(
+            message="UJSONResponse is deprecated. Use JSONResponse instead.",
+            category=FastAPIDeprecationWarning,
+            stacklevel=2,
+        )
         assert ujson is not None, "ujson must be installed to use UJSONResponse"
         return ujson.dumps(content, ensure_ascii=False).encode("utf-8")
 
@@ -42,6 +50,11 @@ class ORJSONResponse(JSONResponse):
     """
 
     def render(self, content: Any) -> bytes:
+        warnings.warn(
+            message="ORJSONResponse is deprecated. Use JSONResponse instead.",
+            category=FastAPIDeprecationWarning,
+            stacklevel=2,
+        )
         assert orjson is not None, "orjson must be installed to use ORJSONResponse"
         return orjson.dumps(
             content, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY

Test output

show
.........                                                                [100%]
=============================== warnings summary ===============================
../../../../../../kaggle/tmp/envs/overlays/starlette-0.52.1-py3-none-any/starlette/testclient.py:45
  /kaggle/tmp/envs/overlays/starlette-0.52.1-py3-none-any/starlette/testclient.py:45: DeprecationWarning: The anyio.abc.BlockingPortal alias is deprecated, use anyio.from_thread.BlockingPortal instead.
    _PortalFactoryType = Callable[[], AbstractContextManager[anyio.abc.BlockingPortal]]

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
9 passed, 1 warning in 1.14s