Last active
December 27, 2025 21:06
-
-
Save caglar10ur/52e49e9be5f64f5ac47107810c9e60fb to your computer and use it in GitHub Desktop.
outputschema-with-tool.go
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
| package main | |
| import ( | |
| "context" | |
| "encoding/json" | |
| "log" | |
| "os" | |
| "google.golang.org/adk/agent" | |
| "google.golang.org/adk/agent/llmagent" | |
| "google.golang.org/adk/cmd/launcher" | |
| "google.golang.org/adk/cmd/launcher/full" | |
| "google.golang.org/adk/model" | |
| "google.golang.org/adk/model/gemini" | |
| "google.golang.org/adk/session" | |
| "google.golang.org/adk/tool" | |
| "google.golang.org/adk/tool/functiontool" | |
| "google.golang.org/genai" | |
| ) | |
| func onBeforeModel(ctx agent.CallbackContext, req *model.LLMRequest) (*model.LLMResponse, error) { | |
| for _, tools := range req.Config.Tools { | |
| for _, decl := range tools.FunctionDeclarations { | |
| b, _ := json.MarshalIndent(decl, "", " ") | |
| log.Printf("decl: %s", string(b)) | |
| } | |
| } | |
| return nil, nil | |
| } | |
| func main() { | |
| ctx := context.Background() | |
| model, err := gemini.NewModel(ctx, "gemini-2.5-flash-lite", &genai.ClientConfig{ | |
| APIKey: os.Getenv("GOOGLE_API_KEY"), | |
| }) | |
| if err != nil { | |
| log.Fatalf("Failed to create model: %v", err) | |
| } | |
| type SumArgs struct { | |
| A int `json:"a"` | |
| B int `json:"b"` | |
| } | |
| type SumResult struct { | |
| Sum int `json:"sum"` | |
| } | |
| handler := func(ctx tool.Context, input SumArgs) (SumResult, error) { | |
| return SumResult{Sum: input.A + input.B}, nil | |
| } | |
| sumTool, err := functiontool.New(functiontool.Config{ | |
| Name: "sum", | |
| Description: "sums two integers", | |
| }, handler) | |
| if err != nil { | |
| log.Fatalf("Failed to create tool: %v", err) | |
| } | |
| a, err := llmagent.New(llmagent.Config{ | |
| Name: "root_agent", | |
| Model: model, | |
| Description: "You can sum and multiply numbers.", | |
| Instruction: "Get the numbers and operations from the user and perform the calculations.", | |
| Tools: []tool.Tool{ | |
| sumTool, | |
| }, | |
| OutputKey: "result", | |
| OutputSchema: &genai.Schema{ | |
| Type: genai.TypeObject, | |
| Description: "Schema sum agent", | |
| Properties: map[string]*genai.Schema{ | |
| "result": { | |
| Type: genai.TypeNumber, | |
| Description: "The result of the sum.", | |
| }, | |
| }, | |
| Required: []string{"result"}, | |
| }, | |
| BeforeModelCallbacks: []llmagent.BeforeModelCallback{onBeforeModel}, | |
| }) | |
| config := &launcher.Config{ | |
| AgentLoader: agent.NewSingleLoader(a), | |
| SessionService: session.InMemoryService(), | |
| } | |
| l := full.NewLauncher() | |
| if err = l.Execute(ctx, config, os.Args[1:]); err != nil { | |
| log.Fatalf("Run failed: %v\n\n%s", err, l.CommandLineSyntax()) | |
| } | |
| } |
Author
caglar10ur
commented
Dec 27, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment