-
Notifications
You must be signed in to change notification settings - Fork 549
feat: unify schema validation using jsonschema library #595
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
Open
GuyGoldenberg
wants to merge
1
commit into
google:main
Choose a base branch
from
GuyGoldenberg:feat/unify-schema-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+343
−499
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // Package schemautil provides utilities for schema conversion and validation. | ||
| package schemautil | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "strings" | ||
|
|
||
| "github.com/google/jsonschema-go/jsonschema" | ||
| "google.golang.org/genai" | ||
| ) | ||
|
|
||
| // GenaiToJSONSchema converts a genai.Schema to a jsonschema.Schema. | ||
| func GenaiToJSONSchema(gs *genai.Schema) (*jsonschema.Schema, error) { | ||
| if gs == nil { | ||
| return nil, nil | ||
| } | ||
|
|
||
| // Marshal to intermediate map | ||
| data, err := json.Marshal(gs) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var m map[string]any | ||
| if err := json.Unmarshal(data, &m); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Normalize type to lowercase (genai uses "STRING", jsonschema expects "string") | ||
| normalizeTypes(m) | ||
|
|
||
| // Marshal back and unmarshal to jsonschema.Schema | ||
| data, err = json.Marshal(m) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var js jsonschema.Schema | ||
| if err := json.Unmarshal(data, &js); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &js, nil | ||
| } | ||
|
|
||
| // normalizeTypes recursively lowercases type fields in the schema map. | ||
| func normalizeTypes(m map[string]any) { | ||
| if t, ok := m["type"].(string); ok { | ||
| m["type"] = strings.ToLower(t) | ||
| } | ||
|
|
||
| // Recurse into properties | ||
| if props, ok := m["properties"].(map[string]any); ok { | ||
| for _, v := range props { | ||
| if prop, ok := v.(map[string]any); ok { | ||
| normalizeTypes(prop) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Recurse into items | ||
| if items, ok := m["items"].(map[string]any); ok { | ||
| normalizeTypes(items) | ||
| } | ||
|
|
||
| // Recurse into anyOf | ||
| if anyOf, ok := m["anyOf"].([]any); ok { | ||
| for _, v := range anyOf { | ||
| if s, ok := v.(map[string]any); ok { | ||
| normalizeTypes(s) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // GenaiToResolvedJSONSchema converts a genai.Schema to a resolved jsonschema. | ||
| func GenaiToResolvedJSONSchema(gs *genai.Schema) (*jsonschema.Resolved, error) { | ||
| if gs == nil { | ||
| return nil, nil | ||
| } | ||
| js, err := GenaiToJSONSchema(gs) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return js.Resolve(nil) | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package schemautil | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "google.golang.org/genai" | ||
| ) | ||
|
|
||
| func TestGenaiToJSONSchema_Nil(t *testing.T) { | ||
| js, err := GenaiToJSONSchema(nil) | ||
| if err != nil { | ||
| t.Errorf("unexpected error: %v", err) | ||
| } | ||
| if js != nil { | ||
| t.Errorf("expected nil, got %v", js) | ||
| } | ||
| } | ||
|
|
||
| func TestGenaiToJSONSchema_BasicTypes(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| genaiType genai.Type | ||
| wantType string | ||
| }{ | ||
| {"string", genai.TypeString, "string"}, | ||
| {"integer", genai.TypeInteger, "integer"}, | ||
| {"number", genai.TypeNumber, "number"}, | ||
| {"boolean", genai.TypeBoolean, "boolean"}, | ||
| {"array", genai.TypeArray, "array"}, | ||
| {"object", genai.TypeObject, "object"}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| gs := &genai.Schema{Type: tt.genaiType} | ||
| js, err := GenaiToJSONSchema(gs) | ||
| if err != nil { | ||
| t.Fatalf("GenaiToJSONSchema error: %v", err) | ||
| } | ||
| if js.Type != tt.wantType { | ||
| t.Errorf("Type = %q, want %q", js.Type, tt.wantType) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGenaiToJSONSchema_Enum(t *testing.T) { | ||
| gs := &genai.Schema{ | ||
| Type: genai.TypeString, | ||
| Enum: []string{"red", "green", "blue"}, | ||
| } | ||
| js, err := GenaiToJSONSchema(gs) | ||
| if err != nil { | ||
| t.Fatalf("GenaiToJSONSchema error: %v", err) | ||
| } | ||
|
|
||
| if len(js.Enum) != 3 { | ||
| t.Fatalf("Enum length = %d, want 3", len(js.Enum)) | ||
| } | ||
| for i, want := range []string{"red", "green", "blue"} { | ||
| if js.Enum[i] != want { | ||
| t.Errorf("Enum[%d] = %v, want %q", i, js.Enum[i], want) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestGenaiToJSONSchema_EnumValidation(t *testing.T) { | ||
| gs := &genai.Schema{ | ||
| Type: genai.TypeString, | ||
| Enum: []string{"red", "green", "blue"}, | ||
| } | ||
| resolved, err := GenaiToResolvedJSONSchema(gs) | ||
| if err != nil { | ||
| t.Fatalf("GenaiToResolvedJSONSchema error: %v", err) | ||
| } | ||
|
|
||
| if err := resolved.Validate("red"); err != nil { | ||
| t.Errorf("Validate('red') error = %v, want nil", err) | ||
| } | ||
|
|
||
| if err := resolved.Validate("purple"); err == nil { | ||
| t.Error("Validate('purple') error = nil, want error") | ||
| } | ||
| } | ||
|
|
||
| func TestGenaiToJSONSchema_Properties(t *testing.T) { | ||
| gs := &genai.Schema{ | ||
| Type: genai.TypeObject, | ||
| Properties: map[string]*genai.Schema{ | ||
| "name": {Type: genai.TypeString, Description: "The name"}, | ||
| "age": {Type: genai.TypeInteger}, | ||
| }, | ||
| Required: []string{"name"}, | ||
| } | ||
| js, err := GenaiToJSONSchema(gs) | ||
| if err != nil { | ||
| t.Fatalf("GenaiToJSONSchema error: %v", err) | ||
| } | ||
|
|
||
| if len(js.Properties) != 2 { | ||
| t.Fatalf("Properties length = %d, want 2", len(js.Properties)) | ||
| } | ||
| if js.Properties["name"].Type != "string" { | ||
| t.Errorf("Properties[name].Type = %q, want string", js.Properties["name"].Type) | ||
| } | ||
| if js.Properties["name"].Description != "The name" { | ||
| t.Errorf("Properties[name].Description = %q, want 'The name'", js.Properties["name"].Description) | ||
| } | ||
| if js.Properties["age"].Type != "integer" { | ||
| t.Errorf("Properties[age].Type = %q, want integer", js.Properties["age"].Type) | ||
| } | ||
| if len(js.Required) != 1 || js.Required[0] != "name" { | ||
| t.Errorf("Required = %v, want [name]", js.Required) | ||
| } | ||
| } | ||
|
|
||
| func TestGenaiToJSONSchema_ObjectValidation(t *testing.T) { | ||
| gs := &genai.Schema{ | ||
| Type: genai.TypeObject, | ||
| Properties: map[string]*genai.Schema{ | ||
| "color": { | ||
| Type: genai.TypeString, | ||
| Enum: []string{"red", "green", "blue"}, | ||
| }, | ||
| }, | ||
| Required: []string{"color"}, | ||
| } | ||
| resolved, err := GenaiToResolvedJSONSchema(gs) | ||
| if err != nil { | ||
| t.Fatalf("GenaiToResolvedJSONSchema error: %v", err) | ||
| } | ||
|
|
||
| valid := map[string]any{"color": "red"} | ||
| if err := resolved.Validate(valid); err != nil { | ||
| t.Errorf("Validate(valid) error = %v, want nil", err) | ||
| } | ||
|
|
||
| invalid := map[string]any{"color": "purple"} | ||
| if err := resolved.Validate(invalid); err == nil { | ||
| t.Error("Validate(invalid) error = nil, want error for invalid enum") | ||
| } | ||
|
|
||
| missing := map[string]any{} | ||
| if err := resolved.Validate(missing); err == nil { | ||
| t.Error("Validate(missing) error = nil, want error for missing required") | ||
| } | ||
| } | ||
|
|
||
| func TestGenaiToJSONSchema_Array(t *testing.T) { | ||
| gs := &genai.Schema{ | ||
| Type: genai.TypeArray, | ||
| Items: &genai.Schema{ | ||
| Type: genai.TypeString, | ||
| Enum: []string{"a", "b", "c"}, | ||
| }, | ||
| } | ||
| resolved, err := GenaiToResolvedJSONSchema(gs) | ||
| if err != nil { | ||
| t.Fatalf("GenaiToResolvedJSONSchema error: %v", err) | ||
| } | ||
|
|
||
| valid := []any{"a", "b"} | ||
| if err := resolved.Validate(valid); err != nil { | ||
| t.Errorf("Validate(valid) error = %v, want nil", err) | ||
| } | ||
|
|
||
| invalid := []any{"a", "d"} | ||
| if err := resolved.Validate(invalid); err == nil { | ||
| t.Error("Validate(invalid) error = nil, want error for invalid enum in array item") | ||
| } | ||
| } | ||
|
|
||
| func TestGenaiToJSONSchema_NumericConstraints(t *testing.T) { | ||
| min := 0.0 | ||
| max := 100.0 | ||
| gs := &genai.Schema{ | ||
| Type: genai.TypeNumber, | ||
| Minimum: &min, | ||
| Maximum: &max, | ||
| } | ||
| resolved, err := GenaiToResolvedJSONSchema(gs) | ||
| if err != nil { | ||
| t.Fatalf("GenaiToResolvedJSONSchema error: %v", err) | ||
| } | ||
|
|
||
| if err := resolved.Validate(50.0); err != nil { | ||
| t.Errorf("Validate(50) error = %v, want nil", err) | ||
| } | ||
|
|
||
| if err := resolved.Validate(-1.0); err == nil { | ||
| t.Error("Validate(-1) error = nil, want error") | ||
| } | ||
|
|
||
| if err := resolved.Validate(101.0); err == nil { | ||
| t.Error("Validate(101) error = nil, want error") | ||
| } | ||
| } | ||
|
|
||
| func TestGenaiToResolvedJSONSchema_Nil(t *testing.T) { | ||
| resolved, err := GenaiToResolvedJSONSchema(nil) | ||
| if err != nil { | ||
| t.Errorf("unexpected error: %v", err) | ||
| } | ||
| if resolved != nil { | ||
| t.Error("expected nil resolved schema for nil input") | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolving the output schema on every
Runcall is inefficient. SincesetModelResponseToolis created per request in the processor, and the schema is derived from the agent's state, you should consider resolving the schema once and reusing it across invocations.