Chat
StreamCompletions
`Chat.StreamCompletions` for SSE-style incremental chat output.
Signature
func (c *ChatClient) StreamCompletions(ctx context.Context, model string, messages []ChatMessage, opts ...option) (*ChatStream, error)Option behavior
Uses the same option set and validation as Completions.
Streaming behavior
- SDK sets
stream=trueinternally before request. - Response is consumed line-by-line from SSE
data:frames. [DONE]ends iteration.Text()accumulates first-choice delta content.
ChatStream methods
Next() boolCurrent() ChatChunkChoice() (ChunkChoice, bool)Text() stringErr() errorClose() error
Example
stream, err := client.Chat.StreamCompletions(
ctx,
chat.ModelSarvamM,
[]chat.ChatMessage{
chat.UserMessage("Give me a 3-line intro to Sarvam AI."),
},
chat.WithTopP(0.9),
)
if err != nil {
panic(err)
}
defer stream.Close()
for stream.Next() {
if choice, ok := stream.Choice(); ok {
fmt.Print(choice.Delta.Content)
}
}
if err := stream.Err(); err != nil {
panic(err)
}