Skip to content
Open
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
17 changes: 15 additions & 2 deletions src/lib/ResponsesParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function maybeParseResponse<
ParsedT = Params extends null ? null : ExtractParsedContentFromParams<NonNullable<Params>>,
>(response: Response, params: Params): ParsedResponse<ParsedT> {
if (!params || !hasAutoParseableInput(params)) {
return {
const parsed = {
...response,
output_parsed: null,
output: response.output.map((item) => {
Expand All @@ -55,6 +55,12 @@ export function maybeParseResponse<
}
}),
};

if (needsOutputText(response, parsed)) {
addOutputText(parsed as Response);
}

return parsed;
}

return parseResponse(response, params);
Expand Down Expand Up @@ -95,7 +101,7 @@ export function parseResponse<
);

const parsed: Omit<ParsedResponse<ParsedT>, 'output_parsed'> = Object.assign({}, response, { output });
if (!Object.getOwnPropertyDescriptor(response, 'output_text')) {
if (needsOutputText(response, parsed)) {
addOutputText(parsed);
}

Expand Down Expand Up @@ -247,6 +253,13 @@ export function validateInputTools(tools: ChatCompletionTool[] | undefined) {
}
}

function needsOutputText(
response: Response,
target: { output_text?: Response['output_text'] | null },
): boolean {
return !Object.getOwnPropertyDescriptor(response, 'output_text') || target.output_text == null;
}

export function addOutputText(rsp: Response): void {
const texts: string[] = [];
for (const output of rsp.output) {
Expand Down