Skip to content

Implicit ignore of omitempty struct tags #539

Description

@l0nedigit

Just a thought, but it may be worthwhile to add a note on structured outputs (docs & codebase) in that if your struct tags have omitempty that the openai-go api will silently ignore them and the model will not populate that field.

Chased my tail on this one for a while. Is there a way to specify to not ignore omitempty tags?

Activity

  1. added
    Docs
    and removed on Jun 24, 2026
  2. jnohclee-rgb commented on Oct 6, 2026

    @jnohclee-rgb

    The current structured-output example now contains an omitempty warning. I checked its actual GenerateSchema helper offline with the examples module's pinned invopop/jsonschema v0.14.0 (Go 1.27.1 darwin/arm64):

    Field tags property exists in schema in required array
    json:"label" yes yes
    json:"label,omitempty" yes no
    json:"label,omitzero" yes no
    json:"label,omitempty" jsonschema:"required" yes yes

    All four assertions passed. In this schema-generation path, omitempty changes requiredness; it does not remove the property from the schema. An explicit jsonschema:"required" overrides that inferred optionality with this pinned reflector version, so it is an option when the same Go type also needs omitempty for ordinary JSON marshaling. Removing omitempty is the simpler option if that dual use is unnecessary.

    I also checked json.Marshal of a zero-valued struct with both omitempty and jsonschema:required: it still emits an empty object. The schema tag affects reflection, not encoding/json's serialization rules. Keep those two behaviors distinct when using a type for both request marshaling and schema generation.

    # from examples module
    go test -mod=readonly -run TestDharmaRequiredTags -v -count=1 -timeout=30s ./structured-outputs
    

    Only the helper was exercised; example main was not run, network was denied and no model/API request occurred. This confirms schema properties/requiredness for the pinned dependency, not server acceptance, field generation by a model, all nested/tag combinations, or other reflector versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Docs

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions