Skip to content

Bug: LoadPrompt pre-renders template with empty DataArgument, ignoring Execute() input parameters #3924

@Zereker

Description

@Zereker

Bug Description

When loading a .prompt file via LoadPrompt, the template is pre-rendered with an empty DataArgument{}, causing template variables to be replaced with empty values. Subsequent calls to prompt.Execute() with actual input parameters have no effect because the template was already "rendered" at load time.

Root Cause

In the LoadPrompt function in ai/prompt.go:

dpMessages, err := dotprompt.ToMessages(parsedPrompt.Template, &dotprompt.DataArgument{})

The LoadPrompt function calls ToMessages with an empty DataArgument{} during prompt loading. This renders the template immediately with empty values instead of preserving the template variables for later substitution.

Steps to Reproduce

  1. Create a .prompt file with template variables:
---
model: gemini-1.5-flash
---
Talk about {{topic}} in {{language}}
  1. Load and execute the prompt with different inputs:
prompt := genkit.LookupPrompt(g, "myPrompt")

// First execution
resp1, _ := prompt.Execute(ctx, ai.WithInput(map[string]any{
    "topic": "weather",
    "language": "English",
}))

// Second execution with different input
resp2, _ := prompt.Execute(ctx, ai.WithInput(map[string]any{
    "topic": "programming", 
    "language": "Chinese",
}))
  1. Both responses will be about the same topic (or empty), ignoring the input parameters.

Expected Behavior

Template variables should be preserved during LoadPrompt and only substituted when Execute() is called with actual input parameters.

Proposed Fix

Delay template rendering until Execute() is called, or pass the template string to the execution phase instead of pre-rendering with empty values.

Environment

  • Go version: 1.23
  • genkit version: v1.2.0

Related

This issue is separate from the dotprompt template sharing bug fixed in google/dotprompt#363

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions