← pod_r2_v1e

fastapi_14964

failed WRONG_FIX wrong_fix · 11 tool calls · 174 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 (12)

#ToolArgumentsResult
1run_command
{
 "command": "grep -rn \"ORJSONResponse\" fastapi | head -20"
}
{"status": "ok", "stdout": "fastapi/responses.py:36:class ORJSONResponse(JSONResponse):\nfastapi/responses.py:45: assert orjson is not None, \"orjson must be in
full
{"status": "ok", "stdout": "fastapi/responses.py:36:class ORJSONResponse(JSONResponse):\nfastapi/responses.py:45:        assert orjson is not None, \"orjson must be installed to use ORJSONResponse\"\nfastapi/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,\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}
3run_command
{
 "command": "sed -n '20,50p' fastapi/responses.py"
}
{"status": "ok", "stdout": " orjson = None # type: ignore\n\n\nclass UJSONResponse(JSONResponse):\n \"\"\"\n JSON response using the high-performance ujson libr
full
{"status": "ok", "stdout": "    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        )\n", "stderr": "", "exit_code": 0}
4run_command
{
 "command": "grep -rn \"warnings\" fastapi/responses.py"
}
{"status": "error", "error_type": "CommandError", "error_message": "", "details": {"stdout": "", "stderr": "", "exit_code": 1}}
full
{"status": "error", "error_type": "CommandError", "error_message": "", "details": {"stdout": "", "stderr": "", "exit_code": 1}}
5run_command
{
 "command": "grep -rn \"warnings\" fastapi | head -20"
}
{"status": "ok", "stdout": "fastapi/params.py:1:import warnings\nfastapi/params.py:76: warnings.warn(\nfastapi/params.py:106: warnings.warn(\nfastapi/params.py:
full
{"status": "ok", "stdout": "fastapi/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)\nfastapi/utils.py:2:import warnings\nfastapi/utils.py:83:    warnings.warn(\nfastapi/exceptions.py:255:    Ref: https://sethmlarson.dev/deprecations-via-warnings-dont-work-for-python-libraries\nfastapi/_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(\n", "stderr": "", "exit_code": 0}
6run_command
{
 "command": "python3 -c \"import warnings; print(warnings.__file__)\""
}
{"status": "ok", "stdout": "/Users/jp/.local/share/uv/python/cpython-3.13.15-macos-aarch64-none/lib/python3.13/warnings.py\n", "stderr": "", "exit_code": 0}
full
{"status": "ok", "stdout": "/Users/jp/.local/share/uv/python/cpython-3.13.15-macos-aarch64-none/lib/python3.13/warnings.py\n", "stderr": "", "exit_code": 0}
7run_command
{
 "command": "cd /workspace && python3 - <<'PY'\nfrom pathlib import Path\np = Path(\"fastapi/responses.py\")\ns = p.read_text()\n\n# Add import warnings at the top\nif \"import warnings\" not in s:\n    # Find a good place for imports. Usually after other standard imports.\n    # Let's just put it at the top.\n    s = \"import warnings\\n\" + s\n\n# Deprecate UJSONResponse\nold_ujson = \"\"\"class 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\nnew_ujson = \"\"\"class 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            \"UJSONResponse is deprecated. Use JSONResponse instead.\",\n            DeprecationWarning,\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# Deprecate ORJSONResponse\nold_orjson = \"\"\"class 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        )\"\"\"\n\nnew_orjson = \"\"\"class 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            \"ORJSONResponse is deprecated. Use JSONResponse instead.\",\n            DeprecationWarning,\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        )\"\"\"\n\ns = s.replace(old_ujson, new_ujson)\ns = s.replace(old_orjson, new_orjson)\np.write_text(s)\nPY\n"
}
{"status": "ok", "stdout": "", "stderr": "", "exit_code": 0}
full
{"status": "ok", "stdout": "", "stderr": "", "exit_code": 0}
8run_command
{
 "command": "sed -n '1,20p' fastapi/responses.py"
}
{"status": "ok", "stdout": "import warnings\nfrom typing import Any\n\nfrom starlette.responses import FileResponse as FileResponse # noqa\nfrom starlette.respo
full
{"status": "ok", "stdout": "import warnings\nfrom 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", "stderr": "", "exit_code": 0}
9run_command
{
 "command": "sed -n '20,60p' fastapi/responses.py"
}
{"status": "ok", "stdout": "except ImportError: # pragma: nocover\n orjson = None # type: ignore\n\n\nclass UJSONResponse(JSONResponse):\n \"\"\"\n JSON respons
full
{"status": "ok", "stdout": "except 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            \"UJSONResponse is deprecated. Use JSONResponse instead.\",\n            DeprecationWarning,\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            \"ORJSONResponse is deprecated. Use JSONResponse instead.\",\n            DeprecationWarning,\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        )\n", "stderr": "", "exit_code": 0}
10run_command
{
 "command": "cd /workspace && python3 - <<'PY'\nimport warnings\nfrom fastapi.responses import UJSONResponse, ORJSONResponse\n\n# We need to capture warnings\nwith warnings.catch_warnings(record=True) as w:\n    warnings.simplefilter(\"always\")\n    \n    # Mock ujson and orjson if they are not installed, \n    # but since we are in the environment, they might be.\n    # The render method is what triggers the warning.\n    \n    try:\n        resp_u = UJSONResponse({\"foo\": \"bar\"})\n        resp_u.render({\"foo\": \"bar\"})\n        print(f\"UJSONResponse warning: {w[-1].message}\")\n    except Exception as e:\n        print(f\"UJSONResponse error: {e}\")\n\n    try:\n        resp_o = ORJSONResponse({\"foo\": \"bar\"})\n        resp_o.render({\"foo\": \"bar\"})\n        print(f\"ORJSONResponse warning: {w[-1].message}\")\n    except Exception as e:\n        print(f\"ORJSONResponse error: {e}\")\nPY\n"
}
{"status": "ok", "stdout": "UJSONResponse warning: UJSONResponse is deprecated. Use JSONResponse instead.\nORJSONResponse warning: ORJSONResponse is deprecated.
full
{"status": "ok", "stdout": "UJSONResponse warning: UJSONResponse is deprecated. Use JSONResponse instead.\nORJSONResponse warning: ORJSONResponse is deprecated. Use JSONResponse instead.\n", "stderr": "", "exit_code": 0}
11run_command
{
 "command": "git diff"
}
{"status": "ok", "stdout": "diff --git a/fastapi/responses.py b/fastapi/responses.py\nindex 6c8db6f3..00dc06c1 100644\n--- a/fastapi/responses.py\n+++ b/fastapi
full
{"status": "ok", "stdout": "diff --git a/fastapi/responses.py b/fastapi/responses.py\nindex 6c8db6f3..00dc06c1 100644\n--- a/fastapi/responses.py\n+++ b/fastapi/responses.py\n@@ -1,3 +1,4 @@\n+import warnings\n from typing import Any\n \n from starlette.responses import FileResponse as FileResponse  # noqa\n@@ -29,6 +30,11 @@ class UJSONResponse(JSONResponse):\n     \"\"\"\n \n     def render(self, content: Any) -> bytes:\n+        warnings.warn(\n+            \"UJSONResponse is deprecated. Use JSONResponse instead.\",\n+            DeprecationWarning,\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@@ -42,6 +48,11 @@ class ORJSONResponse(JSONResponse):\n     \"\"\"\n \n     def render(self, content: Any) -> bytes:\n+        warnings.warn(\n+            \"ORJSONResponse is deprecated. Use JSONResponse instead.\",\n+            DeprecationWarning,\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", "stderr": "", "exit_code": 0}
12submit_patch
{}
{"status": "ok", "patch_size": 1182, "files_changed": 1}
full
{"status": "ok", "patch_size": 1182, "files_changed": 1}

Patch

diff --git a/fastapi/responses.py b/fastapi/responses.py
index 6c8db6f3..00dc06c1 100644
--- a/fastapi/responses.py
+++ b/fastapi/responses.py
@@ -1,3 +1,4 @@
+import warnings
 from typing import Any
 
 from starlette.responses import FileResponse as FileResponse  # noqa
@@ -29,6 +30,11 @@ class UJSONResponse(JSONResponse):
     """
 
     def render(self, content: Any) -> bytes:
+        warnings.warn(
+            "UJSONResponse is deprecated. Use JSONResponse instead.",
+            DeprecationWarning,
+            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 +48,11 @@ class ORJSONResponse(JSONResponse):
     """
 
     def render(self, content: Any) -> bytes:
+        warnings.warn(
+            "ORJSONResponse is deprecated. Use JSONResponse instead.",
+            DeprecationWarning,
+            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
.F
=================================== FAILURES ===================================
________________ test_orjson_response_emits_deprecation_warning ________________

    def test_orjson_response_emits_deprecation_warning():
>       with pytest.warns(FastAPIDeprecationWarning, match="ORJSONResponse is deprecated"):
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E       Failed: DID NOT WARN. No warnings of type (<class 'fastapi.exceptions.FastAPIDeprecationWarning'>,) were emitted.
E        Emitted warnings: [DeprecationWarning('ORJSONResponse is deprecated. Use JSONResponse instead.')].

tests/test_deprecated_responses.py:42: Failed
=============================== warnings summary ===============================
../../../../../../../Users/jp/repos/kaggle-gemini-coding-agent-post-training/.envs/overlays/starlette-0.52.1-py3-none-any/starlette/testclient.py:45
  /Users/jp/repos/kaggle-gemini-coding-agent-post-training/.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]]

tests/test_deprecated_responses.py::test_orjson_response_returns_correct_data
tests/test_deprecated_responses.py::test_orjson_response_emits_deprecation_warning
  /Users/jp/repos/kaggle-gemini-coding-agent-post-training/.envs/overlays/starlette-0.52.1-py3-none-any/starlette/responses.py:46: DeprecationWarning: ORJSONResponse is deprecated. Use JSONResponse instead.
    self.body = self.render(content)

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!
1 failed, 1 passed, 3 warnings in 0.72s