Skip to content

[Go] The tool does not preserve the original data types of its inputs #3414

@dishaprakash

Description

@dishaprakash

Describe the bug
I have created a tool with the DefineToolWithInputSchema which is then run using the RunRaw method. The parameter of the tool is of the type integer (in the input schema) and the same is passed as the input to the RunRaw method , but the data type is not preserved when the input is passed to the actual tool invocation function. Within the invocation function the integer inputs are available as float64 type.
The same has been observed while using the genkit.Generate function as well.

To Reproduce
Code used:

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"log"

	"github.com/firebase/genkit/go/ai"
	"github.com/firebase/genkit/go/genkit"
	"github.com/firebase/genkit/go/plugins/googlegenai"
	"github.com/invopop/jsonschema"
)

func main() {
	ctx := context.Background()
	g, err := genkit.Init(ctx,
		genkit.WithPlugins(&googlegenai.GoogleAI{}),
		genkit.WithDefaultModel("googleai/gemini-1.5-flash"),
	)
	if err != nil {
		log.Fatalf("Failed to init genkit: %v\n", err)
	}

	inputSchema := map[string]any{
		"type": "object",
		"properties": map[string]any{
			"id": map[string]any{
				"name":        "id",
				"description": "The id to be printed.",
				"type":        "integer",
			}},
		"required": []any{"id"},
	}
	schemaByte, err := json.Marshal(inputSchema)
	if err != nil {
		log.Fatal(err)
	}

	var schema *jsonschema.Schema
	if err := json.Unmarshal(schemaByte, &schema); err != nil {
		log.Fatal("error converting input schema into json schema for tool")
	}

	executeFn := func(ctx *ai.ToolContext, input any) (string, error) {
		inputMap, ok := input.(map[string]any)
		if !ok {
			// If the input is not a map, return an error indicating the type mismatch.
			log.Fatalf("tool input expected map[string]any, got %T", input)
		}
		id := inputMap["id"]
		fmt.Printf("input received of type '%T'", id)

		strResult := fmt.Sprintf("%v", id)
		return strResult, nil
	}

	genkitTool := genkit.DefineToolWithInputSchema(
		g,
		"example_tool",
		"prints the id given to the tool",
		schema,
		executeFn,
	)

	resp, err := genkitTool.RunRaw(ctx, map[string]any{
		"id": int64(1),
	})
	if err != nil {
		log.Fatal("error invoking the tool")
	}
	fmt.Println(resp)

}

Output:
input received of type 'float64'1

Expected behavior
The data type of the inputs in the tool invocation function should be consistent with what was passed into the RunRaw function

Screenshots
If applicable, add screenshots to help explain your problem.

Runtime (please complete the following information):

  • OS: Linux

** Go version

  • go version go1.24.4 linux/amd64

Additional context
Using github.com/firebase/genkit/go v0.6.2
The problem seems to be because of JSON marshalling and unmarshalling of the inputs before passing it to the invocation function which wrongly categorises integer inputs as float64

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinggo

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions