Preserve exact JSON validation semantics

This commit is contained in:
2026-08-11 22:55:17 +00:00
parent e83a3ce179
commit a93b799236
3 changed files with 347 additions and 18 deletions

View File

@@ -2424,6 +2424,60 @@ func TestWithProfilesRejectsCyclicExtraParams(t *testing.T) {
}
}
func TestPreparedStructuredOutputRetainsExactSchemaNumbers(t *testing.T) {
const schema = `{
"type": "number",
"const": 9007199254740993,
"minimum": 0.123456789012345678901234567890,
"maximum": 1e400,
"multipleOf": 0.0000000000000000001
}`
engine, err := promptkit.NewEngine(promptkit.Config{},
promptkit.WithPromptFS(publicStructuredPromptFS("exact.schema.prompt", "events.schema.json"), "prompts"),
promptkit.WithSchemaFS(fstest.MapFS{
"schemas/events.schema.json": &fstest.MapFile{Data: []byte(schema)},
}, "schemas"),
promptkit.WithProfiles(promptkit.Profile{
ID: "contract-fast", Endpoint: "http://example.test/v1", Model: "exact-model",
}),
)
if err != nil {
t.Fatalf("construct engine: %v", err)
}
prepared, err := engine.Prepare(context.Background(), promptkit.RunRequest{
PromptID: "exact.schema.prompt",
Inputs: map[string]promptkit.ArtifactRef{
"transcript": promptkit.Inline("Rin opens the gate."),
},
})
if err != nil {
t.Fatalf("prepare structured prompt: %v", err)
}
if prepared.StructuredOutput == nil || prepared.StructuredOutput.JSONSchema == nil {
t.Fatalf("structured output = %#v, want JSON Schema", prepared.StructuredOutput)
}
document, ok := prepared.StructuredOutput.JSONSchema.Schema.(map[string]any)
if !ok {
t.Fatalf("public schema = %#v, want object", prepared.StructuredOutput.JSONSchema.Schema)
}
want := map[string]string{
"const": "9007199254740993",
"minimum": "0.123456789012345678901234567890",
"maximum": "1e400",
"multipleOf": "0.0000000000000000001",
}
for name, wantNumber := range want {
got, ok := document[name].(json.Number)
if !ok {
t.Fatalf("public schema field %q = %#v, want json.Number", name, document[name])
}
if got.String() != wantNumber {
t.Fatalf("public schema field %q = %q, want %q", name, got, wantNumber)
}
}
}
func TestRunStructuredOutputWorksWithSchemaFS(t *testing.T) {
fake := &fakeLLMClient{response: &promptkit.GenerateResponse{Content: `{"events":[]}`}}
engine, err := promptkit.NewEngine(promptkit.Config{