Skip to content

FunctionCall.Args loses empty map on JSON round trip #920

Open

Description

@alexdarbyshire

Serializing a FunctionCall with an allocated empty Args map omits the args field. Unmarshaling the result restores Args as nil instead of an empty map.

This affects consumers that persist and reload conversation history containing tool calls.

An explicitly supplied argument object changes representation across the serialization boundary, requiring downstream normalization. The behavior is reproducible usingencoding/json.

Environment details

  • Programming language: Go
  • OS: macOS
  • Language runtime version: Go 1.27.1
  • Package version: google.golang.org/genai v1.71.0

Steps to reproduce

  1. Construct a genai.FunctionCall with Args: map[string]any{}.
  2. Marshal it to JSON, then unmarshal into a new genai.FunctionCall.
  3. Observe that the restored Args is nil.
package main

import (
	"encoding/json"
	"fmt"

	"google.golang.org/genai"
)

func main() {
	before := genai.FunctionCall{
		Name: "list_tools",
		Args: map[string]any{},
	}

	data, err := json.Marshal(before)
	if err != nil {
		panic(err)
	}

	var after genai.FunctionCall
	if err := json.Unmarshal(data, &after); err != nil {
		panic(err)
	}

	fmt.Printf("before Args is nil: %t\n", before.Args == nil)
	fmt.Printf("JSON: %s\n", data)
	fmt.Printf("after Args is nil: %t\n", after.Args == nil)
}

Actual output:

before Args is nil: false
JSON: {"name":"list_tools"}
after Args is nil: true

Expected JSON:

{"args":{},"name":"list_tools"}

The restored Args should remain a non-nil empty map. Originally nil arguments should continue to be omitted.

Possible fix

The field currently uses:

Args map[string]any `json:"args,omitempty"`

Updating to json:"args,omitzero" (supported by required Go 1.24) would preserve explicit empty objects.

types.go is generated, so this change would need to be made in the generator that produces FunctionCall.Args. The tag change above describes the intended generated output.

I’m happy to provide a regression test or demonstration PR if useful.

Net Result

Fixing this at the SDK serialization boundary would avoid each downstream consumer needing its own workaround. Regression coverage could check nil, empty, and populated arguments, both directly and nested inside Content.

Related

Precedent: #198 fixed dropped explicit zero values during conversion, and #221 added JSON round-trip tests.

Activity

  1. added
    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
    priority: p2Moderately-important priority. Fix may not be included in next release.
    on Sep 16, 2026
  2. Sivasankaran25 commented on Sep 17, 2026

    @Sivasankaran25
    Collaborator

    Hi @alexdarbyshire,

    Thank you for opening this issue! We have reproduced the issue on our end and will be filing a bug internally.

  3. added a commit that references this issue on Oct 27, 2026
    c1b996f
  4. added 3 commits that reference this issue on Nov 5, 2026
    5db62de
    46aa523
    c7d7ceb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions