{"id":"21d9f3e63c5ba104","repo":"pallets/flask","slug":"app-json-response-takes-either-args-or-kwargs-n","errorCode":null,"errorMessage":"app.json.response() takes either args or kwargs, not both","messagePattern":"app\\.json\\.response\\(\\) takes either args or kwargs, not both","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/flask/json/provider.py","lineNumber":79,"sourceCode":"\n        :param s: Text or UTF-8 bytes.\n        :param kwargs: May be passed to the underlying JSON library.\n        \"\"\"\n        raise NotImplementedError\n\n    def load(self, fp: t.IO[t.AnyStr], **kwargs: t.Any) -> t.Any:\n        \"\"\"Deserialize data as JSON read from a file.\n\n        :param fp: A file opened for reading text or UTF-8 bytes.\n        :param kwargs: May be passed to the underlying JSON library.\n        \"\"\"\n        return self.loads(fp.read(), **kwargs)\n\n    def _prepare_response_obj(\n        self, args: tuple[t.Any, ...], kwargs: dict[str, t.Any]\n    ) -> t.Any:\n        if args and kwargs:\n            raise TypeError(\"app.json.response() takes either args or kwargs, not both\")\n\n        if not args and not kwargs:\n            return None\n\n        if len(args) == 1:\n            return args[0]\n\n        return args or kwargs\n\n    def response(self, *args: t.Any, **kwargs: t.Any) -> Response:\n        \"\"\"Serialize the given arguments as JSON, and return a\n        :class:`~flask.Response` object with the ``application/json``\n        mimetype.\n\n        The :func:`~flask.json.jsonify` function calls this method for\n        the current application.\n\n        Either positional or keyword arguments can be given, not both.","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/pallets/flask/blob/6a2f545bfd8ed31e19066a299296917e034aca58/src/flask/json/provider.py#L61-L97","documentation":"app.json.response() (the engine behind jsonify) serializes either positional arguments (treated as a value or a JSON array) or keyword arguments (treated as a JSON object) — mixing both is ambiguous because there is no sensible way to merge a list and a dict into one JSON document, so _prepare_response_obj raises TypeError.","triggerScenarios":"jsonify(data, status='ok') or app.json.response({'a': 1}, b=2) — any call combining at least one positional arg with at least one keyword arg.","commonSituations":"Trying to add extra fields to an existing dict via kwargs (jsonify(user_dict, extra=1)); passing a status or headers kwarg to jsonify thinking it works like Response; refactors that leave a stray positional arg.","solutions":["Merge everything into one dict and pass it positionally: jsonify({**user_dict, 'extra': 1}).","Or pass only keyword arguments: jsonify(name=name, extra=1).","To set status/headers, return a tuple: return jsonify(data), 201, {'X-Header': 'v'}."],"exampleFix":"# before\nreturn jsonify(user, status='ok')\n# after\nreturn jsonify({**user, 'status': 'ok'})","handlingStrategy":"validation","validationCode":"def make_json_response(app, *args, **kwargs):\n    if args and kwargs:\n        raise TypeError('pass either positional data or keyword fields to app.json.response, not both')\n    return app.json.response(*args, **kwargs)","typeGuard":null,"tryCatchPattern":"try:\n    return app.json.response(data)\nexcept TypeError:\n    # mixed args/kwargs — normalize to a single dict first\n    return app.json.response({**base, **extra})","preventionTips":["Same rule as jsonify: give either one positional payload or keyword fields, never both","When merging data sources, build one dict first and pass it as the single argument","Wrap dynamic call sites (where *args/**kwargs are forwarded) with the either/or check"],"tags":["flask","json","jsonify"],"analyzedSha":"6a2f545bfd8ed31e19066a299296917e034aca58","analyzedAt":"2026-07-31T19:13:49.921Z","schemaVersion":2}