Repository navigation
Description
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
ResponseOutputMessageParamloses 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
-
Use unique discriminator values: Instead of all using
"message", use distinct values like"easy_message","input_message","output_message" -
Add secondary discriminators: Use additional fields (like presence of
idandstatus) to distinguish between types -
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
- openai-go/v2: v2.0.2
Activity
fix: OfOutputMessage discriminator parsing
fix: OfOutputMessage discriminator parsing
fix: OfOutputMessage discriminator parsing
fix: OfOutputMessage discriminator parsing
rsd-darshan commentedon Jun 26, 2026
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.ResponseInputItemUnionParamregistersEasyInputMessageParam,ResponseInputItemMessageParam, andResponseOutputMessageParamall under"type":"message", so an output message echoed back into input always decoded as the wrong variant.