From 12beb3e8d531c823ac4fbc7353e0ea81c0debe0a Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Wed, 27 Aug 2025 14:58:44 -0300 Subject: [PATCH] fix(examples): improve sdk examples * Check errors where we weren't checking them * Exit with status 1 on errors --- internal/ai/examples/agent/main.go | 8 ++++++-- internal/ai/examples/simple/main.go | 4 ++-- internal/ai/examples/stream/main.go | 10 +++++++--- internal/ai/examples/streaming-agent-simple/main.go | 2 +- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/internal/ai/examples/agent/main.go b/internal/ai/examples/agent/main.go index 5afff20a96181e9bb6d2d928531bb99d8f77427e..2ba08dbe657817352b8a8b89364fae950b9eb07f 100644 --- a/internal/ai/examples/agent/main.go +++ b/internal/ai/examples/agent/main.go @@ -16,7 +16,7 @@ func main() { model, err := provider.LanguageModel("gpt-4o") if err != nil { fmt.Println(err) - return + os.Exit(1) } // Create weather tool using the new type-safe API @@ -38,9 +38,13 @@ func main() { ai.WithTools(weatherTool), ) - result, _ := agent.Generate(context.Background(), ai.AgentCall{ + result, err := agent.Generate(context.Background(), ai.AgentCall{ Prompt: "What's the weather in pristina", }) + if err != nil { + fmt.Println(err) + os.Exit(1) + } fmt.Println("Steps: ", len(result.Steps)) for _, s := range result.Steps { diff --git a/internal/ai/examples/simple/main.go b/internal/ai/examples/simple/main.go index a8136ee7e1870b54430cf8a5c8dca305c9ade688..a3521c97149685951bff627a9ce484fbe210d226 100644 --- a/internal/ai/examples/simple/main.go +++ b/internal/ai/examples/simple/main.go @@ -14,7 +14,7 @@ func main() { model, err := provider.LanguageModel("claude-sonnet-4-20250514") if err != nil { fmt.Println(err) - return + os.Exit(1) } response, err := model.Generate(context.Background(), ai.Call{ @@ -25,7 +25,7 @@ func main() { }) if err != nil { fmt.Println(err) - return + os.Exit(1) } fmt.Println("Assistant: ", response.Content.Text()) diff --git a/internal/ai/examples/stream/main.go b/internal/ai/examples/stream/main.go index b422976b045c2eaa5adb710bb3d644e2dbf2f155..c04848602f8390a7ba2f05610adb473a2b161ef5 100644 --- a/internal/ai/examples/stream/main.go +++ b/internal/ai/examples/stream/main.go @@ -15,7 +15,7 @@ func main() { model, err := provider.LanguageModel("gpt-4o") if err != nil { fmt.Println(err) - return + os.Exit(1) } stream, err := model.Stream(context.Background(), ai.Call{ @@ -44,11 +44,15 @@ func main() { }) if err != nil { fmt.Println(err) - return + os.Exit(1) } for chunk := range stream { - data, _ := json.Marshal(chunk) + data, err := json.Marshal(chunk) + if err != nil { + fmt.Println(err) + continue + } fmt.Println(string(data)) } } diff --git a/internal/ai/examples/streaming-agent-simple/main.go b/internal/ai/examples/streaming-agent-simple/main.go index 1333c76de3862e4c58b149d0b9227fc877b9a754..4abe25f83c321a35e920240988e816616757f7cc 100644 --- a/internal/ai/examples/streaming-agent-simple/main.go +++ b/internal/ai/examples/streaming-agent-simple/main.go @@ -24,7 +24,7 @@ func main() { model, err := provider.LanguageModel("gpt-4o-mini") if err != nil { fmt.Println(err) - return + os.Exit(1) } // Create echo tool using the new type-safe API