Skip to content
Merged
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 ts/packages/aiclient/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ Supported services:

The library includes support for getting settings needed to call these services from environment variables.

## Incomplete Responses API output

The Responses API adapter rejects `status: "incomplete"` responses and throws on
`response.incomplete` stream events, even when partial text is present. If the
provider supplies a nonempty string in `incomplete_details.reason` (for example,
`max_output_tokens` or `content_filter`), the error includes that reason:
`Model response is incomplete (reason: max_output_tokens)`. Missing or malformed
reasons retain the generic `Model response is incomplete` error. This only changes
diagnostics; it does not accept partial output or change request or retry policy.

## Native decisions

`createDecisionModel({ config?, provider? })` returns a `DecisionResult<DecisionModel>`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,19 @@ type ResponsesUsage = {

type ResponsesResult = {
status?: string;
incomplete_details?: { reason?: string };
output_text?: string;
output?: ResponsesOutputItem[];
usage?: ResponsesUsage;
};

function incompleteResponseMessage(result: ResponsesResult): string {
const reason = result.incomplete_details?.reason;
return typeof reason === "string" && reason.trim().length > 0
? `${incompleteOutputMessage} (reason: ${reason})`
: incompleteOutputMessage;
}

export class ResponsesWireApiProvider implements ProviderAdapter {
readonly wireApi: WireApi = "responses";

Expand Down Expand Up @@ -100,7 +108,7 @@ export class ResponsesWireApiProvider implements ProviderAdapter {
parseResponse(data: unknown, _request: ModelRequest): Result<string> {
const res = data as ResponsesResult;
if (res.status === "incomplete") {
return error(incompleteOutputMessage);
return error(incompleteResponseMessage(res));
}
if (typeof res.output_text === "string") {
return success(res.output_text);
Expand Down Expand Up @@ -131,13 +139,13 @@ export class ResponsesWireApiProvider implements ProviderAdapter {
const evt = JSON.parse(raw) as {
type?: string;
delta?: string;
response?: {
usage?: ResponsesUsage;
};
response?: ResponsesResult;
};
const piece: StreamPiece = {};
if (evt.type === "response.incomplete") {
throw new Error(incompleteOutputMessage);
throw new Error(
incompleteResponseMessage(evt.response ?? {}),
);
}
if (
evt.type === "response.output_text.delta" &&
Expand Down
71 changes: 71 additions & 0 deletions ts/packages/aiclient/test/providerAdapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,77 @@ describe("MessagesWireApiProvider", () => {
describe("ResponsesWireApiProvider", () => {
const adapter = adapterFor("responses");

test.each(["max_output_tokens", "content_filter"])(
"preserves incomplete response reason %s without accepting partial output",
(reason) => {
const response = {
status: "incomplete",
incomplete_details: { reason },
output_text: "partial",
};
const message = `Model response is incomplete (reason: ${reason})`;
expect(adapter.parseResponse(response, makeRequest())).toEqual({
success: false,
message,
});
const decoder = adapter.createStreamDecoder!(makeRequest());
expect(() =>
decoder.push(
JSON.stringify({
type: "response.incomplete",
response,
}),
),
).toThrow(message);
},
);

test.each([
undefined,
null,
{},
{ reason: "" },
{ reason: " " },
{ reason: null },
{ reason: 123 },
{ reason: {} },
])(
"keeps incomplete responses rejected with absent or malformed details %p",
(details) => {
const response = {
status: "incomplete",
incomplete_details: details,
output: [
{
type: "message",
content: [{ type: "output_text", text: "partial" }],
},
],
};
const message = "Model response is incomplete";
expect(adapter.parseResponse(response, makeRequest())).toEqual({
success: false,
message,
});
const decoder = adapter.createStreamDecoder!(makeRequest());
expect(() =>
decoder.push(
JSON.stringify({
type: "response.incomplete",
response,
}),
),
).toThrow(message);
},
);

test("rejects incomplete stream events without a response envelope", () => {
const decoder = adapter.createStreamDecoder!(makeRequest());
expect(() =>
decoder.push(JSON.stringify({ type: "response.incomplete" })),
).toThrow("Model response is incomplete");
});

test("preserves reader JSON schemas through Responses text.format", () => {
const jsonSchema = {
name: "memory_answer",
Expand Down
Loading