Skip to content

Instantly share code, notes, and snippets.

@caglar10ur
Last active December 27, 2025 21:06
Show Gist options
  • Select an option

  • Save caglar10ur/52e49e9be5f64f5ac47107810c9e60fb to your computer and use it in GitHub Desktop.

Select an option

Save caglar10ur/52e49e9be5f64f5ac47107810c9e60fb to your computer and use it in GitHub Desktop.
outputschema-with-tool.go
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())
}
}
@caglar10ur
Copy link
Author

# go build && ./outputschema-with-tool

User -> 41+1

Agent -> 
2025/12/27 13:05:02 decl: {
  "description": "Set your final response using the required output schema. Use this tool to provide your final structured answer instead of outputting text directly.",
  "name": "set_model_response",
  "parametersJsonSchema": {
    "description": "Schema sum agent",
    "properties": {
      "result": {
        "description": "The result of the sum.",
        "type": "NUMBER"
      }
    },
    "required": [
      "result"
    ],
    "type": "OBJECT"
  }
}
2025/12/27 13:05:02 decl: {
  "description": "sums two integers",
  "name": "sum",
  "parametersJsonSchema": {
    "type": "object",
    "required": [
      "a",
      "b"
    ],
    "properties": {
      "a": {
        "type": "integer"
      },
      "b": {
        "type": "integer"
      }
    },
    "additionalProperties": false
  },
  "responseJsonSchema": {
    "type": "object",
    "required": [
      "sum"
    ],
    "properties": {
      "sum": {
        "type": "integer"
      }
    },
    "additionalProperties": false
  }
}
{"result":42}

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