query() supports two input modes:
- Single-message query mode: submit one user message; the SDK closes the session after the current reply completes. See the Quickstart.
- Multi-message session mode: keep the session open and carry on a multi-turn conversation with the model.
Multi-message session
Define a sequence that yields user messages in order:SDKUserMessage.
Managing the session lifecycle
In multi-message sessions, the lifecycle of the object returned byquery() is owned by the caller. The SDK only closes the session as a side effect of the message sequence finishing on its own; in every other case the caller has to decide when to wrap up, otherwise the session keeps hanging.
Common scenarios where the caller needs to manage closure manually:
-
The message sequence itself doesn’t end, for example a generator that loops on external input:
- You want to terminate before the message sequence would end naturally: timeout, user cancellation, other business conditions.