Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
c4bc48b
Added google_search snippet
ivanmkc Oct 1, 2025
22aeb16
Fix: Address Go style guide issues in google_search.go
ivanmkc Oct 1, 2025
880acfb
Migrated to new API
ivanmkc Oct 10, 2025
a62425a
Fixed tool info in md file
ivanmkc Oct 10, 2025
e8f5b43
Added partial check
ivanmkc Oct 10, 2025
ce52b86
Added function-tools sample
ivanmkc Sep 30, 2025
83f2a02
Migrated to new API
ivanmkc Oct 10, 2025
7b5edbc
Added partial check
ivanmkc Oct 10, 2025
c91f9e3
Ran linter
ivanmkc Oct 10, 2025
e357cf0
feat: add go fmt check
ivanmkc Oct 23, 2025
00d27d9
Run go fmt only on changed files
ivanmkc Oct 23, 2025
dac73a9
Added google_search snippet
ivanmkc Oct 1, 2025
3ded2cb
Fix: Address Go style guide issues in google_search.go
ivanmkc Oct 1, 2025
ac535b2
Migrated to new API
ivanmkc Oct 10, 2025
0521569
Added partial check
ivanmkc Oct 10, 2025
d8a61a8
Added function-tools sample
ivanmkc Sep 30, 2025
acc103f
Migrated to new API
ivanmkc Oct 10, 2025
a6439ce
Added partial check
ivanmkc Oct 10, 2025
55b0287
Ran linter
ivanmkc Oct 10, 2025
52c2ef4
feat: add go fmt check
ivanmkc Oct 23, 2025
010f8ee
Run go fmt only on changed files
ivanmkc Oct 23, 2025
ae6bf3d
Feat: Add Golang example for LLM agents
ivanmkc Oct 2, 2025
f2bd668
Added schema agent
ivanmkc Oct 2, 2025
50bf868
Added session support
ivanmkc Oct 2, 2025
803083c
Migrated to new API
ivanmkc Oct 10, 2025
a37bbc5
Added back OutputKey
ivanmkc Oct 14, 2025
ee07ab6
Added missing snippets
ivanmkc Oct 14, 2025
6be760e
Moved snipped to its own package
ivanmkc Oct 14, 2025
0523fbf
Minor cleanup
ivanmkc Oct 14, 2025
1df6d30
Reverted llm-agents.md
ivanmkc Oct 24, 2025
88e22cb
Added back correct changes to llm-agents.md
ivanmkc Oct 24, 2025
ae7ec4d
Updated to newest API
ivanmkc Oct 24, 2025
2cc9cef
Ran linter
ivanmkc Oct 24, 2025
14efb8b
WIP
ivanmkc Oct 1, 2025
6c74deb
WIP: Added the rest of the agents
ivanmkc Oct 2, 2025
4c87ddc
Added sequential agent
ivanmkc Oct 9, 2025
cd9044e
Migrated agents to new API
ivanmkc Oct 9, 2025
e5bbc6b
Fixed snippet tags
ivanmkc Oct 9, 2025
6e9b6c2
Updated parallel and sequential with OutputKey
ivanmkc Oct 11, 2025
91a41fc
Updated loop agent
ivanmkc Oct 11, 2025
ca8adc8
Cleanup
ivanmkc Oct 11, 2025
1442c88
Fixed and ran linter
ivanmkc Oct 24, 2025
b1908d5
Removed comment
ivanmkc Oct 24, 2025
241fd4d
Ran linter
ivanmkc Oct 29, 2025
4ce2a61
Added custom agent snippets
ivanmkc Oct 4, 2025
eb0a3ab
Migrated to new API
ivanmkc Oct 9, 2025
1ac75fe
Fixed incorrect info about channels since run returns an Iterator, no…
ivanmkc Oct 9, 2025
d58edd5
Cleanup and renaming
ivanmkc Oct 11, 2025
1ad5833
Ran linter
ivanmkc Oct 24, 2025
d8badd5
Fixed md
ivanmkc Oct 24, 2025
0b836f4
Added multi-agent docs
ivanmkc Oct 4, 2025
b851266
Updated to use AgentTool
ivanmkc Oct 14, 2025
f1f61f4
Split snippets
ivanmkc Oct 15, 2025
e42d258
Added missing file
ivanmkc Oct 15, 2025
4ccd30b
Ran linter and fixed API
ivanmkc Oct 24, 2025
4a4a09f
Added Models snippets
ivanmkc Oct 4, 2025
fffc201
Migrated to new API
ivanmkc Oct 9, 2025
eb2e400
Embedded imports
ivanmkc Oct 20, 2025
97dc51d
Ran linter
ivanmkc Oct 24, 2025
419e318
Added long running tool
ivanmkc Oct 10, 2025
417ef65
Ran linter and fixed API
ivanmkc Oct 23, 2025
9a04801
Switched to two-turn style
ivanmkc Oct 23, 2025
1c478e6
Ran linter
ivanmkc Oct 24, 2025
4f87fa1
Added callback snippets
ivanmkc Oct 11, 2025
be868a6
Added back imports
ivanmkc Oct 11, 2025
f1a2998
Added types of callbacks snippets
ivanmkc Oct 11, 2025
9fce999
feat(go): align callback snippets with other languages
ivanmkc Oct 12, 2025
81528dd
Ran linter and updated API
ivanmkc Oct 24, 2025
03b0412
Added tool callbacks
ivanmkc Oct 28, 2025
b6e5ebf
Added artifact snippets
ivanmkc Oct 8, 2025
fb5f83b
Use story.pdf instead of random bytes
ivanmkc Oct 9, 2025
e73dc3d
Fixed issue with artifact loading
ivanmkc Oct 9, 2025
708e1d1
Cleanup unneeded fixes
ivanmkc Oct 9, 2025
53644eb
Added back inadvertently removed GCS section
ivanmkc Oct 9, 2025
b8416ea
Addressed PR comments
ivanmkc Oct 24, 2025
62f9ed6
Ran linter
ivanmkc Oct 29, 2025
6ad92ed
feat: Added context snippets
ivanmkc Oct 15, 2025
5ebc5af
Fixed callback signature
ivanmkc Oct 16, 2025
c9247b0
Fixed additional callbacks
ivanmkc Oct 16, 2025
338a9bd
Added remaining snippets
ivanmkc Oct 16, 2025
c914892
Added conceptual runner example
ivanmkc Oct 16, 2025
076ea49
Removed duplicate code
ivanmkc Oct 16, 2025
8875b5d
Clean up tags and imports
ivanmkc Oct 16, 2025
717b11b
Updated API
ivanmkc Oct 24, 2025
1d43066
Twaked text
ivanmkc Oct 24, 2025
443c849
Ran linter
ivanmkc Oct 29, 2025
9404c91
Fixed broken API issues
ivanmkc Oct 31, 2025
1a86abb
Updated to latest Go ADK API
ivanmkc Nov 3, 2025
5ec3baa
Added func param info
ivanmkc Oct 23, 2025
6030485
Interleaved Py and Go
ivanmkc Oct 23, 2025
4e65de7
Revert mistakes
ivanmkc Nov 5, 2025
76b7ab4
Merge pull request #25 from ivanmkc/imkc--function-params
ivanmkc Nov 5, 2025
903c850
adding initial express mode samples
ToniCorinne Oct 15, 2025
3b5cd6e
updating go express_mode_examples
ToniCorinne Nov 4, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/go-fmt.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Go Fmt

on:
pull_request:
paths:
- '**.go'

jobs:
gofmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for all tags and branches
- uses: actions/setup-go@v5
with:
go-version: '1.21'
- name: Run gofmt on changed files
run: |
# List changed Go files in the pull request.
# The `|| true` is to prevent the command from failing if no go files are found
changed_files=$(git diff --name-only --diff-filter=ACMRT ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }} | grep '\.go$' || true)

if [ -z "$changed_files" ]; then
echo "No Go files changed in this PR."
exit 0
fi

echo "Checking formatting for the following files:"
echo "$changed_files"

# Check formatting of changed files
unformatted_files=$(gofmt -l $changed_files)

if [ -n "$unformatted_files" ]; then
echo "Unformatted Go files found:"
echo "$unformatted_files"
exit 1
else
echo "All changed Go files are formatted correctly."
fi
90 changes: 90 additions & 0 deletions docs/agents/custom-agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ The core of any custom agent is the method where you define its unique asynchron
* **Reactive Stream (`Flowable`):** It must return an `io.reactivex.rxjava3.core.Flowable<Event>`. This `Flowable` represents a stream of events that will be produced by the custom agent's logic, often by combining or transforming multiple `Flowable` from sub-agents.
* **`ctx` (InvocationContext):** Provides access to crucial runtime information, most importantly `ctx.session().state()`, which is a `java.util.concurrent.ConcurrentMap<String, Object>`. This is the primary way to share data between steps orchestrated by your custom agent.

=== "Go"

In Go, you implement the `Run` method as part of a struct that satisfies the `agent.Agent` interface. The actual logic is typically a method on your custom agent struct.

* **Signature:** `Run(ctx agent.InvocationContext) iter.Seq2[*session.Event, error]`
* **Iterator:** The `Run` method returns an iterator (`iter.Seq2`) that yields events and errors. This is the standard way to handle streaming results from an agent's execution.
* **`ctx` (InvocationContext):** The `agent.InvocationContext` provides access to the session, including state, and other crucial runtime information.
* **Session State:** You can access the session state through `ctx.Session().State()`.

**Key Capabilities within the Core Asynchronous Method:**

=== "Python"
Expand Down Expand Up @@ -127,6 +136,46 @@ The core of any custom agent is the method where you define its unique asynchron
* **Conditional:** `Flowable.defer()` to choose which `Flowable` to subscribe to based on a condition, or `filter()` if you're filtering events within a stream.
* **Iterative:** Operators like `repeat()`, `retry()`, or by structuring your `Flowable` chain to recursively call parts of itself based on conditions (often managed with `flatMapPublisher` or `concatMap`).

=== "Go"

1. **Calling Sub-Agents:** You invoke sub-agents by calling their `Run` method.

```go
// Example: Running one sub-agent and yielding its events
for event, err := range someSubAgent.Run(ctx) {
if err != nil {
// Handle or propagate the error
return
}
// Yield the event up to the caller
yield(event, nil)
}
```

2. **Managing State:** Read from and write to the session state to pass data between sub-agent calls or make decisions.
```go
// The `ctx` (`agent.InvocationContext`) is passed directly to your agent's `Run` function.
// Read data set by a previous agent
previousResult, err := ctx.Session().State().Get("some_key")
if err != nil {
// Handle cases where the key might not exist yet
}

// Make a decision based on state
if val, ok := previousResult.(string); ok && val == "some_value" {
// ... call a specific sub-agent ...
} else {
// ... call another sub-agent ...
}

// Store a result for a later step
if err := ctx.Session().State().Set("my_custom_result", "calculated_value"); err != nil {
// Handle error
}
```

3. **Implementing Control Flow:** Use standard Go constructs (`if`/`else`, `for`/`switch` loops, goroutines, channels) to create sophisticated, conditional, or iterative workflows involving your sub-agents.

## Managing Sub-Agents and State

Typically, a custom agent orchestrates other agents (like `LlmAgent`, `LoopAgent`, etc.).
Expand Down Expand Up @@ -162,6 +211,14 @@ Let's illustrate the power of custom agents with an example pattern: a multi-sta
```java
--8<-- "examples/java/snippets/src/main/java/agents/StoryFlowAgentExample.java:init"
```

=== "Go"

We define the `StoryFlowAgent` struct and a constructor. In the constructor, we store the necessary sub-agents and tell the `BaseAgent` framework about the top-level agents this custom agent will directly orchestrate.

```go
--8<-- "examples/go/snippets/agents/custom-agent/storyflow_agent.go:init"
```
---

### Part 2: Defining the Custom Execution Logic { #part-2-defining-the-custom-execution-logic }
Expand Down Expand Up @@ -195,6 +252,20 @@ Let's illustrate the power of custom agents with an example pattern: a multi-sta
3. Then, the `sequentialAgent's` Flowable executes. It calls the `grammar_check` then `tone_check`, reading `current_story` and writing `grammar_suggestions` and `tone_check_result` to the state.
4. **Custom Part:** After the sequentialAgent completes, logic within a `Flowable.defer` checks the "tone_check_result" from `invocationContext.session().state()`. If it's "negative", the `storyGenerator` Flowable is *conditionally concatenated* and executed again, overwriting "current_story". Otherwise, an empty Flowable is used, and the overall workflow proceeds to completion.

=== "Go"

The `Run` method orchestrates the sub-agents by calling their respective `Run` methods in a loop and yielding their events.

```go
--8<-- "examples/go/snippets/agents/custom-agent/storyflow_agent.go:executionlogic"
```
**Explanation of Logic:**

1. The initial `storyGenerator` runs. Its output is expected to be in the session state under the key `"current_story"`.
2. The `revisionLoopAgent` runs, which internally calls the `critic` and `reviser` sequentially for `max_iterations` times. They read/write `current_story` and `criticism` from/to the state.
3. The `postProcessorAgent` runs, calling `grammar_check` then `tone_check`, reading `current_story` and writing `grammar_suggestions` and `tone_check_result` to the state.
4. **Custom Part:** The code checks the `tone_check_result` from the state. If it's "negative", the `story_generator` is called *again*, overwriting the `current_story` in the state. Otherwise, the flow ends.

---

### Part 3: Defining the LLM Sub-Agents { #part-3-defining-the-llm-sub-agents }
Expand All @@ -216,6 +287,12 @@ These are standard `LlmAgent` definitions, responsible for specific tasks. Their
--8<-- "examples/java/snippets/src/main/java/agents/StoryFlowAgentExample.java:llmagents"
```

=== "Go"

```go
--8<-- "examples/go/snippets/agents/custom-agent/storyflow_agent.go:llmagents"
```

---

### Part 4: Instantiating and Running the custom agent { #part-4-instantiating-and-running-the-custom-agent }
Expand All @@ -234,6 +311,12 @@ Finally, you instantiate your `StoryFlowAgent` and use the `Runner` as usual.
--8<-- "examples/java/snippets/src/main/java/agents/StoryFlowAgentExample.java:story_flow_agent"
```

=== "Go"

```go
--8<-- "examples/go/snippets/agents/custom-agent/storyflow_agent.go:story_flow_agent"
```

*(Note: The full runnable code, including imports and execution logic, can be found linked below.)*

---
Expand All @@ -255,3 +338,10 @@ Finally, you instantiate your `StoryFlowAgent` and use the `Runner` as usual.
# Full runnable code for the StoryFlowAgent example
--8<-- "examples/java/snippets/src/main/java/agents/StoryFlowAgentExample.java:full_code"
```

=== "Go"

```go
# Full runnable code for the StoryFlowAgent example
--8<-- "examples/go/snippets/agents/custom-agent/storyflow_agent.go:full_code"
```
51 changes: 51 additions & 0 deletions docs/agents/llm-agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ First, you need to establish what the agent *is* and what it's *for*.
.build();
```

=== "Golang"

```go
// Example: Defining the basic identity
--8<-- "examples/go/snippets/agents/llm-agents/snippets/main.go:identity"
```


## Guiding the Agent: Instructions (`instruction`)

Expand Down Expand Up @@ -136,6 +143,13 @@ tells the agent:
.build();
```

=== "Golang"

```go
// Example: Adding instructions
--8<-- "examples/go/snippets/agents/llm-agents/snippets/main.go:instruction"
```

*(Note: For instructions that apply to *all* agents in a system, consider using
`global_instruction` on the root agent, detailed further in the
[Multi-Agents](multi-agents.md) section.)*
Expand Down Expand Up @@ -208,6 +222,12 @@ on the conversation and its instructions.
.build();
```

=== "Golang"

```go
--8<-- "examples/go/snippets/agents/llm-agents/snippets/main.go:tool_example"
```

Learn more about Tools in the [Tools](../tools/index.md) section.

## Advanced Configuration & Control
Expand Down Expand Up @@ -255,6 +275,14 @@ You can adjust how the underlying LLM generates responses using `generate_conten
.build();
```

=== "Golang"

```go
import "google.golang.org/genai"

--8<-- "examples/go/snippets/agents/llm-agents/snippets/main.go:gen_config"
```

### Structuring Data (`input_schema`, `output_schema`, `output_key`)

For scenarios requiring structured data exchange with an `LLM Agent`, the ADK provides mechanisms to define expected input and desired output formats using schema definitions.
Expand All @@ -266,6 +294,7 @@ For scenarios requiring structured data exchange with an `LLM Agent`, the ADK pr
* **`output_key` (Optional):** Provide a string key. If set, the text content of the agent's *final* response will be automatically saved to the session's state dictionary under this key. This is useful for passing results between agents or steps in a workflow.
* In Python, this might look like: `session.state[output_key] = agent_response_text`
* In Java: `session.state().put(outputKey, agentResponseText)`
* In Golang, within a callback handler: `ctx.State().Set(output_key, agentResponseText)`

=== "Python"

Expand Down Expand Up @@ -315,6 +344,14 @@ For scenarios requiring structured data exchange with an `LLM Agent`, the ADK pr
.build();
```

=== "Golang"

The input and output schema is a `google.genai.types.Schema` object.

```go
--8<-- "examples/go/snippets/agents/llm-agents/snippets/main.go:schema_example"
```

### Managing Context (`include_contents`)

Control whether the agent receives the prior conversation history.
Expand Down Expand Up @@ -344,6 +381,14 @@ Control whether the agent receives the prior conversation history.
.build();
```

=== "Golang"

```go
import "google.golang.org/adk/agent/llmagent"

--8<-- "examples/go/snippets/agents/llm-agents/snippets/main.go:include_contents"
```

### Planner

<div class="language-support-tag" title="">
Expand Down Expand Up @@ -551,6 +596,12 @@ call_agent("If it's raining in New York right now, what is the current temperatu
--8<-- "examples/java/snippets/src/main/java/agents/LlmAgentExample.java:full_code"
```

=== "Golang"

```go
--8<-- "examples/go/snippets/agents/llm-agents/main.go:full_code"
```

_(This example demonstrates the core concepts. More complex agents might incorporate schemas, context control, planning, etc.)_

## Related Concepts (Deferred Topics)
Expand Down
12 changes: 12 additions & 0 deletions docs/agents/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@ For deployed applications, a service account is the standard method.
// different availability or quota limitations.
```

=== "Go"

```go
import (
"google.golang.org/adk/agent/llmagent"
"google.golang.org/adk/model/gemini"
"google.golang.org/genai"
)

--8<-- "examples/go/snippets/agents/models/models.go:gemini-example"
```

!!!warning "Secure Your Credentials"
Service account credentials or API keys are powerful credentials. Never expose them publicly. Use a secret manager like [Google Secret Manager](https://cloud.google.com/secret-manager) to store and access them securely in production.

Expand Down
Loading
Loading