Skip to content
Draft
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
4 changes: 2 additions & 2 deletions typescript/src/typechat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,14 @@ export function createJsonTranslator<T extends object>(model: TypeChatLanguageMo
function stripNulls(obj: any) {
let keysToDelete: string[] | undefined;
for (const k in obj) {
const value = obj[k];
let value = obj[k];
if (value === null) {
(keysToDelete ??= []).push(k);
}
else {
if (Array.isArray(value)) {
if (value.some(x => x === null)) {
obj[k] = value.filter(x => x !== null);
value = obj[k] = value.filter(x => x !== null);
}
}
if (typeof value === "object") {
Expand Down
26 changes: 26 additions & 0 deletions typescript/test/failure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,32 @@ import { createModuleTextFromProgram, createTypeScriptJsonValidator } from "../d
import { createZodJsonValidator } from "../dist/zod/index.js";

type Example = { value: number };

describe("stripNulls nested arrays", () => {
for (const { groups, expected } of [
{ groups: [null, [null, 1]], expected: [[1]] },
{ groups: [[null, 1], null, [null, null]], expected: [[1], []] }
]) {
it(`normalizes nested arrays after filtering ${JSON.stringify(groups)}`, async () => {
const validator = createTypeScriptJsonValidator<{ groups: number[][] }>(
"export interface Data { groups: number[][]; }", "Data");
try {
const model: TypeChatLanguageModel = {
complete: async () => success(JSON.stringify({ groups }))
};
const translator = createJsonTranslator(model, validator);
translator.stripNulls = true;
translator.attemptRepair = false;
const result = await translator.translate("Return groups");
assert.ok(result.success);
assert.deepEqual(result.data.groups, expected);
}
finally {
validator.close();
}
});
}
});
type IsUnknown<T> = unknown extends T ? keyof T extends never ? true : false : false;

class GetterFailure {
Expand Down