llm
config_error
ai_generated
true
openai.BadRequestError: Invalid schema for response_format: 'properties' must be an object with string keys.
ID: llm/structured-output-schema-mismatch
90%Fix Rate
90%Confidence
1Evidence
2023-11-20First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| openai>=1.0.0 | active | — | — | — |
| openai>=1.12.0 | active | — | — | — |
Root Cause
The JSON schema provided in the response_format parameter is malformed, e.g., using integer keys or missing required fields like 'type'.
generic中文
response_format参数中提供的JSON schema格式错误,例如使用整数键或缺少必需的字段如'type'。
Official Documentation
https://platform.openai.com/docs/guides/structured-outputsWorkarounds
-
95% success Use the Pydantic library to generate the schema automatically and validate it: from pydantic import BaseModel from openai import OpenAI class ResponseModel(BaseModel): name: str age: int client = OpenAI() schema = ResponseModel.model_json_schema() print(schema) # Check that 'properties' has string keys response = client.chat.completions.create( model='gpt-4o-mini', messages=[{'role': 'user', 'content': 'Extract info'}], response_format={ 'type': 'json_schema', 'json_schema': {'name': 'response', 'schema': schema} } )
Use the Pydantic library to generate the schema automatically and validate it: from pydantic import BaseModel from openai import OpenAI class ResponseModel(BaseModel): name: str age: int client = OpenAI() schema = ResponseModel.model_json_schema() print(schema) # Check that 'properties' has string keys response = client.chat.completions.create( model='gpt-4o-mini', messages=[{'role': 'user', 'content': 'Extract info'}], response_format={ 'type': 'json_schema', 'json_schema': {'name': 'response', 'schema': schema} } ) -
90% success Manually ensure the schema has the correct structure: { 'type': 'object', 'properties': { 'name': {'type': 'string'}, 'age': {'type': 'integer'} }, 'required': ['name', 'age'] }
Manually ensure the schema has the correct structure: { 'type': 'object', 'properties': { 'name': {'type': 'string'}, 'age': {'type': 'integer'} }, 'required': ['name', 'age'] }
中文步骤
使用Pydantic库自动生成并验证schema: from pydantic import BaseModel from openai import OpenAI class ResponseModel(BaseModel): name: str age: int client = OpenAI() schema = ResponseModel.model_json_schema() print(schema) # 检查'properties'是否有字符串键 response = client.chat.completions.create( model='gpt-4o-mini', messages=[{'role': 'user', 'content': '提取信息'}], response_format={ 'type': 'json_schema', 'json_schema': {'name': 'response', 'schema': schema} } )手动确保schema具有正确的结构: { 'type': 'object', 'properties': { 'name': {'type': 'string'}, 'age': {'type': 'integer'} }, 'required': ['name', 'age'] }
Dead Ends
Common approaches that don't work:
-
50% fail
OpenAI's schema validation is stricter than standard JSON; it requires specific keys like 'type': 'object' and 'properties' must be an object.
-
40% fail
Older models may not support the same schema features; the schema must comply with OpenAI's JSON Schema subset.