{"record":{"id":"735a3d67e1a603a7","repo":"openai/openai-python","slug":"unable-to-automatically-parse-response-format-type","errorCode":null,"errorMessage":"Unable to automatically parse response format type {response_format}","messagePattern":"Unable to automatically parse response format type (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/openai/lib/_parsing/_completions.py","lineNumber":253,"sourceCode":"\n    input_fn = cast(object, input_tool.get(\"function\"))\n    if isinstance(input_fn, PydanticFunctionTool):\n        return True\n\n    return cast(FunctionDefinition, input_fn).get(\"strict\") or False\n\n\ndef _parse_content(response_format: type[ResponseFormatT], content: str) -> ResponseFormatT:\n    if is_basemodel_type(response_format):\n        return cast(ResponseFormatT, model_parse_json(response_format, content))\n\n    if is_dataclass_like_type(response_format):\n        if PYDANTIC_V1:\n            raise TypeError(f\"Non BaseModel types are only supported with Pydantic v2 - {response_format}\")\n\n        return pydantic.TypeAdapter(response_format).validate_json(content)\n\n    raise TypeError(f\"Unable to automatically parse response format type {response_format}\")\n\n\ndef type_to_response_format_param(\n    response_format: type | completion_create_params.ResponseFormat | Omit,\n) -> ResponseFormatParam | Omit:\n    if not is_given(response_format):\n        return omit\n\n    if is_response_format_param(response_format):\n        return response_format\n\n    # type checkers don't narrow the negation of a `TypeGuard` as it isn't\n    # a safe default behaviour but we know that at this point the `response_format`\n    # can only be a `type`\n    response_format = cast(type, response_format)\n\n    json_schema_type: type[pydantic.BaseModel] | pydantic.TypeAdapter[Any] | None = None\n","sourceCodeStart":235,"sourceCodeEnd":271,"githubUrl":"https://github.com/openai/openai-python/blob/9917c6e28e66e90e1227b3d223c06a8c5441515a/src/openai/lib/_parsing/_completions.py#L235-L271","documentation":"_parse_content only knows how to parse pydantic BaseModel types and dataclass-like types (dataclass, TypedDict, NamedTuple). Any other type passed as response_format (e.g. a plain dict, int, or arbitrary class) raises this TypeError before/after the API call.","triggerScenarios":"Passing response_format=int, response_format=dict[str, int], or a non-dataclass plain class to chat.completions.parse / maybe_parse_content.","commonSituations":"Assuming .parse() can return arbitrary built-in types; passing an already-constructed ResponseFormat dict object instead of a class.","solutions":["Use a pydantic BaseModel subclass as response_format","For simple containers, wrap them in a dataclass or BaseModel (e.g. class Output(BaseModel): items: list[int])","For raw control, use completions.create with response_format={'type':'json_object'} and parse yourself"],"exampleFix":"# before\nclient.chat.completions.parse(..., response_format=list[str])\n# after\nclass Output(BaseModel):\n    items: list[str]\ncompletion = client.chat.completions.parse(..., response_format=Output)","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"from openai._compat import is_basemodel_type\nfrom openai.lib._parsing._completions import is_dataclass_like_type\ndef parseable(t: type) -> bool:\n    return is_basemodel_type(t) or is_dataclass_like_type(t)","tryCatchPattern":null,"preventionTips":["Always model outputs as BaseModel/dataclass types","Don't pass built-in annotations like list[str] directly"],"tags":["parsing","type-validation","completions"],"backgroundTag":"unsupported-response-format","analyzedSha":"9917c6e28e66e90e1227b3d223c06a8c5441515a","analyzedAt":"2026-08-28T11:46:34.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}