Fixed a misleading tool calling error after a stopped or interrupted answer (#1202)

Co-authored-by: Thorsten Sommer <SommerEngineering@users.noreply.github.com>
This commit is contained in:
j-erlerandThorsten Sommer authored and GitHub committed 2026-11-20 14:46:27 +01:00
1 parent fb8f51dec4
commit ca7413d84c
18 files changed
+272 -76

No files matched your search

@@ -101,7 +101,9 @@ public sealed class ChatCompletionToolCallAccumulator(Func<ServerSentEvent, ILis
/// </returns>
/// <remarks>
/// The end of the stream is the end of the message. There is nothing else to wait for: a
/// "[DONE]" is not sent by every gateway, and a finish reason not by every one either.
/// "[DONE]" is not sent by every gateway, and a finish reason not by every one either. That
/// holds for a stream which the provider closed only. A stream which was cut off never gets
/// here: its adapter ends the round before, cf. <see cref="ServerSentEventStreamEnd"/>.
/// </remarks>
public ChatCompletionResponseMessage? Build()
{
@@ -18,7 +18,7 @@ public sealed class ChatCompletionToolCallingAdapter<TRequest>(
TextMessage systemPrompt, IDictionary<string, object> apiParameters,
IList<object> providerTools, bool mayAskForSequentialToolCalls,
IReadOnlyList<(ToolDefinition Definition, IToolImplementation Implementation)> runnableTools,
Func<ChatCompletionAPIRequest, CancellationToken, IAsyncEnumerable<ServerSentEvent>> streamRequestAsync,
Func<ChatCompletionAPIRequest, ServerSentEventStreamEnd, CancellationToken, IAsyncEnumerable<ServerSentEvent>> streamRequestAsync,
Func<ServerSentEvent, IList<ISource>> readSources,
ILogger logger)
: IToolCallingProviderAdapter where TRequest : ChatCompletionAPIRequest
@@ -71,14 +71,19 @@ public sealed class ChatCompletionToolCallingAdapter<TRequest>(
// behind it, fragment by fragment. The usage goes out with every round: which of them
// describes the conversation is the loop's decision, which knows which round this is.
//
var streamEnd = new ServerSentEventStreamEnd();
var accumulator = new ChatCompletionToolCallAccumulator(readSources);
await foreach (var serverSentEvent in streamRequestAsync(requestDto, token))
await foreach (var serverSentEvent in streamRequestAsync(requestDto, streamEnd, token))
{
var part = accumulator.Process(serverSentEvent);
if (part.HasContent || part.Usage.IsKnown)
yield return ToolCallingStreamEvent.TextDelta(new ContentStreamChunk(part.TextDelta, part.Sources, Usage: part.Usage));
}
// A stream which was cut off ends the round without an outcome, cf. IToolCallingProviderAdapter.ExecuteRoundAsync:
if (!streamEnd.IsReached)
yield break;
var message = accumulator.Build();
if (message is null)
yield break;
@@ -207,7 +207,7 @@ public sealed class ProviderOpenAI() : BaseProvider(LLMProviders.OPEN_AI, new Ur
baseInput,
additionalApiParameters,
runnableTools,
(requestDto, requestToken) => this.StreamResponsesRequest(requestDto, requestedSecret, requestToken));
(requestDto, streamEnd, requestToken) => this.StreamResponsesRequest(requestDto, requestedSecret, streamEnd, requestToken));
var loop = Program.SERVICE_PROVIDER.GetRequiredService<IToolCallingLoop>();
var loopContext = new ToolCallingLoopContext
@@ -296,9 +296,9 @@ public sealed class ProviderOpenAI() : BaseProvider(LLMProviders.OPEN_AI, new Ur
/// classification come from the shared stream reader, which the tool calling rounds used to
/// go without; reading the events is the adapter's business.
/// </remarks>
private IAsyncEnumerable<ServerSentEvent> StreamResponsesRequest(ResponsesAPIRequest requestDto, RequestedSecret requestedSecret, CancellationToken token)
private IAsyncEnumerable<ServerSentEvent> StreamResponsesRequest(ResponsesAPIRequest requestDto, RequestedSecret requestedSecret, ServerSentEventStreamEnd streamEnd, CancellationToken token)
{
return this.ReadServerSentEventsAsync("OpenAI", "responses call", RequestBuilder, token);
return this.ReadServerSentEventsAsync("OpenAI", "responses call", RequestBuilder, streamEnd, token);
async Task<HttpRequestMessage> RequestBuilder()
{
@@ -106,8 +106,10 @@ public sealed class ResponsesStreamAccumulator
return null;
//
// No closing event came, so the round is put back together from the items which did.
// Reasoning items are among them, which is what the next request needs to continue.
// No closing event came, although the provider closed the stream: some gateways never send
// it. So the round is put back together from the items which did come. Reasoning items are
// among them, which is what the next request needs to continue. A stream which was cut off
// never gets here: its adapter ends the round before, cf. ServerSentEventStreamEnd.
//
return new ResponsesResponse
{
@@ -15,7 +15,7 @@ namespace AIStudio.Provider.OpenAI;
/// </remarks>
public sealed class ResponsesToolCallingAdapter(Model chatModel, IList<object> baseInput, IDictionary<string, object> apiParameters,
IReadOnlyList<(ToolDefinition Definition, IToolImplementation Implementation)> runnableTools,
Func<ResponsesAPIRequest, CancellationToken, IAsyncEnumerable<ServerSentEvent>> streamRequestAsync) : IToolCallingProviderAdapter
Func<ResponsesAPIRequest, ServerSentEventStreamEnd, CancellationToken, IAsyncEnumerable<ServerSentEvent>> streamRequestAsync) : IToolCallingProviderAdapter
{
private readonly List<object> internalItems = [];
private readonly List<string> recordedRequestTexts = [];
@@ -59,14 +59,19 @@ public sealed class ResponsesToolCallingAdapter(Model chatModel, IList<object> b
// goes out with every round: which of them describes the conversation is the loop's
// decision, which knows which round this is.
//
var streamEnd = new ServerSentEventStreamEnd();
var accumulator = new ResponsesStreamAccumulator();
await foreach (var serverSentEvent in streamRequestAsync(request, token))
await foreach (var serverSentEvent in streamRequestAsync(request, streamEnd, token))
{
var part = accumulator.Process(serverSentEvent);
if (part.HasContent || part.Usage.IsKnown)
yield return ToolCallingStreamEvent.TextDelta(new ContentStreamChunk(part.TextDelta, part.Sources, Usage: part.Usage));
}
// A stream which was cut off ends the round without an outcome, cf. IToolCallingProviderAdapter.ExecuteRoundAsync:
if (!streamEnd.IsReached)
yield break;
var response = accumulator.Build();
if (response is null)
yield break;