Class OpenAICompatibleService
Base class for providers that follow the OpenAI-compatible streaming format (SSE with "data:" prefix, "[DONE]" terminator, choices/delta structure). Used by ChatGPT, Grok, Qwen, and other compatible providers. Providers override ParseStreamChunk(string, StreamOptions) to handle provider-specific parsing.
public abstract class OpenAICompatibleService : AIService, IAIService, IFunctionRegisterable
- Inheritance
-
OpenAICompatibleService
- Implements
- Derived
- Inherited Members
- Extension Methods
Constructors
OpenAICompatibleService(string?, string, HttpClient)
protected OpenAICompatibleService(string? apiKey, string baseUrl, HttpClient httpClient)
Parameters
apiKeystringbaseUrlstringhttpClientHttpClient
Fields
RequiresProviderCallIdMetadataKey
protected const string RequiresProviderCallIdMetadataKey = "requires_provider_call_id"
Field Value
StreamIndexExplicitMetadataKey
protected const string StreamIndexExplicitMetadataKey = "stream_index_explicit"
Field Value
StreamItemIdMetadataKey
protected const string StreamItemIdMetadataKey = "stream_item_id"
Field Value
Methods
CreateStreamParseFailure(string, Exception, StreamDiagnostics)
Lets providers turn a malformed SSE data event into an explicit terminal error. Returning null preserves the tolerant behavior used by legacy compatible providers.
protected virtual StreamingContent? CreateStreamParseFailure(string jsonData, Exception exception, StreamDiagnostics diagnostics)
Parameters
jsonDatastringexceptionExceptiondiagnosticsStreamDiagnostics
Returns
EnrichStreamAssistantMessage(Message)
Allows a provider to attach state collected while streaming before the assistant message is added to the conversation history.
protected virtual void EnrichStreamAssistantMessage(Message assistantMessage)
Parameters
assistantMessageMessage
OnStreamRoundFailed()
Lets providers discard state collected during a failed streaming round.
protected virtual void OnStreamRoundFailed()
OnStreamRoundStarting()
Allows a provider to reset state scoped to a single streaming round.
protected virtual void OnStreamRoundStarting()
ParseOpenAICompatibleUsage(JsonElement)
Parses OpenAI-compatible usage JSON (handles both prompt_tokens/input_tokens variants).
protected static TokenUsage? ParseOpenAICompatibleUsage(JsonElement usage)
Parameters
usageJsonElement
Returns
ParseStreamChunk(string, StreamOptions)
Parses a single SSE JSON chunk into a provider-neutral stream chunk. Each provider overrides this to handle its specific JSON format.
protected abstract OpenAICompatibleService.OpenAIStreamChunk ParseStreamChunk(string jsonData, StreamOptions options)
Parameters
jsonDatastringoptionsStreamOptions
Returns
StreamCompletionAsync(Message, Func<string, Task>)
public override Task StreamCompletionAsync(Message message, Func<string, Task> messageReceivedAsync)
Parameters
Returns
StreamRoundAsync(StreamOptions, bool, FunctionCallingPolicy, CancellationToken)
Executes a single streaming round: sends an HTTP request, reads the SSE stream, yields chunks, and handles function execution if detected. Yield a FunctionResult to signal the template to continue to the next round; otherwise the stream ends.
protected override IAsyncEnumerable<StreamingContent> StreamRoundAsync(StreamOptions options, bool useFunctions, FunctionCallingPolicy policy, CancellationToken cancellationToken)
Parameters
optionsStreamOptionsuseFunctionsboolpolicyFunctionCallingPolicycancellationTokenCancellationToken
Returns
ValidateStreamTermination(bool, bool, StreamDiagnostics)
Lets providers require an explicit successful terminal event before committing a streamed assistant message or executing a collected function call.
protected virtual StreamingContent? ValidateStreamTermination(bool doneMarkerReceived, bool completionEventReceived, StreamDiagnostics diagnostics)
Parameters
doneMarkerReceivedboolcompletionEventReceivedbooldiagnosticsStreamDiagnostics