Skip to content

ResponsesRequest fails to marshal when ServiceTier is omitted #301

Description

@ell-hol

Summary

components.ResponsesRequest fails to marshal when ServiceTier is omitted.

This happens before any HTTP request is sent, during local JSON serialization.

I opened a PR with a minimal reproduction and proposed a fix #300

I saw the contributing guide says this repository contains generated code and does not accept direct code changes, so I am opening this issue as the official report and linking the PR for context.

Reproduction

This code fails when ServiceTier is not explicitly set:

package main

import (
	"context"
	"fmt"
	"log"
	"os"

	openrouter "github.com/OpenRouterTeam/go-sdk"
	"github.com/OpenRouterTeam/go-sdk/models/components"
)

func main() {
	ctx := context.Background()

	s := openrouter.New(
		openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
	)

	res, err := s.Beta.Responses.Send(ctx, components.ResponsesRequest{
		Input: openrouter.Pointer(
			components.CreateInputsUnionStr("Hey there"),
		),
		Model:  openrouter.Pointer("openai/gpt-4o-mini"),
		Stream: openrouter.Bool(true),
	}, nil)

	if err != nil {
		log.Printf("error type: %T", err)
		log.Printf("error value: %+v", err)
		log.Fatal(err)
	}

	if res != nil {
		defer res.EventStream.Close()

		for res.EventStream.Next() {
			event := res.EventStream.Value()
			delta := event.Data.TextDeltaEvent.GetDelta()
			fmt.Print(delta)
		}
	}
}

Actual behavior

The request fails before reaching the API:

error serializing request body: json: error calling MarshalJSON for type components.ResponsesRequest:
json: error calling MarshalJSON for type json.RawMessage:
invalid character 'a' looking for beginning of value

The same issue also happens with Stream: openrouter.Bool(false), so the problem is not streaming-specific.

A minimal local reproduction is:

req := components.ResponsesRequest{
	Input: openrouter.Pointer(
		components.CreateInputsUnionStr("Hey there"),
	),
	Model:  openrouter.Pointer("openai/gpt-4o-mini"),
	Stream: openrouter.Bool(false),
}

_, err := json.Marshal(req)

This fails with the same serialization error.

Expected behavior

ResponsesRequest should marshal successfully when ServiceTier is omitted.

Since ResponsesRequest.ServiceTier has:

ServiceTier optionalnullable.OptionalNullable[ResponsesRequestServiceTier] `default:"auto" json:"service_tier"`

the generated JSON should either omit service_tier or serialize the default as a valid JSON string:

"service_tier": "auto"

Root cause

The default value auto appears to be emitted as raw JSON:

auto

That is invalid JSON. It should be emitted as:

"auto"

This explains why explicitly setting ServiceTier works:

serviceTier := components.ResponsesRequestServiceTierAuto

ServiceTier: optionalnullable.From(&serviceTier)

In that case, the enum value goes through the normal marshaling path and is serialized correctly.

Notes

The linked PR adds a regression test showing the failure and a small patch that prevents non-JSON default tag values from being emitted as raw JSON.

Full test suite passed locally with:

go test ./...

Environment:

github.com/OpenRouterTeam/go-sdk v0.5.0
Go: go1.26.3
OS: macOS amd64

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions