Skip to content

Add streaming support with IAsyncEnumerable #3

@GmausDev

Description

@GmausDev

Summary

Add streaming response support using IAsyncEnumerable<T> for chat completions, enabling real-time token-by-token output similar to other AI SDKs.

Current Behavior

All responses are returned only after the complete response is received:

var response = await client.ChatAsync("Tell me a story");
Console.WriteLine(response); // Only prints after full response

Proposed API

// New streaming method
await foreach (var chunk in client.ChatStreamAsync("Tell me a story"))
{
    Console.Write(chunk.Delta?.Content); // Print tokens as they arrive
}

// Or with the full request object
var request = new ChatCompletionRequest 
{ 
    Stream = true,
    Messages = [...] 
};
await foreach (var chunk in client.CreateChatCompletionStreamAsync(request))
{
    // Process each chunk
}

Implementation Details

New Types Needed

public class ChatCompletionChunk
{
    public string Id { get; set; }
    public List<ChatChunkChoice> Choices { get; set; }
}

public class ChatChunkChoice
{
    public int Index { get; set; }
    public ChatChunkDelta Delta { get; set; }
    public string? FinishReason { get; set; }
}

public class ChatChunkDelta
{
    public string? Role { get; set; }
    public string? Content { get; set; }
}

SSE Parsing

Parse Server-Sent Events (SSE) format:

data: {"id":"...","choices":[{"delta":{"content":"Hello"}}]}

data: {"id":"...","choices":[{"delta":{"content":" world"}}]}

data: [DONE]

Expected Benefits

  • Real-time output - Display tokens as they're generated
  • Better UX - Users see immediate feedback
  • Lower perceived latency - First token appears faster
  • Feature parity - Match capabilities of other AI SDKs

Priority

🟡 P1 - High Impact

Dependencies

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions