Skip to content

Conversation

@apascal07
Copy link
Collaborator

Checklist (if applicable):

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @apascal07, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the robustness of the Go AI module by introducing a suite of new unit tests focused on the intricate handling of default values for prompt inputs. These tests validate that the system correctly interprets and applies defaults from various sources, including Go struct fields, explicit map inputs, and jsonschema tags, ensuring predictable behavior during prompt definition and rendering, especially concerning value precedence and schema generation.

Highlights

  • Comprehensive Default Value Testing: New tests have been added to thoroughly cover various scenarios for default values in prompt inputs, ensuring robust behavior.
  • WithInputType Default Handling: Tests confirm that WithInputType correctly captures default values from struct fields, handles zero values, and processes map inputs, establishing precedence for struct values over jsonschema defaults.
  • Prompt Rendering with Defaults: Verifies that DefinePrompt correctly applies default values during prompt rendering, allowing partial inputs to utilize defaults for missing optional fields and ensuring provided inputs override defaults.
  • jsonschema Default Reflection: Tests ensure that jsonschema default tags are accurately reflected in the generated input schema and that omitempty fields are not incorrectly marked as required.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
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 adds a comprehensive set of tests for default value handling in prompts, covering scenarios with structs, maps, zero values, and jsonschema default tags. The tests are well-structured and cover important edge cases.

My review includes a few suggestions:

  • A high-severity concern about an inconsistency in how WithInputType handles map[string]any versus structs, which could lead to bugs.
  • Several medium-severity suggestions to refactor repetitive test assertions into table-driven tests to improve code maintainability and readability.

Comment on lines +709 to +720
t.Run("map input is used directly as DefaultInput", func(t *testing.T) {
input := map[string]any{
"name": "default",
"age": 25,
}

opt := WithInputType(input).(*inputOptions)

if diff := cmp.Diff(input, opt.DefaultInput); diff != "" {
t.Errorf("DefaultInput should match map input, diff (-want +got):\n%s", diff)
}
})
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The test map input is used directly as DefaultInput confirms a behavior in WithInputType that creates an inconsistency in how default inputs are handled. When a map[string]any is provided, it bypasses the JSON marshaling/unmarshaling cycle that struct inputs go through.

This leads to different types for numeric values in DefaultInput:

  • For structs, int values become float64 (e.g., lines 677, 700).
  • For maps, int values remain int (as tested here).

This inconsistency can lead to subtle bugs if downstream code expects all numbers in DefaultInput to be float64. To ensure consistent behavior, it would be better to process all input types through the same JSON marshaling/unmarshaling logic in WithInputType.

I'd recommend updating WithInputType to remove the special case for map[string]any and then adjusting this test to expect float64 values, similar to the other tests in this file. Since I cannot suggest changes to option.go, here is how this test could be updated after that change:

t.Run("map input is converted consistently", func(t *testing.T) {
    input := map[string]any{
        "name": "default",
        "age":  25,
    }

    opt := WithInputType(input).(*inputOptions)

    expected := map[string]any{
        "name": "default",
        "age":  float64(25),
    }

    if diff := cmp.Diff(expected, opt.DefaultInput); diff != "" {
        t.Errorf("DefaultInput should be consistent with struct processing, diff (-want +got):\n%s", diff)
    }
})

Comment on lines +722 to +759
t.Run("jsonschema default tag is reflected in schema", func(t *testing.T) {
type TestInputWithDefaults struct {
Name string `json:"name" jsonschema:"default=guest"`
Age int `json:"age" jsonschema:"default=25"`
Active bool `json:"active" jsonschema:"default=true"`
}

opt := WithInputType(TestInputWithDefaults{}).(*inputOptions)

props, ok := opt.InputSchema["properties"].(map[string]any)
if !ok {
t.Fatal("expected properties in schema")
}

nameSchema, ok := props["name"].(map[string]any)
if !ok {
t.Fatal("expected name property in schema")
}
if nameSchema["default"] != "guest" {
t.Errorf("expected name default to be 'guest', got %v", nameSchema["default"])
}

ageSchema, ok := props["age"].(map[string]any)
if !ok {
t.Fatal("expected age property in schema")
}
if ageSchema["default"] != float64(25) {
t.Errorf("expected age default to be 25, got %v", ageSchema["default"])
}

activeSchema, ok := props["active"].(map[string]any)
if !ok {
t.Fatal("expected active property in schema")
}
if activeSchema["default"] != true {
t.Errorf("expected active default to be true, got %v", activeSchema["default"])
}
})
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This test contains repetitive checks for each field in the schema. To improve maintainability and readability, consider refactoring it into a table-driven test, similar to the pattern used in TestSchemaAsMapWithDefaults in go/internal/base/json_test.go.

	t.Run("jsonschema default tag is reflected in schema", func(t *testing.T) {
		type TestInputWithDefaults struct {
			Name   string `json:"name" jsonschema:"default=guest"`
			Age    int    `json:"age" jsonschema:"default=25"`
			Active bool   `json:"active" jsonschema:"default=true"`
		}

		opt := WithInputType(TestInputWithDefaults{}).(*inputOptions)

		props, ok := opt.InputSchema["properties"].(map[string]any)
		if !ok {
			t.Fatal("expected properties in schema")
		}

		tests := []struct {
			field    string
			expected any
		}{
			{"name", "guest"},
			{"age", float64(25)},
			{"active", true},
		}

		for _, tc := range tests {
			t.Run(tc.field, func(t *testing.T) {
				prop, ok := props[tc.field].(map[string]any)
				if !ok {
					t.Fatalf("expected %s property in schema", tc.field)
				}
				if prop["default"] != tc.expected {
					t.Errorf("expected default for %s to be %v, got %v", tc.field, tc.expected, prop["default"])
				}
			})
		}
	})

Comment on lines +299 to +331
t.Run("schema has jsonschema defaults exposed", func(t *testing.T) {
p := DefinePrompt(
reg, "schema-defaults-exposed",
WithPrompt("{{name}}"),
WithInputType(InputWithSchemaDefaults{Name: "admin", Priority: 10}),
)

desc := p.(api.Action).Desc()
schema := desc.InputSchema

props, ok := schema["properties"].(map[string]any)
if !ok {
t.Fatal("expected properties in input schema")
}

nameSchema, ok := props["name"].(map[string]any)
if !ok {
t.Fatal("expected name property in schema")
}

if nameSchema["default"] != "guest" {
t.Errorf("expected schema default to be 'guest', got %v", nameSchema["default"])
}

prioritySchema, ok := props["priority"].(map[string]any)
if !ok {
t.Fatal("expected priority property in schema")
}

if prioritySchema["default"] != float64(5) {
t.Errorf("expected schema default to be 5, got %v", prioritySchema["default"])
}
})
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This test has repetitive checks for schema properties. For better readability and maintainability, you could refactor this into a table-driven test. This would make it easier to add more properties to check in the future.

	t.Run("schema has jsonschema defaults exposed", func(t *testing.T) {
		p := DefinePrompt(
			reg, "schema-defaults-exposed",
			WithPrompt("{{name}}"),
			WithInputType(InputWithSchemaDefaults{Name: "admin", Priority: 10}),
		)

		desc := p.(api.Action).Desc()
		schema := desc.InputSchema

		props, ok := schema["properties"].(map[string]any)
		if !ok {
			t.Fatal("expected properties in input schema")
		}

		testCases := []struct {
			field    string
			expected any
		}{
			{"name", "guest"},
			{"priority", float64(5)},
		}

		for _, tc := range testCases {
			t.Run(tc.field, func(t *testing.T) {
				prop, ok := props[tc.field].(map[string]any)
				if !ok {
					t.Fatalf("expected %s property in schema", tc.field)
				}
				if prop["default"] != tc.expected {
					t.Errorf("expected schema default to be %v, got %v", tc.expected, prop["default"])
				}
			})
		}
	})

Comment on lines +333 to +358
t.Run("defaultInput in metadata uses struct values not jsonschema defaults", func(t *testing.T) {
p := DefinePrompt(
reg, "schema-defaults-metadata",
WithPrompt("{{name}}"),
WithInputType(InputWithSchemaDefaults{Name: "admin", Priority: 10}),
)

desc := p.(api.Action).Desc()
promptMeta, ok := desc.Metadata["prompt"].(map[string]any)
if !ok {
t.Fatal("expected prompt metadata")
}

defaultInput, ok := promptMeta["defaultInput"].(map[string]any)
if !ok {
t.Fatal("expected defaultInput in prompt metadata")
}

if defaultInput["name"] != "admin" {
t.Errorf("expected defaultInput name to be 'admin', got %v", defaultInput["name"])
}

if defaultInput["priority"] != float64(10) {
t.Errorf("expected defaultInput priority to be 10, got %v", defaultInput["priority"])
}
})
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Similar to the previous test, this one can be refactored into a table-driven test to reduce code duplication and improve maintainability.

	t.Run("defaultInput in metadata uses struct values not jsonschema defaults", func(t *testing.T) {
		p := DefinePrompt(
			reg, "schema-defaults-metadata",
			WithPrompt("{{name}}"),
			WithInputType(InputWithSchemaDefaults{Name: "admin", Priority: 10}),
		)

		desc := p.(api.Action).Desc()
		promptMeta, ok := desc.Metadata["prompt"].(map[string]any)
		if !ok {
			t.Fatal("expected prompt metadata")
		}

		defaultInput, ok := promptMeta["defaultInput"].(map[string]any)
		if !ok {
			t.Fatal("expected defaultInput in prompt metadata")
		}

		testCases := []struct {
			field    string
			expected any
		}{
			{"name", "admin"},
			{"priority", float64(10)},
		}

		for _, tc := range testCases {
			t.Run(tc.field, func(t *testing.T) {
				if defaultInput[tc.field] != tc.expected {
					t.Errorf("expected defaultInput %s to be %v, got %v", tc.field, tc.expected, defaultInput[tc.field])
				}
			})
		}
	})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant