Repository navigation
Streaming
Streaming returns a response in pieces while the model is still generating, so you can show text as it arrives. Every StreamContentAsync overload returns IAsyncEnumerable<GenerateContentResponse>, which you read with await foreach.
Package: Google_GenerativeAI. Setup: Initialization.
using GenerativeAI;
using GenerativeAI.Types;
var model = new GoogleAi("YOUR_API_KEY").CreateGenerativeModel(GoogleAIModels.Gemini38Flash);This is the most general overload. Build the request as described in Generate Content.
var request = new GenerateContentRequest();
request.AddText("Write a story about a robot learning to love.");
await foreach (var chunk in model.StreamContentAsync(request))
{
Console.Write(chunk.Text());
}await foreach (var chunk in model.StreamContentAsync("Generate a list of 10 creative project ideas."))
{
Console.Write(chunk.Text());
}Streams a response for a prompt and a remote file URI, such as a Files API URI, a gs:// URI on Vertex AI or a public URL.
await foreach (var chunk in model.StreamContentAsync(
"Describe what is happening in this image.",
remoteFile.Uri!, // e.g. from FileClient.UploadFileAsync
"image/jpeg"))
{
Console.Write(chunk.Text());
}To stream with a local file path, use GeminiModel.StreamContentAsync(prompt, filePath). See Multimodal Features.
var parts = new List<Part> { new Part { Text = "Translate 'Hello, world!' to Spanish." } };
await foreach (var chunk in model.StreamContentAsync(parts))
{
Console.Write(chunk.Text());
}var contents = new List<Content>
{
new Content("Explain the concept of quantum entanglement.", Roles.User)
};
await foreach (var chunk in model.StreamContentAsync(contents))
{
Console.Write(chunk.Text());
}Every overload takes a CancellationToken. Pass it directly or use WithCancellation:
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
await foreach (var chunk in model.StreamContentAsync("Write a long essay.", cts.Token))
{
Console.Write(chunk.Text());
}Each chunk holds only part of the output. Join the chunks to get the full text:
var sb = new System.Text.StringBuilder();
await foreach (var chunk in model.StreamContentAsync("Tell me a joke."))
{
sb.Append(chunk.Text());
}
Console.WriteLine(sb.ToString());- Function calling: When the last chunk asks for a function call and the model has function tools with auto-calling enabled, the SDK runs the function and keeps streaming the model's follow-up. See Function Calling.
-
Automatic continuation: If a stream ends with
FinishReason.CONTINUATION, or fails after a continuation checkpoint, the SDK resumes it with the continuation token. This is controlled byEnableAutomaticContinuationandMaxContinuations(see Generate Content). -
Chat sessions:
ChatSession.StreamContentAsyncadds the joined response to the history when the stream completes. See Chat Session. -
Thinking: With
ThinkingConfig.IncludeThoughts = true, chunks can contain thought parts (Part.Thought == true). -
Errors: An API error is thrown as
ApiExceptionwhile you iterate. Wrap theawait foreachintry/catch.
Getting Started
- Initialization
- Authentication
- Configuration & Error Handling
- Supported Models
- Migration Guide (v4)
- Release Notes (v4)
Core Features
- Generate Content
- Chat Session
- Streaming
- Multimodal Features
- JSON Mode
- Function Calling
- Embeddings
- Context Caching
Gemini APIs
- Interactions API
- Multimodal Live API
- File Search
- Batch API
- Model Tuning
- Webhooks
- Agent APIs
- Image Generation
- Video Generation
- Speech Generation
- Files Client
- Model Client
Vertex AI
Integrations
Guides
Legacy