Skip to content

Commit d36ddd6

Browse files
fix: allow f.object().array() as input field (#452)
The validation logic was missing a case for the 'object' type, which caused an error when f.object().array() was used as an input field. This commit adds the missing case to the validation logic and includes a test to verify the fix. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 8372b13 commit d36ddd6

File tree

3 files changed

+76
-62
lines changed

3 files changed

+76
-62
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { vi, describe, it, expect } from 'vitest';
2+
3+
import { ax } from '../index.js';
4+
import { f } from './sig.js';
5+
6+
const sig = f()
7+
.input(
8+
'items',
9+
f
10+
.object(
11+
{
12+
id: f.number('Index'),
13+
name: f.string('Name'),
14+
},
15+
'Item'
16+
)
17+
.array()
18+
)
19+
.output(
20+
'classifiedItems',
21+
f
22+
.object(
23+
{
24+
id: f.number('Index'),
25+
label: f.string('Label'),
26+
},
27+
'Classification'
28+
)
29+
.array()
30+
)
31+
.build();
32+
33+
const gen = ax(sig);
34+
35+
import type { AxAIService, AxChatResponse } from '../ai/types.js';
36+
37+
const llm: AxAIService = {
38+
chat: vi.fn().mockResolvedValue({
39+
results: [
40+
{
41+
index: 0,
42+
content:
43+
'Classified Items: [{"id": 0, "label": "A"}, {"id": 1, "label": "B"}]',
44+
},
45+
],
46+
} as AxChatResponse),
47+
embed: vi.fn(),
48+
getOptions: vi.fn().mockReturnValue({}),
49+
setOptions: vi.fn(),
50+
getName: vi.fn(),
51+
getFeatures: vi.fn(),
52+
getModelList: vi.fn(),
53+
getMetrics: vi.fn(),
54+
getLogger: vi.fn(),
55+
getLastUsedChatModel: vi.fn(),
56+
getLastUsedEmbedModel: vi.fn(),
57+
getLastUsedModelConfig: vi.fn(),
58+
getId: vi.fn(),
59+
};
60+
61+
describe('f.object().array() on inputs', () => {
62+
it('should not throw a validation error for valid input', async () => {
63+
const result = await gen.forward(llm, {
64+
items: [
65+
{ id: 0, name: 'Foo' },
66+
{ id: 1, name: 'Bar' },
67+
],
68+
});
69+
expect(result.classifiedItems).toEqual([
70+
{ id: 0, label: 'A' },
71+
{ id: 1, label: 'B' },
72+
]);
73+
});
74+
});

src/ax/dsp/util.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export const validateValue = (
3333
return val instanceof Date || typeof val === 'string';
3434
case 'json':
3535
return typeof val === 'object' || typeof val === 'string';
36+
case 'object':
37+
return typeof val === 'object';
3638
default:
3739
return false; // Unknown or unsupported type
3840
}

src/ax/package-exports.test.ts

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)