Skip to content

Commit ddaaa78

Browse files
committed
fix(examples): improve sdk examples
* Check errors where we weren't checking them * Exit with status 1 on errors
1 parent cca36ab commit ddaaa78

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

examples/agent/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func main() {
1616
model, err := provider.LanguageModel("gpt-4o")
1717
if err != nil {
1818
fmt.Println(err)
19-
return
19+
os.Exit(1)
2020
}
2121

2222
// Create weather tool using the new type-safe API
@@ -38,9 +38,13 @@ func main() {
3838
ai.WithTools(weatherTool),
3939
)
4040

41-
result, _ := agent.Generate(context.Background(), ai.AgentCall{
41+
result, err := agent.Generate(context.Background(), ai.AgentCall{
4242
Prompt: "What's the weather in pristina",
4343
})
44+
if err != nil {
45+
fmt.Println(err)
46+
os.Exit(1)
47+
}
4448

4549
fmt.Println("Steps: ", len(result.Steps))
4650
for _, s := range result.Steps {

examples/simple/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func main() {
1414
model, err := provider.LanguageModel("claude-sonnet-4-20250514")
1515
if err != nil {
1616
fmt.Println(err)
17-
return
17+
os.Exit(1)
1818
}
1919

2020
response, err := model.Generate(context.Background(), ai.Call{
@@ -25,7 +25,7 @@ func main() {
2525
})
2626
if err != nil {
2727
fmt.Println(err)
28-
return
28+
os.Exit(1)
2929
}
3030

3131
fmt.Println("Assistant: ", response.Content.Text())

examples/stream/main.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func main() {
1515
model, err := provider.LanguageModel("gpt-4o")
1616
if err != nil {
1717
fmt.Println(err)
18-
return
18+
os.Exit(1)
1919
}
2020

2121
stream, err := model.Stream(context.Background(), ai.Call{
@@ -44,11 +44,15 @@ func main() {
4444
})
4545
if err != nil {
4646
fmt.Println(err)
47-
return
47+
os.Exit(1)
4848
}
4949

5050
for chunk := range stream {
51-
data, _ := json.Marshal(chunk)
51+
data, err := json.Marshal(chunk)
52+
if err != nil {
53+
fmt.Println(err)
54+
continue
55+
}
5256
fmt.Println(string(data))
5357
}
5458
}

examples/streaming-agent-simple/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func main() {
2424
model, err := provider.LanguageModel("gpt-4o-mini")
2525
if err != nil {
2626
fmt.Println(err)
27-
return
27+
os.Exit(1)
2828
}
2929

3030
// Create echo tool using the new type-safe API

0 commit comments

Comments
 (0)