Skip to content

Add --var template variable support to generate command with command-specific reserved key validation #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 30, 2025

This PR adds --var template variable support to the generate command, matching the functionality already available in the run command. This enables users to create flexible, reusable prompt files for test generation scenarios while preventing conflicts with reserved variables only where necessary.

Problem

The generate command previously only supported a fixed input variable when templating prompt files, while the run command supported custom template variables via --var name=value flags. This inconsistency made it difficult to create reusable prompt templates for test generation.

Solution

Added complete --var template variable support to the generate command by:

  1. Added CLI flag support: The --var flag can now be used multiple times to pass custom template variables
  2. Template variable parsing: Implemented shared util.ParseTemplateVariables() function used by both run and generate commands
  3. Command-specific reserved key validation: Prevents input from being used as a template variable key in the generate command only to avoid conflicts with the built-in variable
  4. Pipeline integration: Template variables are applied during test execution in runSingleTestWithContext()
  5. Backward compatibility: Existing input variable is preserved alongside custom variables

Usage

# Single variable
gh models generate --var name=Alice prompt.yml

# Multiple variables  
gh models generate --var name=Alice --var topic="machine learning" prompt.yml

# Reserved variable protection (generate command only)
gh models generate --var input=test prompt.yml  # Error: 'input' is a reserved variable name
gh models run --var input=test prompt.yml       # Works fine - input allowed in run command

Example prompt file with template variables:

name: "Customer Support Template"
messages:
  - role: system
    content: "You are a helpful assistant for {{name}}."
  - role: user
    content: "Tell me about {{topic}} in {{style}} style."

Implementation Details

  • Shared utility: Both run and generate commands use util.ParseTemplateVariables() for consistent behavior
  • Command-specific validation: Only the generate command validates against using "input" as a template variable to prevent conflicts with its built-in variable
  • Template data structure: {"input": inputValue, "name": "customValue", ...}
  • Error handling: Validates variable format, detects duplicate keys, prevents reserved key usage in generate command only
  • Integration point: Variables are applied during groundtruth generation when running individual tests

Testing

  • Added comprehensive unit tests for template variable parsing covering various test cases
  • Added integration test verifying template variable application in the pipeline
  • Added specific test verifying run command accepts "input" as template variable
  • Added specific test verifying generate command rejects "input" as template variable
  • All existing tests continue to pass

This change enables more flexible and parameterized prompt files for test generation while maintaining full backward compatibility and preventing variable conflicts only where necessary.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] Add var template variable support to generate command Add --var template variable support to generate command Jul 30, 2025
@Copilot Copilot AI requested a review from pelikhan July 30, 2025 16:10
Copilot finished work on behalf of pelikhan July 30, 2025 16:10
@pelikhan
Copy link
Contributor

Refactor the function to parse var string into a shared utility with the run command

Copy link
Contributor Author

Copilot AI commented Jul 30, 2025

Refactor the function to parse var string into a shared utility with the run command

Refactored the parseTemplateVariables function into a shared utility util.ParseTemplateVariables that both commands now use. This eliminates the code duplication while maintaining identical functionality and test coverage. Commit: 808ccb4

Copilot finished work on behalf of pelikhan July 30, 2025 16:37
@pelikhan pelikhan marked this pull request as ready for review July 30, 2025 21:27
@pelikhan pelikhan requested a review from a team as a code owner July 30, 2025 21:27
@Copilot Copilot AI review requested due to automatic review settings July 30, 2025 21:27
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@@ -50,6 +52,12 @@ func NewGenerateCommand(cfg *command.Config) *cobra.Command {
return fmt.Errorf("failed to parse flags: %w", err)
}

// Parse template variables from flags
templateVars, err := util.ParseTemplateVariables(cmd.Flags())
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that "input" is not a template bars key

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added validation to prevent "input" from being used as a template variable key. The util.ParseTemplateVariables function now returns a clear error: "'input' is a reserved variable name and cannot be used with --var". This prevents conflicts with the built-in "input" variable used for backward compatibility. Commit: b01b3b7

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

input is allowed for "run" command but not for "generate" command. Move the check to the generate command code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the "input" validation from the shared utility function to the generate command code. Now the run command allows "input" as a template variable while the generate command still prevents it to avoid conflicts with the built-in variable. Commit: 7a60a81

@Copilot Copilot AI changed the title Add --var template variable support to generate command Add --var template variable support to generate command with reserved key validation Jul 30, 2025
@Copilot Copilot AI requested a review from pelikhan July 30, 2025 21:43
Copilot finished work on behalf of pelikhan July 30, 2025 21:43
@Copilot Copilot AI changed the title Add --var template variable support to generate command with reserved key validation Add --var template variable support to generate command with command-specific reserved key validation Jul 30, 2025
Copilot finished work on behalf of pelikhan July 30, 2025 22:19
@pelikhan pelikhan merged commit df2c83f into pelikhan/promptpex Jul 31, 2025
2 checks passed
@pelikhan pelikhan deleted the copilot/fix-bd306c91-c6f4-4b6e-b4da-13d3904381f7 branch July 31, 2025 04:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants