Repository navigation
feat(validate): allow literal braces with {{ and }}, matching str.format - #19
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #13.
Problem.
validate.Templaterejected every{not followed byname}, 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 withunclosed or malformed placeholder.{{"answer": "..."}}failed withdoubled braces.Rule (the option agreed in the issue): Python
str.formatsyntax, the formatter both SDK READMEs demonstrate.{name}is a slot.{{and}}are a literal{and}.{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
scanreplaces the oldbraces()check plus the placeholder regexp.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 slotname.Tested
TestLiteralBraces:{{1, 2}}{{name}}as a literal, not a slot{{{name}}}as literal brace, slot, literal braceTestBraceTyposdrops the{{name}}case and adds{name}}, whichstr.formatalso rejects.str.formatrenders{"answer": "..."},{name}and{N}, and raisesSingle '}' encounteredfor{name}}. That matches the validator exactly.main,Templaterejects these templates (e.g.template uses doubled braces near "{{\"answer\": ...").TestLiteralBracesitself does not compile there becauseSlotsis new. With this changego vet ./...andgo test ./...pass.Follow-ups after this is tagged
deriveSlotsshould callvalidate.Slots. Today it would treat{{name}}as a slot and the publish would fail withdeclared slots never used.detectSlotsandvalidateTemplateneed the same rule (relevant to ui#29).