Skip to content

Commit

Permalink
fix(js/ai/prompt): delete null descriptions returned by dotprompt (#1687
Browse files Browse the repository at this point in the history
)
  • Loading branch information
pavelgj authored Jan 29, 2025
1 parent af71fd7 commit a018679
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
6 changes: 6 additions & 0 deletions js/ai/src/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,12 @@ function loadPrompt(
if (variant) {
promptMetadata.variant = variant;
}

// dotprompt can set null description on the schema, which can confuse downstream schema consumers
if (promptMetadata.output?.schema?.description === null) {
delete promptMetadata.output?.schema?.description;
}

return {
name: registryDefinitionKey(name, variant ?? undefined, ns),
model: promptMetadata.model,
Expand Down
24 changes: 24 additions & 0 deletions js/core/tests/schema_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { describe, it } from 'node:test';
import {
ValidationError,
parseSchema,
toJsonSchema,
validateSchema,
z,
} from '../src/schema.js';
Expand Down Expand Up @@ -138,3 +139,26 @@ describe('parse()', () => {
);
});
});

describe('toJsonSchema', () => {
it('converts zod to JSON schema', async () => {
assert.deepStrictEqual(
toJsonSchema({
schema: z.object({
output: z.string(),
}),
}),
{
$schema: 'http://json-schema.org/draft-07/schema#',
additionalProperties: true,
properties: {
output: {
type: 'string',
},
},
required: ['output'],
type: 'object',
}
);
});
});
9 changes: 9 additions & 0 deletions js/genkit/tests/prompts/schemaRef.prompt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
model: googleai/gemini-1.5-flash
input:
schema: myInputSchema
output:
schema: myOutputSchema
---

Write a poem about {{foo}}.
19 changes: 19 additions & 0 deletions js/genkit/tests/prompts_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,8 @@ describe.only('prompt', () => {
});
defineEchoModel(ai);
pm = defineProgrammableModel(ai);
ai.defineSchema('myInputSchema', z.object({ foo: z.string() }));
ai.defineSchema('myOutputSchema', z.object({ output: z.string() }));
});

it('loads from from the folder', async () => {
Expand Down Expand Up @@ -1014,6 +1016,23 @@ describe.only('prompt', () => {
});
});

it('resolved schema refs', async () => {
const prompt = ai.prompt('schemaRef');

const rendered = await prompt.render({ foo: 'bar' });
assert.deepStrictEqual(rendered.output?.jsonSchema, {
$schema: 'http://json-schema.org/draft-07/schema#',
additionalProperties: true,
properties: {
output: {
type: 'string',
},
},
required: ['output'],
type: 'object',
});
});

it('loads a varaint from from the folder', async () => {
const testPrompt = ai.prompt('test', { variant: 'variant' }); // see tests/prompts folder

Expand Down

0 comments on commit a018679

Please sign in to comment.