Table of Contents

Class OpenAICompatibleService

Namespace
Mythosia.AI.Services.Base
Assembly
Mythosia.AI.dll

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

apiKey string
baseUrl string
httpClient HttpClient

Fields

RequiresProviderCallIdMetadataKey

protected const string RequiresProviderCallIdMetadataKey = "requires_provider_call_id"

Field Value

string

StreamIndexExplicitMetadataKey

protected const string StreamIndexExplicitMetadataKey = "stream_index_explicit"

Field Value

string

StreamItemIdMetadataKey

protected const string StreamItemIdMetadataKey = "stream_item_id"

Field Value

string

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

jsonData string
exception Exception
diagnostics StreamDiagnostics

Returns

StreamingContent

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

assistantMessage Message

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

usage JsonElement

Returns

TokenUsage

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

jsonData string
options StreamOptions

Returns

OpenAICompatibleService.OpenAIStreamChunk

StreamCompletionAsync(Message, Func<string, Task>)

public override Task StreamCompletionAsync(Message message, Func<string, Task> messageReceivedAsync)

Parameters

message Message
messageReceivedAsync Func<string, Task>

Returns

Task

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

options StreamOptions
useFunctions bool
policy FunctionCallingPolicy
cancellationToken CancellationToken

Returns

IAsyncEnumerable<StreamingContent>

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

doneMarkerReceived bool
completionEventReceived bool
diagnostics StreamDiagnostics

Returns

StreamingContent