Skip to content

[Go] no error chain in errors defined in core package #3811

@janishorsts

Description

@janishorsts

Describe the bug

Handling the error of prompt output is fragile when it, for example, makes a call to a tool that fails.

The current implementation of core.GenkitError eliminates the error chain, converts the error chain to a string, and stores it in the Message field.

All core package-defined errors eliminate the error chain this way.

To Reproduce

package main

import (
	"errors"
	"testing"

	"github.com/firebase/genkit/go/ai"
	"github.com/firebase/genkit/go/genkit"
	"github.com/firebase/genkit/go/plugins/googlegenai"
)

var SomethingErr = errors.New("something went wrong")

func TestHandleError(t *testing.T) {
	g := genkit.Init(t.Context(),
		genkit.WithPlugins(new(googlegenai.GoogleAI)),
		genkit.WithDefaultModel("googleai/gemini-2.5-pro"),
	)

	getWeather := genkit.DefineTool(g, "getWeather", `Fetches the real-time weather for a city`,
		func(ctx *ai.ToolContext, location struct {
			City string `json:"city"`
		},
		) (string, error) {
			return "", SomethingErr
		},
	)

	prompt := genkit.DefinePrompt(g, "weather",
		ai.WithTools(getWeather),
		ai.WithInputType(In{}),
		ai.WithOutputType(Out{}),
		ai.WithPrompt("{{text}}"),
	)

	_, err := prompt.Execute(t.Context(), ai.WithInput(In{Text: "What is the weather in Riga?"}))

	// fails with:
	// want 'something went wrong', got 'tool "getWeather" failed: error calling tool getWeather: something went wrong'
	// err is core.GenkitError without the error chain
	if !errors.Is(err, SomethingErr) {
		t.Fatalf("want '%s', got '%s'", SomethingErr, err)
	}

}

Expected behavior

  • Retain the error chain.
  • Allow the genkit package consumers to handle errors in the Golang way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggo

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions