Codú
Petr Homola2 min read

Structured LLM output in Go

A few days ago I decided to explore Google's new JSON Schema package for Go (GitHub, documentation) and use it in my LLM projects.

It is well designed and works seamlessly with the Gemini SDK but can be cumbersome at times so I started to write a simple wrapper, which allows you to write less code when using the library.

Using the LLM is very simple:

ctx := context.Background()

cl, err := ai.NewClient(ctx, ai.Gemini3FlashPreview)
if err != nil {
	log.Fatal(err)
}

resp, err := cl.GenerateText(ctx, ai.NewText("What is an LLM? Generate a short answer."))
if err != nil {
	log.Fatal(err)
}

fmt.Println(resp)

If you cloned the repo, you can run the example using:

go run ./examples/hello_world

However, for structured LLM output we need to define a JSON schema. It turns out then JSON schemas can be automatically inferred from Go types. So let's declare the following structure:

type Actor struct {
	Name        string
	YearOfBirth int
	Movies      []struct {
		Name string
		Year int
	}
}

Now we needn't care about JSON schemas, we can simply use this custom type as follows to make an LLM create an instance of it:

resp, err := ai.Generate[Actor](ctx, cl, ai.NewText("Provide information about Brad Pitt with a list of all his movies."))
if err != nil {
	log.Fatal(err)
}

Go doesn't support methods with type parameters yet so Generate is a standalone function.

Again, to run the example in the repo, use:

go run ./examples/structured_output

The above code makes the LLM generate an instance of our type with the information we've asked for.

LlmAiJsonGoJsonschema
Petr Homola@petr-homola-dub

Studied physics & CS; PhD in NLP; interested in AI, HPC & PLT

Loading

Loading discussion...

Hey! 👋

Got something to say?

or to leave a comment.