Streaming Responses
Streaming Text Completion
Section titled “Streaming Text Completion”Stream plain text completions as they are generated, ideal for autocomplete, summaries, and single-output generation.
stream, err := client.TextCompletionStreamRequest(schemas.NewDeepIntShieldContext(context.Background(), schemas.NoDeadline), &schemas.DeepIntShieldTextCompletionRequest{ Provider: schemas.OpenAI, Model: "gpt-4o-mini", Input: &schemas.TextCompletionInput{ PromptStr: deepintshield.Ptr("A for apple and B for"), },})
if err != nil { log.Printf("Streaming request failed: %v", err) return}
for chunk := range stream { // Handle errors in stream if chunk.DeepIntShieldError != nil { log.Printf("Stream error: %v", chunk.DeepIntShieldError) break }
// Process response chunks if chunk.DeepIntShieldTextCompletionResponse != nil && len(chunk.DeepIntShieldTextCompletionResponse.Choices) > 0 { choice := chunk.DeepIntShieldTextCompletionResponse.Choices[0]
// Check for streaming content if choice.TextCompletionResponseChoice != nil && choice.TextCompletionResponseChoice.Text != nil { content := *choice.DeepIntShieldTextCompletionResponseChoice.Text fmt.Print(content) // Print content as it arrives } }}Streaming Chat Responses
Section titled “Streaming Chat Responses”Receive incremental chat deltas in real-time. Append delta content to progressively render assistant messages.
stream, err := client.ChatCompletionStreamRequest(schemas.NewDeepIntShieldContext(context.Background(), schemas.NoDeadline), &schemas.DeepIntShieldChatRequest{ Provider: schemas.OpenAI, Model: "gpt-4o-mini", Input: messages,})
if err != nil { log.Printf("Streaming request failed: %v", err) return}
for chunk := range stream { // Handle errors in stream if chunk.DeepIntShieldError != nil { log.Printf("Stream error: %v", chunk.DeepIntShieldError) break }
// Process response chunks if chunk.DeepIntShieldChatResponse != nil && len(chunk.DeepIntShieldChatResponse.Choices) > 0 { choice := chunk.DeepIntShieldChatResponse.Choices[0]
// Check for streaming content if choice.ChatStreamResponseChoice != nil && choice.ChatStreamResponseChoice.Delta != nil && choice.ChatStreamResponseChoice.Delta.Content != nil {
content := *choice.ChatStreamResponseChoice.Delta.Content fmt.Print(content) // Print content as it arrives } }}Note: Streaming requests also follow the default timeout setting defined in provider configuration, which defaults to 30 seconds.
Responses API Streaming
Section titled “Responses API Streaming”Use the OpenAI-style Responses API with streaming for unified flows. Events arrive via SSE; accumulate text deltas until completion.
messages := []schemas.ResponsesMessage{ { Role: deepintshield.Ptr(schemas.ResponsesInputMessageRoleUser), Content: &schemas.ResponsesMessageContent{ ContentStr: deepintshield.Ptr("Hello, DeepIntShield!"), }, },}
stream, err := client.ResponsesStreamRequest(schemas.NewDeepIntShieldContext(context.Background(), schemas.NoDeadline), &schemas.DeepIntShieldResponsesRequest{ Provider: schemas.OpenAI, Model: "gpt-4o-mini", Input: messages,})
if err != nil { log.Printf("Streaming request failed: %v", err) return}
for chunk := range stream { // Handle errors in stream if chunk.DeepIntShieldError != nil { log.Printf("Stream error: %v", chunk.DeepIntShieldError) break }
// Process response chunks if chunk.DeepIntShieldResponsesStreamResponse != nil { delta := chunk.DeepIntShieldResponsesStreamResponse.Delta
// Check for streaming content if delta != nil { fmt.Print(*delta) // Print content as it arrives } }}Text-to-Speech Streaming: Real-time Audio Generation
Section titled “Text-to-Speech Streaming: Real-time Audio Generation”Stream audio generation in real-time as text is converted to speech. Ideal for long texts or when you need immediate audio playback.
stream, err := client.SpeechStreamRequest(schemas.NewDeepIntShieldContext(context.Background(), schemas.NoDeadline), &schemas.DeepIntShieldSpeechRequest{ Provider: schemas.OpenAI, Model: "tts-1", // Using text-to-speech model Input: &schemas.SpeechInput{ Input: "Hello! This is a sample text that will be converted to speech using DeepIntShield's speech synthesis capabilities. The weather today is wonderful, and I hope you're having a great day!", }, Params: &schemas.SpeechParameters{ VoiceConfig: &schemas.SpeechVoiceInput{ Voice: schemas.Ptr("alloy"), }, ResponseFormat: schemas.Ptr("mp3"), },})
if err != nil { panic(err)}
// Handle speech synthesis streamvar audioData []bytevar totalChunks intfilename := "output.mp3"
for chunk := range stream { if chunk.DeepIntShieldError != nil { panic(fmt.Sprintf("Stream error: %s", chunk.DeepIntShieldError.Error.Message)) }
if chunk.DeepIntShieldSpeechStreamResponse != nil { // Accumulate audio data from each chunk audioData = append(audioData, chunk.DeepIntShieldSpeechStreamResponse.Audio...) totalChunks++ fmt.Printf("Received chunk %d, size: %d bytes\n", totalChunks, len(chunk.DeepIntShieldSpeechStreamResponse.Audio)) }}
if len(audioData) > 0 { // Save the accumulated audio to a file err := os.WriteFile(filename, audioData, 0644) if err != nil { panic(fmt.Sprintf("Failed to save audio file: %v", err)) }
fmt.Printf("Speech synthesis streaming complete! Audio saved to %s\n", filename) fmt.Printf("Total chunks received: %d, final file size: %d bytes\n", totalChunks, len(audioData))}Speech-to-Text Streaming: Real-time Audio Transcription
Section titled “Speech-to-Text Streaming: Real-time Audio Transcription”Stream audio transcription results as they’re processed. Get immediate text output for real-time applications or long audio files.
// Read the audio file for transcriptionaudioFilename := "output.mp3"audioData, err := os.ReadFile(audioFilename)if err != nil { panic(fmt.Sprintf("Failed to read audio file %s: %v. Please make sure the file exists.", audioFilename, err))}
fmt.Printf("Loaded audio file %s (%d bytes) for transcription...\n", audioFilename, len(audioData))
stream, err := client.TranscriptionStreamRequest(schemas.NewDeepIntShieldContext(context.Background(), schemas.NoDeadline), &schemas.DeepIntShieldTranscriptionRequest{ Provider: schemas.OpenAI, Model: "whisper-1", // Using Whisper model for transcription Input: &schemas.TranscriptionInput{ File: audioData, }, Params: &schemas.TranscriptionParameters{ Prompt: schemas.Ptr("This is a sample audio transcription from DeepIntShield speech synthesis."), // Optional: provide context },})
if err != nil { panic(err)}
for chunk := range stream { if chunk.DeepIntShieldError != nil { panic(fmt.Sprintf("Stream error: %s", chunk.DeepIntShieldError.Error.Message)) }
if chunk.DeepIntShieldTranscriptionStreamResponse != nil && chunk.DeepIntShieldTranscriptionStreamResponse.Delta != nil { // Print each chunk of text as it arrives fmt.Print(*chunk.DeepIntShieldTranscriptionStreamResponse.Delta) }}Streaming Best Practices
Section titled “Streaming Best Practices”Buffering for Audio
Section titled “Buffering for Audio”For audio streaming, consider buffering chunks before saving:
const bufferSize = 1024 * 1024 // 1MB buffer
var audioBuffer bytes.Buffervar lastSave time.Time
for chunk := range stream { if chunk.DeepIntShieldSpeechStreamResponse != nil { audioBuffer.Write(chunk.DeepIntShieldSpeechStreamResponse.Audio)
// Save every second or when buffer is full if time.Since(lastSave) > time.Second || audioBuffer.Len() > bufferSize { // Append to file file, err := os.OpenFile("streaming_audio.mp3", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) if err == nil { file.Write(audioBuffer.Bytes()) file.Close() audioBuffer.Reset() lastSave = time.Now() } } }}Context and Cancellation
Section titled “Context and Cancellation”Use context to control streaming duration:
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)defer cancel()
stream, err := client.ChatCompletionStreamRequest(schemas.NewDeepIntShieldContext(ctx, schemas.NoDeadline), &schemas.DeepIntShieldChatRequest{ // ... your request})
// Stream will automatically stop after 30 secondsVoice Options
Section titled “Voice Options”OpenAI TTS supports these voices:
alloy- Balanced, natural voiceecho- Deep, resonant voicefable- Expressive, storytelling voiceonyx- Strong, confident voicenova- Bright, energetic voiceshimmer- Gentle, soothing voice
// Different voice exampleVoiceConfig: schemas.SpeechVoiceInput{ Voice: deepintshield.Ptr("nova"),},Note: Please check each model’s documentation to see if it supports the corresponding streaming features. Not all providers support all streaming capabilities.
Next Steps
Section titled “Next Steps”- Tool Calling - Enable AI to use external functions
- Multimodal AI - Process images and multimedia content
- Provider Configuration - Multiple providers for redundancy
- Core Features - Advanced DeepIntShield capabilities