OpenAIError
openai.OpenAIError: Run failed with requires_action not handled
Stack trace
openai.OpenAIError: Run failed: requires_action not handled
File "/app/main.py", line 42, in run_assistant
response = client.chat.completions.create(model="gpt-4o", messages=messages)
File "/usr/local/lib/python3.9/site-packages/openai/api_resources/chat.py", line 75, in create
raise OpenAIError(f"Run failed: {error_detail}")
openai.OpenAIError: Run failed: requires_action not handled Why it happens
The OpenAI API can return a 'requires_action' status indicating additional user or system action is needed before continuing. If the client code does not explicitly check for and handle this status, it raises an error. This usually happens when using advanced assistant features or workflows requiring multi-step interactions.
Detection
Monitor for OpenAIError exceptions containing 'requires_action' in the message and log the full response payload to detect unhandled action requirements before failure.
Causes & fixes
The API returned a 'requires_action' status indicating further steps are needed but the client code does not check for it.
Add explicit handling for 'requires_action' in the response, such as prompting the user or triggering the required follow-up action.
Using an OpenAI assistant feature that requires multi-turn interaction without implementing the required action flow.
Implement the multi-turn action flow as documented, handling 'requires_action' responses by branching logic or user prompts.
Client code assumes all runs succeed immediately and does not handle intermediate states.
Add robust error and state handling around run completions to detect and process intermediate statuses like 'requires_action'.
Code: broken vs fixed
from openai import OpenAI
import os
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
messages = [{"role": "user", "content": "Start the assistant flow."}]
# This line raises OpenAIError: Run failed: requires_action not handled
response = client.chat.completions.create(model="gpt-4o", messages=messages)
print(response) from openai import OpenAI, OpenAIError
import os
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
messages = [{"role": "user", "content": "Start the assistant flow."}]
try:
response = client.chat.completions.create(model="gpt-4o", messages=messages)
except OpenAIError as e:
if "requires_action" in str(e):
# Handle the requires_action response here, e.g., prompt user or trigger follow-up
print("Requires action detected. Please handle the next steps.")
else:
raise
else:
print(response) Workaround
Wrap the API call in a try/except block catching OpenAIError, then parse the error message or response for 'requires_action' and implement a fallback prompt or manual intervention.
Prevention
Design your assistant workflows to detect and handle intermediate states like 'requires_action' by implementing multi-turn interaction logic and state management to avoid unhandled errors.