Skip to content

[Go] ai.LookupModel returns (*ai.modelActionDef)(nil) when no model is found, which is not the same as nil #847

@yukinagae

Description

@yukinagae

Describe the bug

The below code does not return nil when no model is found.
Instead of returning nil, it returns (*ai.modelActionDef)(nil) and (*ai.modelActionDef)(nil) == nil always be false.

https://github.com/firebase/genkit/blob/main/go/ai/generate.go#L92-L96

// LookupModel looks up a [Model] registered by [DefineModel].
// It returns nil if the model was not defined.
func LookupModel(provider, name string) Model {
	return (*modelActionDef)(core.LookupActionFor[*GenerateRequest, *GenerateResponse, *GenerateResponseChunk](atype.Model, provider, name))
}

To Reproduce

If I run the sample code below:

	if err := googleai.Init(ctx, nil); err != nil {
		log.Fatal(err)
	}

	genkit.DefineFlow("menuSuggestionFlow", func(ctx context.Context, input string) (string, error) {
		m := googleai.Model("foo")
		if m == nil {
			log.Fatal(" Model not found")
		}
		log.Printf("model: %#v", m)
		log.Printf("model == nil: %#v", m == nil)

It produces the below log where nil check does not work as expected.

time=2024-09-01T11:00:23.509+09:00 level=INFO msg="model: (*ai.modelActionDef)(nil)"
time=2024-09-01T11:00:23.509+09:00 level=INFO msg="model == nil: false"

Expected behavior

ai.LookupModel(provider, name) returns nil when no model is found, as the below ai.LookupEmbedder(provider, name) does.

https://github.com/firebase/genkit/blob/main/go/ai/embedder.go#L68-L76

// LookupEmbedder looks up an [Embedder] registered by [DefineEmbedder].
// It returns nil if the embedder was not defined.
func LookupEmbedder(provider, name string) Embedder {
	action := core.LookupActionFor[*EmbedRequest, *EmbedResponse, struct{}](atype.Embedder, provider, name)
	if action == nil {
		return nil
	}
	return (*embedderActionDef)(action)
}

Runtime (please complete the following information):

  • OS: MacOS
  • Version: 12.7.6

Go version

  • Go: 1.23.0
$ go version
go version go1.23.0 darwin/arm64

Additional context

This blog post Check For Nil Interface In Go explains the tricky aspects of checking for nil interfaces in detail.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggo

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions