sarvamai-go SDK Documentation
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

  1. SDK sets stream=true internally before request.
  2. Response is consumed line-by-line from SSE data: frames.
  3. [DONE] ends iteration.
  4. Text() accumulates first-choice delta content.

ChatStream methods

  • Next() bool
  • Current() ChatChunk
  • Choice() (ChunkChoice, bool)
  • Text() string
  • Err() error
  • Close() 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)
}

On this page