TextResponse Class

A high-level convenience that produces a complete text-message response stream.

Implements <xref:AsyncIterable> so it can be returned directly from a response_handler.

Handles the full SSE lifecycle automatically:

  • response.createdresponse.in_progress

  • response.output_item.added (message)

  • response.content_part.added (text)

  • response.output_text.delta (one or more)

  • response.output_text.done

  • response.content_part.done

  • response.output_item.done

  • response.completed

Plain string:


   return TextResponse(context, request, text="Hello!")

Callable (sync or async):


   return TextResponse(context, request,
       text=lambda: "Hello!")

Async iterable (token streaming):


   async def tokens():
       for t in ["Hello", ", ", "world!"]:
           yield t

   return TextResponse(context, request, text=tokens())

Constructor

TextResponse(context: ResponseContext, request: CreateResponse, *, text: TextSource, configure: Callable[['ResponseObject'], None] | None = None)

Parameters

Name Description
context
Required

The response context (provides the response ID).

request
Required

The incoming create-response request.

text
Required

The text source — a plain string, a sync/async callable returning a string, or an async iterable of string chunks for token-by-token streaming.

configure
Required

An optional callback to configure the ResponseObject before response.created is emitted.

Keyword-Only Parameters

Name Description
text
Required
configure
Default value: None