google/adk-go

Panic when Artifact tool is not properly setup

Open

#283 opened on 2025年11月13日

GitHub で見る
 (1 comment) (1 reaction) (0 assignees)Go (699 forks)github user discovery
buggood first issue

Repository metrics

Stars
 (8,108 stars)
PR merge metrics
 (PR metrics pending)

説明

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/adk@v0.1.0/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/adk@v0.1.0/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/adk@v0.1.0/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/adk@v0.1.0/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)
}

コントリビューターガイド