Skip to content

feat(validate): allow literal braces with {{ and }}, matching str.format - #19

Merged
sujal-maheshwari2004 merged 1 commit into
mainfrom
feat/brace-escapes
Nov 15, 2026
Merged

sujal-maheshwari2004 merged 1 commit into
mainfrom
feat/brace-escapes

Conversation

@sujal-maheshwari2004

@sujal-maheshwari2004 sujal-maheshwari2004 commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Fixes #13.

Problem. validate.Template rejected every { not followed by name}, and rejected {{ outright, so a template could not contain a literal brace in any form. Prompts with JSON output examples, which are very common, were unpublishable:

  • Reply as JSON: {"answer": "..."} failed with unclosed or malformed placeholder.
  • {{"answer": "..."}} failed with doubled braces.

Rule (the option agreed in the issue): Python str.format syntax, the formatter both SDK READMEs demonstrate.

  • {name} is a slot.
  • {{ and }} are a literal { and }.
  • Any other { or } is rejected, as before, with an error that now suggests the escape.

Compatibility. {{ was an error until now, so every template that validated before validates the same way, with the same slots. Nothing stored changes meaning.

Change

  • A single scan replaces the old braces() check plus the placeholder regexp.
  • New exported validate.Slots(template) returns slots in order of first use, using the same rules. Clients that derive slots should use it rather than \{(\w+)\}, which would read {{name}} as slot name.

Tested

  • New TestLiteralBraces:
    • a JSON example
    • {{1, 2}}
    • {{name}} as a literal, not a slot
    • {{{name}}} as literal brace, slot, literal brace
    • repeated slots
  • TestBraceTypos drops the {{name}} case and adds {name}}, which str.format also rejects.
  • Same templates through Python: str.format renders {"answer": "..."}, {name} and {N}, and raises Single '}' encountered for {name}}. That matches the validator exactly.
  • On main, Template rejects these templates (e.g. template uses doubled braces near "{{\"answer\": ..."). TestLiteralBraces itself does not compile there because Slots is new. With this change go vet ./... and go test ./... pass.

Follow-ups after this is tagged

  • priompt: bump proto. The server validates on write and serve, so publishing JSON examples starts working.
  • cli: deriveSlots should call validate.Slots. Today it would treat {{name}} as a slot and the publish would fail with declared slots never used.
  • ui: detectSlots and validateTemplate need the same rule (relevant to ui#29).
  • Docs: add a short "literal braces" note to the prompt-writing pages.

Any { not followed by name} was rejected and {{ was rejected outright, so no
template could contain a literal brace: prompts with JSON output examples,
which are common, could not be published.

{{ and }} now mean a literal { and }, exactly as in Python's str.format, so
template.format(...) keeps working; {name} is a slot; any other { or } is
still rejected. Templates that validated before are unaffected ({{ was an
error until now). Adds validate.Slots so clients derive slots with the same
rules instead of a regexp that would read {{name}} as a slot.

Fixes #13
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.

Templates cannot contain literal braces, so JSON examples are unpublishable

1 participant