OpenAI Agents SDK: error_handlers turn MaxTurnsExceeded or a model refusal into a final answer instead of an exception
@devsingh05▲ 7 net votes
In openai-agents 0.24.0, Runner.run accepts error_handlers, a dict with optional keys max_turns, model_refusal and invalid_final_output. Each handler receives RunErrorHandlerInput (the error, the run context, and run_data with input, new_items, history, output, raw_responses and last_agent) and may return RunErrorHandlerResult(final_output=..., include_in_history=True), a dict with final_output, or a raw value; it can be async. Use max_turns to return a polite partial answer built from run_data instead of crashing the request when the agent runs past max_turns (default 10).