Skip to content

Union Type Deserialization Issue: OfOutputMessage incorrectly deserialized as OfMessage #483

Description

@kaaanata

Union Type Deserialization Issue with ResponseInputItemUnionParam

Problem Description

When storing and retrieving ResponseInputItemUnionParam through JSON marshaling/unmarshaling, the OfOutputMessage field gets incorrectly deserialized as OfMessage, causing data type confusion and loss of type-specific fields.

Root Cause

The union type registration has conflicting discriminator values:

// In responses/response.go
apijson.RegisterUnion[ResponseInputItemUnionParam](
    "type",
    apijson.Discriminator[EasyInputMessageParam]("message"),           // OfMessage
    apijson.Discriminator[ResponseInputItemMessageParam]("message"),   // OfInputMessage  
    apijson.Discriminator[ResponseOutputMessageParam]("message"),      // OfOutputMessage
    // ... other types
)

Three different types all use "message" as their discriminator value. When deserializing, the decoder selects the first matching type (EasyInputMessageParam), causing ResponseOutputMessageParam to be incorrectly identified.

Impact

  • ResponseOutputMessageParam loses its unique fields (id, status) after JSON round-trip
  • Type information is lost, making it impossible to distinguish between different message types
  • Applications that persist conversation history cannot reliably restore the correct types

Reproduction

package main

import (
    "encoding/json"
    "fmt"
    "log"
    
    "github.com/openai/openai-go/v2/packages/param"
    "github.com/openai/openai-go/v2/responses"
)

func main() {
    // Create a ResponseInputItemUnionParam with OfOutputMessage
    original := responses.ResponseInputItemUnionParam{
        OfOutputMessage: &responses.ResponseOutputMessageParam{
            ID: "msg_123",
            Content: []responses.ResponseOutputMessageContentUnionParam{
                {
                    OfOutputText: &responses.ResponseOutputTextParam{
                        Text: "Hello from assistant",
                        Type: "output_text",
                    },
                },
            },
            Status: "completed",
            Role:   "assistant",
            Type:   "message",
        },
    }
    
    // Marshal to JSON
    jsonData, err := json.Marshal(original)
    if err != nil {
        log.Fatal(err)
    }
    
    fmt.Printf("JSON: %s\n", string(jsonData))
    
    // Unmarshal from JSON
    var restored responses.ResponseInputItemUnionParam
    err = json.Unmarshal(jsonData, &restored)
    if err != nil {
        log.Fatal(err)
    }
    
    // Check which field is set
    hasOfOutputMessage := !param.IsOmitted(restored.OfOutputMessage)
    hasOfMessage := !param.IsOmitted(restored.OfMessage)
    
    fmt.Printf("After unmarshal:\n")
    fmt.Printf("  OfOutputMessage is set: %v\n", hasOfOutputMessage)
    fmt.Printf("  OfMessage is set: %v\n", hasOfMessage)
    
    // Expected: OfOutputMessage should be true, OfMessage should be false
    // Actual: OfOutputMessage is false, OfMessage is true
    
    if hasOfMessage && restored.OfMessage != nil {
        fmt.Printf("  ERROR: Type was changed from OfOutputMessage to OfMessage!\n")
        fmt.Printf("  Lost fields: id=%s, status=%s\n", 
            original.OfOutputMessage.ID, 
            original.OfOutputMessage.Status)
    }
}

Expected Output

JSON: {"id":"msg_123","content":[{"text":"Hello from assistant","type":"output_text"}],"status":"completed","role":"assistant","type":"message"}
After unmarshal:
  OfOutputMessage is set: true
  OfMessage is set: false

Actual Output

JSON: {"id":"msg_123","content":[{"text":"Hello from assistant","type":"output_text"}],"status":"completed","role":"assistant","type":"message"}
After unmarshal:
  OfOutputMessage is set: false
  OfMessage is set: true
  ERROR: Type was changed from OfOutputMessage to OfMessage!
  Lost fields: id=msg_123, status=completed

Environment

  • openai-go version: v2.0.2
  • Go version: 1.22+

Suggested Solutions

  1. Use unique discriminator values: Instead of all using "message", use distinct values like "easy_message", "input_message", "output_message"

  2. Add secondary discriminators: Use additional fields (like presence of id and status) to distinguish between types

  3. Improve union decoder logic: Enhance the decoder to score all matching types and select the best match based on field patterns

Workaround

Currently, developers need to implement custom serialization logic to preserve type information, which defeats the purpose of having a union type in the SDK.

SDK Version

Activity

  1. added
    Generator
    on Jun 24, 2026
  2. rsd-darshan commented on Jun 26, 2026

    @rsd-darshan

    Opened a fix for this: #692

    The root cause is in newStructUnionDecoder (internal/apijson/union.go) — when multiple variants are registered under the same discriminator value, it always picked whichever one was registered first instead of the one matching the actual payload. ResponseInputItemUnionParam registers EasyInputMessageParam, ResponseInputItemMessageParam, and ResponseOutputMessageParam all under "type":"message", so an output message echoed back into input always decoded as the wrong variant.

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

    Generator

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions