Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/incomplete-model-stream-finish-reason.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@truefoundry/trueforge-assistant-ui-runtime": patch
"@truefoundry/trueforge-core": patch
"@truefoundry/trueforge-ui": patch
---

Keep a turn alive when the provider closes the model stream without a finish reason. The response
it did send is now kept and reported with the new `incomplete` finish reason, and the chat shows a
warning under the answer instead of failing the turn with "Response stream ended without a finish
reason". A stream that ends with no output at all still fails.
6 changes: 6 additions & 0 deletions packages/assistant-ui-runtime/src/incompleteResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* assistant-ui `data` part name for a model response the provider cut short. Emitted when a
* model message finishes as `incomplete`, so the reader knows the answer above it may be partial
* even though the turn itself succeeded.
*/
export const INCOMPLETE_RESPONSE_DATA_NAME = 'model.incomplete_response';
1 change: 1 addition & 0 deletions packages/assistant-ui-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export {
useTrueForgeTurnId,
useTrueForgeUpdateAgentSpec,
} from './hooks.js';
export { INCOMPLETE_RESPONSE_DATA_NAME } from './incompleteResponse.js';
export {
CANCELLATION_REASON_CUSTOM_KEY,
MESSAGE_CUSTOM_KEY,
Expand Down
6 changes: 6 additions & 0 deletions packages/assistant-ui-runtime/src/modelMessageContent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ThreadAssistantMessagePart } from '@assistant-ui/core';
import type { PendingResponseRef } from './foldPeerThreads.js';
import { INCOMPLETE_RESPONSE_DATA_NAME } from './incompleteResponse.js';
import { extractImagePartsFromModelMessage } from './modelMessageImageContent.js';
import type { ModelMessageEvent } from './server/index.js';

Expand Down Expand Up @@ -145,5 +146,10 @@ export function buildAssistantContent(message: ModelMessageEvent, context?: Tool
for (const toolCall of message.toolCalls ?? []) {
parts.push(toolCallToPart(toolCall, context));
}
// The provider closed the stream without saying why, so everything above is whatever arrived
// before it went away. The turn still succeeds; the notice trails the content it qualifies.
if (message.finishReason === 'incomplete') {
parts.push({ type: 'data', name: INCOMPLETE_RESPONSE_DATA_NAME, data: {} });
}
return parts;
}
22 changes: 22 additions & 0 deletions packages/assistant-ui-runtime/test/modelMessageContent.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest';
import type { ModelMessageEvent } from '../src/server/index.js';

import { INCOMPLETE_RESPONSE_DATA_NAME } from '../src/incompleteResponse.js';
import { buildAssistantContent } from '../src/modelMessageContent.js';

function modelMessage(event: Omit<ModelMessageEvent, 'type' | 'createdAt'>): ModelMessageEvent {
Expand Down Expand Up @@ -61,3 +62,24 @@ describe('buildAssistantContent tool results', () => {
expect(tool.result).toBe('found it');
});
});

describe('buildAssistantContent incomplete responses', () => {
it('trails the content with a notice when the provider cut the response short', () => {
const parts = buildAssistantContent(
modelMessage({ id: 'm1', threadId: 'main', content: 'partial answer', finishReason: 'incomplete' }),
);

expect(parts).toEqual([
{ type: 'text', text: 'partial answer' },
{ type: 'data', name: INCOMPLETE_RESPONSE_DATA_NAME, data: {} },
]);
});

it('adds no notice when the provider reported a finish reason', () => {
const parts = buildAssistantContent(
modelMessage({ id: 'm1', threadId: 'main', content: 'full answer', finishReason: 'stop' }),
);

expect(parts).toEqual([{ type: 'text', text: 'full answer' }]);
});
});
17 changes: 13 additions & 4 deletions packages/trueforge-core/src/core/llm/LLMTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,14 @@ export const CompletionUsageSchema = z
})
.openapi('CompletionUsage');

export const FinishReasonSchema = ChatCompletionChunkFinishReasonSchema.openapi('FinishReason');
/**
* The OpenAI reasons plus `incomplete`, which is ours: the provider closed the stream without
* sending a finish reason, so the message is whatever arrived before the connection ended.
*/
export const FinishReasonSchema = z
.enum([...ChatCompletionChunkFinishReasonSchema.options, 'incomplete'])
.describe('Why the model stopped generating; `incomplete` when the provider never sent a reason.')
.openapi('FinishReason');

export type ThinkingBlock = z.infer<typeof ThinkingBlockSchema>;
export type RedactedThinkingBlock = z.infer<typeof RedactedThinkingBlockSchema>;
Expand Down Expand Up @@ -180,11 +187,13 @@ export function getEmptyUsage(): CompletionUsage {
/**
* Extended choice type that includes thought_signature in the delta.
*/
interface ExtendedChoice extends Omit<ChatCompletionChunk.Choice, 'delta'> {
interface ExtendedChoice extends Omit<ChatCompletionChunk.Choice, 'delta' | 'finish_reason'> {
/**
* A chat completion delta generated by streamed model responses, with thought_signature support.
*/
delta: ExtendedDelta;
/** Widened over the OpenAI set so a stream that never reported one can say `incomplete`. */
finish_reason: FinishReason | null;
}

/**
Expand All @@ -202,8 +211,8 @@ export interface ExtendedChatCompletionChunk extends Omit<ChatCompletionChunk, '
export interface RawAssistantMessageWithUsage {
output: RawAssistantMessage;
usage: CompletionUsage;
/** null when the stream ended without a finish_reason chunk (e.g. provider omitted it). */
finish_reason: FinishReason | null;
/** `incomplete` when the stream ended without a finish_reason chunk (e.g. provider omitted it). */
finish_reason: FinishReason;
}

export const UNKNOWN_TOOL_NAME = 'unknown';
19 changes: 17 additions & 2 deletions packages/trueforge-core/src/core/llm/VercelAILLM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { createOpenAI } from '@ai-sdk/openai';
import type { OpenAICompatibleProviderOptions } from '@ai-sdk/openai-compatible';
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import type { JSONObject } from '@ai-sdk/provider';
import { InvalidResponseDataError } from '@ai-sdk/provider';
import type { ProviderOptions, ReasoningPart } from '@ai-sdk/provider-utils';
import type {
AssistantContent,
Expand Down Expand Up @@ -1163,8 +1164,10 @@ export function mapFinishReason(reason: FinishReason): RawAssistantMessageWithUs
return 'tool_calls';
case 'content-filter':
return 'content_filter';
// No harness equivalent for these — fall back to 'stop'.
// Reached only once a stream error was tolerated, since any other one is thrown before this.
case 'error':
return 'incomplete';
// The provider did report an ending, just not one the harness models.
case 'other':
return 'stop';
default: {
Expand Down Expand Up @@ -1313,7 +1316,8 @@ export async function* mapStreamToChunks({
return block;
};
let finalUsage: CompletionUsage = getEmptyUsage();
let finalFinishReason: RawAssistantMessageWithUsage['finish_reason'] = 'stop';
// Only a `finish-step` can promote this: a stream that stops before one really is incomplete.
let finalFinishReason: RawAssistantMessageWithUsage['finish_reason'] = 'incomplete';

for await (const part of stream) {
switch (part.type) {
Expand Down Expand Up @@ -1453,6 +1457,17 @@ export async function* mapStreamToChunks({

case 'error': {
const raw = part.error;
// A stream that closes without a finish reason is reported as an error part, after which
// the adapter still emits `finish`. What already arrived is a usable answer, so keep it and
// let `finish-step` mark it `incomplete`. A response with nothing in it is still a failure.
const hasOutput =
accumulatedText.length > 0 ||
accumulatedReasoning.length > 0 ||
accumulatedThinking.length > 0 ||
toolCallStates.size > 0;
if (InvalidResponseDataError.isInstance(raw) && hasOutput) {
break;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty start events bypass failure guard

Medium Severity

The empty-output guard treats a reasoning-start or tool-input-start as usable output, even when no text, reasoning, or tool arguments arrived. A stream that dies right after those start events is kept as incomplete instead of failing, so the user can get a blank answer plus warning, or AgentThread can continue and invoke a tool with empty or truncated arguments.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 94ef0c8. Configure here.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All invalid-response errors are swallowed

Medium Severity

Any InvalidResponseDataError is tolerated once some output exists, not only the openai-compatible “stream ended without a finish reason” case. Other invalid-payload failures from the AI SDK (malformed chunks, failed field validation, non-string tool-call ids) are then turned into a successful incomplete turn instead of failing.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 94ef0c8. Configure here.

const message = describeStreamError(raw);
// Preserve the original stream error as cause; toast/turn use .message only.
const cause = raw instanceof Error ? raw : new Error(message);
Expand Down
7 changes: 7 additions & 0 deletions packages/trueforge-core/src/core/runtime/AgentThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,13 @@ export class AgentThread {
});
const contextAssistantMessage = toContextAssistantMessage(assistantMessage);
const finishReason = result.value.finish_reason;
if (finishReason === 'incomplete') {
this.logger.warn('Provider closed the model stream without a finish reason', {
thread_id: this.threadId,
model: this.definition.modelName,
output_tokens: result.value.usage.output_tokens,
});
}
const agentAssistantMessage = buildModelMessageEvent({
assistantMessage: await enrichAssistantMessage({
assistantMessage: result.value.output,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function finishReasonErrorMessage({
outputTokens,
maxOutputTokens,
}: {
finishReason: FinishReason | null;
finishReason: FinishReason;
outputTokens: number;
maxOutputTokens: number | undefined;
}): string | undefined {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Stream processing: usage/finish-reason normalisation and the
* TextStreamPart → ExtendedChatCompletionChunk mapper.
*/
import { InvalidResponseDataError } from '@ai-sdk/provider';
import type {
FinishReason,
LanguageModelUsage,
Expand Down Expand Up @@ -63,6 +64,14 @@ async function* makeStream(parts: TextStreamPart<ToolSet>[]): AsyncGenerator<Tex
for (const part of parts) yield part;
}

/** What `@ai-sdk/openai-compatible` enqueues when the stream closes with no finish reason. */
function missingFinishReasonError(): InvalidResponseDataError {
return new InvalidResponseDataError({
data: undefined,
message: 'Response stream ended without a finish reason.',
});
}

async function drainStream(
gen: AsyncGenerator<ExtendedChatCompletionChunk, RawAssistantMessageWithUsage, unknown>,
): Promise<{ chunks: ExtendedChatCompletionChunk[]; final: RawAssistantMessageWithUsage }> {
Expand Down Expand Up @@ -169,7 +178,7 @@ describe('mapFinishReason', () => {
['length', 'length'],
['tool-calls', 'tool_calls'],
['content-filter', 'content_filter'],
['error', 'stop'],
['error', 'incomplete'],
['other', 'stop'],
];

Expand Down Expand Up @@ -644,6 +653,67 @@ describe('mapStreamToChunks', () => {
).rejects.toMatchObject({ message: 'upstream error', cause });
});

// `@ai-sdk/openai-compatible` reports a stream that closed with no finish reason this way, then
// still emits `finish-step`. The text the provider did send is a usable answer.
it('keeps the streamed output when the provider closed without a finish reason', async () => {
const { final } = await drainStream(
mapStreamToChunks({
stream: makeStream([
{ type: 'text-delta', id: 'txt-0', text: 'partial answer' },
{ type: 'error', error: missingFinishReasonError() },
makeFinishStep('error'),
]),
chunkMeta: CHUNK_META,
}),
);

expect(final.output.content).toBe('partial answer');
expect(final.finish_reason).toBe('incomplete');
});

it('keeps tool calls streamed before the provider closed without a finish reason', async () => {
const { final } = await drainStream(
mapStreamToChunks({
stream: makeStream([
{ type: 'tool-input-start', id: 'call-1', toolName: 'search' },
{ type: 'tool-input-delta', id: 'call-1', delta: '{"q":"hi"}' },
{ type: 'error', error: missingFinishReasonError() },
makeFinishStep('error'),
]),
chunkMeta: CHUNK_META,
}),
);

expect(final.output.tool_calls).toEqual([
{ id: 'call-1', type: 'function', function: { name: 'search', arguments: '{"q":"hi"}' } },
]);
expect(final.finish_reason).toBe('incomplete');
});

it('rejects when the provider closed without a finish reason and sent nothing at all', async () => {
const cause = missingFinishReasonError();
await expect(
drainStream(
mapStreamToChunks({
stream: makeStream([{ type: 'error', error: cause }, makeFinishStep('error')]),
chunkMeta: CHUNK_META,
}),
),
).rejects.toMatchObject({ message: cause.message, cause });
});

it('reports a stream that ends before any finish-step as incomplete', async () => {
const { final } = await drainStream(
mapStreamToChunks({
stream: makeStream([{ type: 'text-delta', id: 'txt-0', text: 'cut off' }]),
chunkMeta: CHUNK_META,
}),
);

expect(final.output.content).toBe('cut off');
expect(final.finish_reason).toBe('incomplete');
});

it('rejects on an abort part rather than returning the partial message as a clean stop', async () => {
await expect(
drainStream(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,14 @@ describe('AgentThread finish reason errors', () => {
const done = await runUntilDone({ finishReason, modelParams });
expect(done).toMatchObject({ status: 'error', error });
});

// The provider hung up without saying why, but what it sent is still an answer worth keeping.
it('finishes the turn with the partial output when the finish reason is incomplete', async () => {
const done = await runUntilDone({ finishReason: 'incomplete', modelParams: undefined });

expect(done).toMatchObject({
status: 'done',
output: { content: 'partial', finish_reason: 'incomplete' },
});
});
});
30 changes: 30 additions & 0 deletions packages/trueforge-ui/src/atoms/IncompleteResponseBanner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Icon } from '../icons/Icon.js';
import { cn } from './lib/cn.js';

export type IncompleteResponseBannerProps = {
className?: string;
};

/** Orange warning banner for a response the provider cut short, shown under the partial answer. */
export function IncompleteResponseBanner({ className }: IncompleteResponseBannerProps) {
return (
<div
role="status"
className={cn(
'aui-incomplete-response-root border-warning-bg bg-warning-bg/10 text-warning-bg mt-2 inline-flex w-fit max-w-full items-center gap-3 rounded-xl border px-3 py-1.5 text-sm font-medium leading-[1.4]',
className,
)}
>
<Icon name="circle-exclamation" size="1rem" className="text-warning-bg shrink-0" aria-hidden />
<span className="aui-incomplete-response-message min-w-0 break-words">
The model provider ended this response early, so it may be incomplete.
</span>
</div>
);
}

declare module '../theme/SlotsProvider.js' {
interface AtomSlots {
IncompleteResponseBanner: typeof IncompleteResponseBanner;
}
}
Comment on lines +26 to +30

@datadog-official datadog-official Bot Nov 20, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Low: Code Quality Violation

ES2015 module syntax is preferred over namespaces. (...read more)

Namespaces should be avoided as an outdated feature of TypeScript. Use module syntax instead.

Helpful? 👍/👎 View in Datadog  Leave us feedback  Documentation
🚩 Report as false positive. Flags the rule for review to improve detection accuracy.

Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
'use client';

import type { EnrichedPartState } from '@assistant-ui/react';
import { CONTEXT_COMPACTION_DATA_NAME } from '@truefoundry/trueforge-assistant-ui-runtime';
import {
CONTEXT_COMPACTION_DATA_NAME,
INCOMPLETE_RESPONSE_DATA_NAME,
} from '@truefoundry/trueforge-assistant-ui-runtime';

import { AssistantTextContainer } from './AssistantTextContainer.js';
import { ContextCompactionContainer } from './ContextCompactionContainer.js';
import { IncompleteResponseContainer } from './IncompleteResponseContainer.js';
import { MessageImageContainer } from './MessageImageContainer.js';
import { ToolCallContainer } from './ToolCallContainer.js';

/**
* Renders a part that is not grouped. Context-compaction `data` parts render here because they are the
* only data parts we emit today; other data / generative UI parts render nothing until they get a renderer.
* Renders a part that is not grouped. Only the `data` parts we emit ourselves have a renderer here;
* other data / generative UI parts render nothing until they get one.
*/
export function AssistantLeafPartContainer({ part }: { part: EnrichedPartState }) {
switch (part.type) {
Expand All @@ -25,6 +29,9 @@ export function AssistantLeafPartContainer({ part }: { part: EnrichedPartState }
if (part.name === CONTEXT_COMPACTION_DATA_NAME) {
return <ContextCompactionContainer />;
}
if (part.name === INCOMPLETE_RESPONSE_DATA_NAME) {
return <IncompleteResponseContainer />;
}
return null;
default:
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use client';

import { useAuiState } from '@assistant-ui/react';
import { INCOMPLETE_RESPONSE_DATA_NAME } from '@truefoundry/trueforge-assistant-ui-runtime';

import { useSlot } from '../theme/SlotsProvider.js';

export function IncompleteResponseContainer() {
const IncompleteResponseBanner = useSlot('IncompleteResponseBanner');
const part = useAuiState(s => s.part);

if (part.type !== 'data' || part.name !== INCOMPLETE_RESPONSE_DATA_NAME) {
return null;
}

return <IncompleteResponseBanner />;
}
Loading
Loading