From 53ac0990381283042d050db2e8e3f2afcd1e2c52 Mon Sep 17 00:00:00 2001 From: divit aggarwal Date: Tue, 29 Sep 2026 16:08:00 +0530 Subject: [PATCH] fix(ts): refuse a temperature that is not a list of 3 at construction, as Python does (#502 parity) The decode indexes the temperature by question type. Python's Agent refuses any other shape when it loads (#502), naming the value it got. The TypeScript Agent filled a short list with 1.0 per missing type and broadcast a bare number to all three, so a malformed config loaded cleanly and answered with a temperature the checkpoint never shipped, with no warning. Throw "Incompatible model: temperature must be a list of 3 floats" for a list of any other length or a non-list, from the constructor option and from cfg alike. A list of 3 is clamped exactly as before. Passing a bare number was never documented, but it did work before, and it now throws. This is a parity fix; it makes no prediction-accuracy claim. Co-Authored-By: Claude Opus 5.5 --- laya-ts/src/agent.ts | 11 ++++++++--- laya-ts/tests/lang-temperature.test.ts | 10 ++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/laya-ts/src/agent.ts b/laya-ts/src/agent.ts index 6a8c8566a..ba1201998 100644 --- a/laya-ts/src/agent.ts +++ b/laya-ts/src/agent.ts @@ -305,8 +305,13 @@ export class Agent extends HookRegistry { this.tok = opts.tok ?? defaultTokenizer(); const raw = (cfg.temperature ?? [1.0, 1.0, 1.0]) as unknown; this.temperatureRaw = raw; - const rawList = Array.isArray(raw) ? raw : [raw, raw, raw]; - this.temperature = [0, 1, 2].map((i) => clampTemperature(rawList[i] ?? 1.0)); + // The decode indexes this by question type, so refuse any other shape here, as Python's + // Agent.__init__ does (#502): a short list silently fell back to 1.0 per missing type. + if (!Array.isArray(raw) || raw.length !== 3) { + throw new Error(`Incompatible model: temperature must be a list of 3 floats, got ${JSON.stringify(raw)}`); + } + const rawList: unknown[] = raw; + this.temperature = rawList.map((t) => clampTemperature(t)); this.temperatureByOptionsRaw = (cfg.temperature_by_options ?? {}) as Record; this.temperatureByOptions = Object.fromEntries( Object.entries(this.temperatureByOptionsRaw).map(([k, v]) => [k, clampTemperature(v)]), @@ -316,7 +321,7 @@ export class Agent extends HookRegistry { ([k, v]) => [k, v, this.temperatureByOptions[k]] as [string, unknown, number], ), ...[0, 1, 2].map( - (i) => [`temperature[${i}]`, rawList[i] ?? 1.0, this.temperature[i]] as [string, unknown, number], + (i) => [`temperature[${i}]`, rawList[i], this.temperature[i]] as [string, unknown, number], ), ]; const rejected: string[] = []; diff --git a/laya-ts/tests/lang-temperature.test.ts b/laya-ts/tests/lang-temperature.test.ts index a86031990..d815245db 100644 --- a/laya-ts/tests/lang-temperature.test.ts +++ b/laya-ts/tests/lang-temperature.test.ts @@ -81,6 +81,16 @@ describe("lang_temperatures", () => { ).toThrow('Language override "de" temperature must be a list of 3 floats'); }); + it("rejects a base temperature that is not a list of 3 floats, as Python does (#502)", () => { + for (const temperature of [[1, 1], [1, 1, 1, 1], 2, "1"]) { + expect(() => new Agent({ provider: fakeProvider(), temperature } as any)).toThrow( + "Incompatible model: temperature must be a list of 3 floats", + ); + } + expect(() => new Agent({ provider: fakeProvider(), cfg: { temperature: [1, 2] } } as any)).toThrow("list of 3"); + expect(new Agent({ provider: fakeProvider(), temperature: [1, 2, 3] } as any).temperature).toEqual([1, 2, 3]); + }); + it("a null entry is an empty override (base raw temperature, no buckets)", async () => { const a = new Agent({ provider: fakeProvider(),