Skip to content

feat(go/ai): support middleware in dotprompt frontmatter#5229

Merged
apascal07 merged 1 commit intomainfrom
ap/go-prompt-middleware
May 5, 2026
Merged

feat(go/ai): support middleware in dotprompt frontmatter#5229
apascal07 merged 1 commit intomainfrom
ap/go-prompt-middleware

Conversation

@apascal07
Copy link
Copy Markdown
Collaborator

@apascal07 apascal07 commented May 4, 2026

Adds Go runtime support for the dotprompt use: frontmatter field, mirroring the JS-side work in #5223. Each entry may be a bare middleware name or a map with name and optional config, matching the TypeScript shape.

---
model: googleai/gemini-flash-latest
use:
  - name: retry
    config:
      maxAttempts: 3
---
{{ role "system" }}
You are a helpful assistant.

The references travel through []Middleware via an unexported middlewareRefArg adapter, which configsToRefs strips before constructing the MiddlewareRef. This routes resolution through the registry-lookup path rather than the in-process fast path used for Go-defined middleware, so JSON-shaped configs from YAML propagate untouched.

Errors surface at load time for malformed input (non-list use:, missing name, empty entries) and at execute time for names that do not resolve in the registry.

Checklist:

  • PR title is following https://www.conventionalcommits.org/en/v1.0.0/
  • Tested (unit tests in go/ai/middleware_test.go and go/ai/prompt_test.go covering the parser, the lazy adapter, end-to-end load + execute, unregistered names, and invalid shapes)
  • Docs updated

Wire the dotprompt `use:` field through the Go prompt loader so
middleware references travel from frontmatter to runtime execution,
mirroring the JS implementation. Entries may be a bare middleware
name or a map with `name` and optional `config`.

References are carried through `configsToRefs` via an unexported
`middlewareRefArg` adapter that is stripped before reaching
`resolveRefs`, routing resolution through the registry (rather than
the in-process fast path) so config payloads propagate untouched.
@github-actions github-actions Bot added the go label May 4, 2026
@apascal07 apascal07 requested a review from MichaelDoyle May 4, 2026 21:02
@apascal07 apascal07 marked this pull request as ready for review May 4, 2026 21:02
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces support for lazy middleware references in dotprompt frontmatter, allowing middleware to be resolved via the registry. It includes the necessary internal types, parsing logic, and comprehensive test coverage. I have provided a suggestion to simplify the error handling in prompt.go by removing redundant error wrapping.

Comment thread go/ai/prompt.go
Copy link
Copy Markdown
Contributor

@MichaelDoyle MichaelDoyle left a comment

Choose a reason for hiding this comment

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

I have't verified in the Dev UI yet, but this matches what I expect.

@apascal07 apascal07 merged commit fb657b3 into main May 5, 2026
12 checks passed
@apascal07 apascal07 deleted the ap/go-prompt-middleware branch May 5, 2026 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants