Skip to content

Panic when Artifact tool is not properly setup #283

@verdverm

Description

@verdverm

Seems like this is something we could catch before reaching the terminal session, or at least provide an error instead of panicking

$ go run main.go                                                                                                                                                                                                                                                                      Agent!

User -> hello

Agent -> panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x18 pc=0x100a9eba0]

goroutine 1 [running]:
google.golang.org/adk/internal/toolinternal.(*internalArtifacts).List(0x10180c050?, {0x101c76638?, 0x140001f12c0?})
        <autogenerated>:1 +0x30
google.golang.org/adk/tool/loadartifactstool.(*artifactsTool).appendInitialInstructions(0x140001f1040?, {0x1012ad560, 0x140001f12c0}, 0x140001f1040)
        /Users/tony/go/pkg/mod/google.golang.org/[email protected]/tool/loadartifactstool/load_artifacts_tool.go:122 +0x68
google.golang.org/adk/tool/loadartifactstool.(*artifactsTool).ProcessRequest(0x140002a29a0, {0x1012ad560, 0x140001f12c0}, 0x140001f1040)
        /Users/tony/go/pkg/mod/google.golang.org/[email protected]/tool/loadartifactstool/load_artifacts_tool.go:115 +0x54
google.golang.org/adk/internal/llminternal.toolPreprocess({0x1012abba8, 0x14000103cb0}, 0x140001f1040, {0x140002a29c0?, 0x140001dd4d8?, 0x100502b08?})
        /Users/tony/go/pkg/mod/google.golang.org/[email protected]/internal/llminternal/base_flow.go:232 +0xdc
google.golang.org/adk/internal/llminternal.(*Flow).preprocess(0x140002872c0, {0x1012abba8, 0x14000103cb0}, 0x140001f1040)
        /Users/tony/go/pkg/mod/google.golang.org/[email protected]/internal/llminternal/base_flow.go:218 +0x300

...
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"google.golang.org/adk/agent/llmagent"
	"google.golang.org/adk/cmd/launcher/adk"
	"google.golang.org/adk/cmd/launcher/full"
	"google.golang.org/adk/model/gemini"
	"google.golang.org/adk/server/restapi/services"
	"google.golang.org/adk/tool"
	"google.golang.org/adk/tool/functiontool"
	"google.golang.org/adk/tool/loadartifactstool"
	"google.golang.org/genai"
)

func main() {
	fmt.Println("Agent!")

	ctx := context.Background()

	model, _ := gemini.NewModel(ctx, "gemini-2.5-flash", &genai.ClientConfig{
		APIKey: os.Getenv("GOOGLE_API_KEY"),
	})

	sumTool, err := makeSummer()
	if err != nil {
		log.Fatalf("Failed to create summer: %v", err)
	}

	agent, err := llmagent.New(llmagent.Config{
		Name:        "hello_time_agent",
		Model:       model,
		Description: "Tells the current time in a specified city.",
		Instruction: "You are a helpful assistant that tells the current time in a city.",
		Tools: []tool.Tool{
			loadartifactstool.New(),
			sumTool,
		},
	})
	if err != nil {
		log.Fatalf("Failed to create agent: %v", err)
	}

	config := &adk.Config{
		AgentLoader: services.NewSingleAgentLoader(agent),
	}

	l := full.NewLauncher()
	err = l.Execute(ctx, config, os.Args[1:])
	if err != nil {
		log.Fatalf("Error while executing agent: %v", err)
	}
}

type SumArgs struct {
	A int `json:"a"` // an integer to sum
	B int `json:"b"` // another integer to sum
}
type SumResult struct {
	Sum int `json:"sum"` // the sum of two integers
}

func makeSummer() (tool.Tool, error) {
	handler := func(ctx tool.Context, input SumArgs) SumResult {
		return SumResult{Sum: input.A + input.B}
	}
	return functiontool.New(functiontool.Config{
		Name:        "sum",
		Description: "sums two integers",
	}, handler)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions