Skip to main content

Streaming Responses

The server streams Server-Sent Events (SSE). Each event is a JSON object containing agent output, logs, and other events.

Response Format

data: {"type":"message","data":{"content":"Hello"}}

data: {"type":"log","data":{"message":"Processing..."}}

data: {"type":"message","data":{"content":"Done!"}}

Event Types

Message Events

Agent responses are sent as message events:
{
  "type": "message",
  "data": {
    "content": "Response text from the agent"
  }
}

Log Events

Diagnostic information and logs:
{
  "type": "log",
  "data": {
    "message": "Processing step completed"
  }
}
Each line starts with data: followed by a type-discriminated JSON envelope.

Consuming the Stream

With curl

curl -N \
  -H "Content-Type: application/json" \
  -X POST http://127.0.0.1:8080/query \
  -d '{"parts":[{"type":"text","text":"Hello"}],"kwargs":{}}'
The -N flag is essential for real-time streaming.

With JavaScript

const eventSource = new EventSource('/query');

eventSource.onmessage = (event) => {
  const data = JSON.parse(event.data);

  if (data.type === 'message') {
    console.log('Agent:', data.data.content);
  } else if (data.type === 'log') {
    console.log('Log:', data.data.message);
  }
};

With Python

import requests
import json

response = requests.post(
    'http://127.0.0.1:8080/query',
    json={"parts": [{"type": "text", "text": "Hello"}], "kwargs": {}},
    stream=True
)

for line in response.iter_lines():
    if line:
        event = json.loads(line.decode('utf-8').replace('data: ', ''))
        print(f"{event['type']}: {event['data']}")

Next Steps