Skip to main content
In this tutorial, you’ll learn how to build an HTTP endpoint that is compatible with the AG-UI protocol.

Prerequisites

Make sure to have Python and Poetry installed.

Setup a New Project with Poetry

First, let’s create a new project and set up Poetry for dependency management:

Install Dependencies

Now, let’s install the necessary packages:

Create a Basic Endpoint with FastAPI

Create a new file called my_endpoint/main.py with the following code:

Run and Test Your Endpoint

Start the server with:
In another terminal, test your endpoint is running using curl:
You should see the following response:

Parsing AG-UI Input

Next let’s update our endpoint to properly parse the incoming AG-UI request using the RunAgentInput Pydantic model:
FastAPI automatically validates the incoming request against the RunAgentInput schema. If the request doesn’t match the expected format, it will return a 422 Validation Error with details about what went wrong.

Add Event Streaming

AG-UI supports streaming events using Server-Sent Events (SSE). Let’s modify our /awp endpoint to stream events back to the client:
Awesome! We are already sending RunStartedEvent and RunFinishedEvent events, which gives us a basic AG-UI compliant endpoint. Now let’s make it do something useful.

Implementing Basic Chat

Let’s enhance our endpoint to call OpenAI’s API and stream the responses back as AG-UI events:
You’ll need to set your OpenAI API key as an environment variable and then restart the server:
This implementation creates a fully functional AG-UI endpoint that processes messages and streams back the responses in real-time.