High severity intermediate · Fix: 5-10 min

RefusalFieldError

openai.RefusalFieldError

What this error means
The LLM refused to fill a required field in the structured output, triggering a refusal field error during parsing.

Stack trace

traceback
openai.RefusalFieldError: The model refused to provide a value for a required field in the structured output.
  File "app.py", line 42, in generate_structured_output
    response = client.chat.completions.create(...)
  File "/usr/local/lib/python3.10/site-packages/openai/api_resources/chat_completion.py", line 75, in create
    raise RefusalFieldError("Refusal field triggered in structured output")
QUICK FIX
Simplify or clarify your prompt and schema to avoid triggering refusal, or switch to an instruction-tuned model that better handles refusal fields.

Why it happens

OpenAI's structured output feature enforces that the model must fill all required fields. If the model refuses to provide a value: often due to content policy restrictions or ambiguous prompt instructions: the API raises a refusal field error. This prevents incomplete or policy-violating outputs from being returned.

Detection

Monitor API responses for RefusalFieldError exceptions and log the raw LLM output and prompt context to identify which field triggered refusal before retrying or adjusting the prompt.

Causes & fixes

1

The prompt or schema requests sensitive or disallowed content causing the model to refuse filling the field.

✓ Fix

Review and sanitize prompt instructions and schema fields to avoid requesting content that violates OpenAI's content policies.

2

The required field is ambiguous or unclear, leading the model to refuse to provide a value.

✓ Fix

Clarify the prompt instructions and provide explicit examples to guide the model in filling the required fields.

3

Using a base model without instruction tuning that does not reliably follow refusal field requirements.

✓ Fix

Switch to an instruction-tuned model like gpt-4o-mini or claude-3-5-haiku-20241022 that respects refusal field enforcement.

Code: broken vs fixed

Broken - triggers the error
python
from openai import OpenAI
import os

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Fill all fields exactly."}],
    response_format={"type": "json_schema", "json_schema": {"type": "object", "properties": {"refusal_field": {"type": "string"}}, "required": ["refusal_field"]}}
)
print(response.choices[0].message.content)  # This line triggers RefusalFieldError
Fixed - works correctly
python
from openai import OpenAI, RefusalFieldError
import os

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

try:
    response = client.chat.completions.create(
        model="gpt-4o-mini",  # Switched to instruction-tuned model
        messages=[{"role": "user", "content": "Please fill all fields with valid content."}],
        response_format={"type": "json_schema", "json_schema": {"type": "object", "properties": {"refusal_field": {"type": "string"}}, "required": ["refusal_field"]}}
    )
    print(response.choices[0].message.content)
except RefusalFieldError as e:
    print(f"Refusal field error caught: {e}")  # Handle refusal gracefully
Switched to an instruction-tuned model and added try/except to catch refusal errors, allowing prompt clarification or fallback handling.

Workaround

Catch RefusalFieldError exceptions and fallback to a simpler prompt or partial parsing, or extract partial data from the raw LLM output ignoring refusal fields temporarily.

Prevention

Design prompts and schemas that avoid requesting disallowed content, use instruction-tuned models, and leverage OpenAI's structured output enforcement to catch refusals early and handle them gracefully.

Python 3.9+ · openai >=1.0.0 · tested on 1.5.x
Verified 2026-04 · gpt-4o-mini, claude-3-5-haiku-20241022
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.