POST /v1/flight_ai_search waits 20 seconds, so most searches answer 202 first and go on as a task that you read or stream until it ends. A question, or a message that needs no search, usually answers within the wait. Pick one of three ways to get the result.
Wait in the call
Without aPrefer header the call waits up to 20 seconds.
200when the turn ended in that time.statusiscompleted,input_required,failedorcanceled. Nothing more to read.202when the search is still running.statusisworking, and the response carries:
Prefer: wait=N sets the wait to N seconds, from 0 to 20. A larger value is applied as 20, and Preference-Applied says so (wait=30 is answered with Preference-Applied: wait=20). An unreadable Prefer value is ignored and the default wait applies.
Read the task
200 with the same body as POST. Read it every Retry-After seconds while status is working, and stop at any other status.
While the search runs, the task carries the early reply: message says what is being searched, and criteria holds it.
failed with error.code deadline_exceeded. Send the message again.
Stream the events
SendAccept: text/event-stream on POST to receive the turn as Server-Sent Events on the same connection.
Every event carries
task_id and context_id. id is the event’s sequence number in the task. The stream ends after the event that ends the turn. Comment lines (: keep-alive) arrive every 15 seconds and carry no event; most SSE clients skip them.
Events carry no charge. Read the task once the turn has ended to get it; see Charges.
Resume a stream
A stream also closes after 45 seconds without an event that ends the turn, or when the connection drops. Many flight searches run longer than 45 seconds, so plan to reconnect. Reconnect to the task’s events with theid of the last event you received:
id: 4 here), with no gap and no repeat. Without Last-Event-ID, or with a value that is not a number, the stream starts from the first event of the task. For a task that has already ended, the stream sends the remaining events and closes.
GET .../events always answers with a stream, whatever the Accept header.
When POST with Accept: text/event-stream is refused (400, 402, 404, 410, 422, or an authentication error), the answer is the JSON error, with no stream.
Cancel a search
- A task that is
workingorinput_requiredis canceled:200withstatus: "canceled". It writes no further result. - A task that has already ended answers
409CONFLICT, for exampleThe task has already ended (canceled).
Retry a message safely
Send anIdempotency-Key header (1 to 200 characters) with each message when your client may send it twice, for example after a timeout or a 502, 503 or 504.
- A retry with the same key does not send a second message. It answers the first message again: its stored answer once that turn is over, or the same wait as a new message while it runs.
- Keys belong to your account, not to a conversation: every API key of the account shares them. The body of a retry is not compared with the first one, so a retry with a different
textstill gets the first answer. Use a new key for each new message.
