genkit icon indicating copy to clipboard operation
genkit copied to clipboard

[JS] Misleading or broken `.prompt(...).render(...)` generics

Open builtbybrayne opened this issue 1 year ago • 2 comments

Describe the bug There seems to be a mismatch between the claimed generics on the genkit.prompt<I, O>("...").render(...) method(s) and the actual behaviour of the code.

With the expected generics, the input variables are not correctly available in the dotprompt file. With a modified input that makes the rendering work, the generics are broken and I lose type-safety.

To Reproduce My reading of the generics on the genkit.prompt<I, O>("...").render(...) suggest that:

GIVEN I have the following zod schemas (with some simplification for brevity):

const aiInputSchema = z.object({
    time: z.string().describe("an ISO8601 string of the current time."),
    nlMessage: z.string().describe("the natural language message from our user."),
    ...    
});
const aiOutputSchema = z.object({
    reasoning: z.string().describe("a natural language scratchpad which the agent can use to think through the request and do reasoning"),
    nlMessage: z.string().describe("Natural language message sent to our user."),
    ...
});

AND the following my_prompt_file.prompt dotprompt file (with some simplification for brevity):

---
input:
    schema:
        nlMessage: string, the natural language message from our users
        time: string, the current time in ISO8601 format
        ...
output:
    schema:
        reasoning: string, a natural language scratchpad which the agent can use to think through the request and do reasoning        
        nlMessage: string, a natural language message for our user
        ...
---
{{role "system"}}
{{>common_universal_instructions}}

# Your Role
...

{{role "user"}}
--DATA--
time: {{time}}

--USER--
{{nlMessage}}

--INSTRUCTION--
...

THEN I should define my prompt/render calls like so:

const prompt = await ai.prompt<typeof aiInputSchema, typeof aiOutputSchema>("my_prompt_file").render({
    nlMessage:  "...",
    time: "....",      
});

Expected behavior

== Expectation == The inputs are correctly munged into the prompt template and available via the relevant {{time}} and {{nlMessage}} variables in the prompt template.

== Actual Behaviour == The variables are not available and therefore not rendered in the final prompt.

== Unexpected Workaround == If I wrap the inputs in an object with an input field as follows...

const prompt = await ai.prompt<typeof aiInputSchema, typeof aiOutputSchema>("my_prompt_file").render({
    input: {
        nlMessage:  "...",
        time: "....",      
    }      
});

... then the prompt rendering works. BUT this breaks the generics, which I have to remove and means that I lose type-safety, which is undesirable.

Screenshots N/A

Runtime (please complete the following information):

Node version v23.10.0

Additional context

I definitely want to use the .prompt(...).render(...) variation of the prompt generation. We are managing history ourselves. So we need to tap the rendered prompt before it gets handed off to generateStream(...).

builtbybrayne avatar Apr 08 '25 18:04 builtbybrayne

Hi, I was encountering the same rendering issue. The PR #2843 should fix this issue once it gets released.

EduardWayland avatar May 02 '25 10:05 EduardWayland

Closing as completed — fixed on main in PR #2843.

MichaelDoyle avatar Nov 18 '26 23:11 MichaelDoyle