{"record":{"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/3596b1ab61cea85edb8970e83ff61daa073facf8/src/flask/json/provider.py#L61-L97","documentation":"JSONProvider._prepare_response_obj(args, kwargs) is used by response()/jsonify. If BOTH positional args and keyword args are supplied (args and kwargs both truthy), it raises TypeError. The API contract is: give either a single positional value, multiple positional values (serialized as a list), or keyword args (serialized as a dict)—never both.","triggerScenarios":"Calling app.json.response(obj, key=val) or jsonify(value, extra='x'). Passing any mix where at least one positional and one keyword argument are present. _prepare_response_obj checks args and kwargs and rejects the combination before serialization.","commonSituations":"Refactoring a jsonify call and accidentally combining a positional data argument with a keyword. Calling response() directly with mixed arguments. Migrating from an older custom JSON helper that accepted both.","solutions":["Pass data either positionally or as keywords, not both: jsonify(myobj) or jsonify(key=value).","If you need a dict plus extra keys, merge them first: jsonify({**myobj, 'extra': 'x'}).","For multiple values use only positional args (they become a list) or only keyword args (they become a dict)."],"exampleFix":"# before\nreturn jsonify(data, status='ok')  # both args and kwargs -> TypeError\n\n# after\nreturn jsonify({**data, 'status': 'ok'})  # kwargs only","handlingStrategy":"validation","validationCode":"def validate_response_args(args: tuple, kwargs: dict):\n    if args and kwargs:\n        raise TypeError('Pass either positional args OR kwargs, not both')\n    return True","typeGuard":"def uses_single_channel(args: tuple, kwargs: dict) -> bool:\n    return not (args and kwargs)","tryCatchPattern":"try:\n    app.json.response(obj, key='x')\nexcept TypeError as e:\n    if 'either args or kwargs' in str(e):\n        resp = app.json.response({**obj, 'key': 'x'})\n    else:\n        raise","preventionTips":["Use jsonify(dict) or jsonify(**dict), never a mix.","Review jsonify calls after refactors to ensure args/kwargs are not combined."],"tags":["flask","json","api-contract"],"backgroundTag":null,"analyzedSha":"3596b1ab61cea85edb8970e83ff61daa073facf8","analyzedAt":"2026-08-11T20:11:52.622Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}