如何为gpt-4-vision-preview启用JSON模式?文档问题与报错咨询
解决gpt-4-vision-preview启用JSON模式报错问题
问题原因
OpenAI API文档曾存在错误,现已修正。旧文档声称调用gpt-4-vision-preview或gpt-3.5-turbo时可启用JSON模式,实际仅gpt-4-1106-preview和gpt-3.5-turbo-1106这两个版本支持response_format参数设置为json_object。
按照旧文档指引调用gpt-4-vision-preview并配置response_format: {"type": "json_object"}时,会触发以下验证错误:
{'error': {'message': '1 validation error for Request
body -> response_format
extra fields not permitted (type=value_error.extra)', 'type': 'invalid_request_error', 'param': None, 'code': None}}
解决方案
有两种处理方式可选:
1. 切换到支持JSON模式的模型
将请求中的模型替换为gpt-4-1106-preview或gpt-3.5-turbo-1106,保留response_format参数即可强制返回JSON格式。修正后的代码示例:
headers = { "Content-Type": "application/json", "Authorization": f"Bearer {api_key}" } payload = { "model": "gpt-4-1106-preview", "response_format": {"type": "json_object"}, "messages": [ { "role": "system", "content": "You are a helpful assistant. Your response should be in JSON format." }, { "role": "user", "content": [ { "type": "text", "text": prompt }, { "type": "image_url", "image_url": { "url": f"data:image/jpeg;base64,{base64_image}" } } ] } ], "max_tokens": 1000, } response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload) print(response.json())
2. 保留gpt-4-vision-preview,移除response_format参数
若必须使用视觉模型,需删除response_format配置,同时通过系统提示词引导模型返回JSON。注意这种方式无法强制模型严格输出JSON,仅为软性约束:
headers = { "Content-Type": "application/json", "Authorization": f"Bearer {api_key}" } payload = { "model": "gpt-4-vision-preview", "messages": [ { "role": "system", "content": "You are a helpful assistant. Your response should be in JSON format." }, { "role": "user", "content": [ { "type": "text", "text": prompt }, { "type": "image_url", "image_url": { "url": f"data:image/jpeg;base64,{base64_image}" } } ] } ], "max_tokens": 1000, } response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload) print(response.json())
内容的提问来源于stack exchange,提问作者chibop
相关产品推荐
相关产品推荐

