Skip to content

OpenAI Agents SDK ​

openai-agents for Python. It uses the Responses API by default; Chat Completions works too.

Connect ​

Build a model object, so the SDK sends the id as given (its string lookup would read anthropic/... as a provider prefix). Turn tracing off, or the SDK posts traces to OpenAI:

py
def model_for(model: str, api: str):
    """The SDK model for one id on one API. A model object sends the id as given: the SDK's own string lookup would read
    `anthropic/...` or `openai/...` as a provider prefix. The run's variant is kebab-case (the summary pattern)."""
    set_tracing_disabled(True)
    client = AsyncOpenAI(base_url=os.environ["PLUGBOARD_BASE_URL"], api_key=os.environ["PLUGBOARD_KEY"])
    if api == "responses":
        return OpenAIResponsesModel(model=model, openai_client=client)
    return OpenAIChatCompletionsModel(model=model, openai_client=client)

Chat ​

py
agent = Agent(name="pong", model=model_for(model, api), instructions="Answer tersely.")
result = Runner.run_sync(agent, "Reply with exactly: pong")

Tool calls ​

The SDK runs the tool and makes the follow-up request itself.

py
@function_tool
def get_weather(city: str) -> str:
    """Current weather for a city."""
    tool_calls.append(city)
    return '{"temp_c": 18, "sky": "cloudy"}'

agent = Agent(name="weather", model=model_for(model, api), instructions="Use the tool.", tools=[get_weather])
result = Runner.run_sync(agent, WEATHER_PROMPT)

Structured output ​

py
agent = Agent(name="meal", model=model_for(model, api), instructions="Estimate the meal.", output_type=Meal)
meal = Runner.run_sync(agent, PROMPT).final_output

Streaming ​

Runner.run_streamed(agent, prompt) yields raw_response_event items; text arrives as response.output_text.delta.

py
agent = Agent(name="pong", model=model_for(model, api), instructions="Answer tersely.")
result = Runner.run_streamed(agent, "Reply with exactly: pong")
async for _event in result.stream_events():
    pass

Limits ​

  • The SDK doesn't expose the wire finish_reason. Check the output items for a function call instead.