openai-go icon indicating copy to clipboard operation
openai-go copied to clipboard

SDK struct incorrectly trying to umarshal "type": "message" , "role" : "assistant" to EasyInputMessageParam instead of ResponseOutputMessageParam

Open harshit-gupta-glean opened this issue 2 months ago • 1 comments

Minimal failing test to reproduce :

func Test_Unmarshalling_Output_Text1(t *testing.T) {
	/*
			Initial request :

			{
		    "include": [
		        "reasoning.encrypted_content"
		    ],
		    "input": [

		        {
		            "role": "user",
		            "content": [
		                {
		                    "text": "hi there",
		                    "type": "input_text"
		                }
		            ],
		            "type": "message"
		        }
		    ],
		    "model": "gpt-5-2025-08-07",
		    "parallel_tool_calls": true,
		    "service_tier": "priority",
		    "store": false,
		    "reasoning": {
		        "effort": "minimal",
		        "summary": "auto"
		    },
		    "stream": true
		}
	*/

	/*

			response.completed event data :

			{
		    "type": "response.completed",
		    "sequence_number": 18,
		    "response": {
		        "id": "resp_68cb981bf9b88190a62a59ad84cd26d00ce9065a56366ed3",
		        "object": "response",
		        "created_at": 1758173212,
		        "status": "completed",
		        "background": false,
		        "error": null,
		        "incomplete_details": null,
		        "instructions": null,
		        "max_output_tokens": null,
		        "max_tool_calls": null,
		        "model": "gpt-5-2025-08-07",
		        "output": [
		            {
		                "id": "rs_68cb981c89c08190bcdb6a385e4805b10ce9065a56366ed3",
		                "type": "reasoning",
		                "encrypted_content": "gAAAAABoy5gcU0fXx-0obxCQLnknIM54qLdwX1qkg7nNBAFcyzcJ-3LJK5NjpGUWE26yhhqqYK9DjiwHyMPlgQ2_n7crUzarOFctjkrA3oKPT7SDL_9nfrjb3_1FMAK6YD4esCpTGu866J4mFHAKLwxNwB6N4tprcsl3BhAxH9spaGdAjdblYRHKAKZ5xC9QaogaUBNmLLqHMs2ld04nMv7E_YkfyqKdCekHJg22PpzFo9tGurqbFtdQkBR5ECJpFdRIHMeminYaNrfaMGeilICYk3ILDf0wRKsb9PVjthJLmTh427tLYTTKT9f2hKtJCKwubVBQfVp0QrY-KXSdzq_S6tru5EckGtOqd5fpAnhvjcbjicXqym1Rl_7bm7YSr9K1HOO6RUGcS3gYyCfcGJVhzfz9s040Z3cRn4wdlNZfG-qOUf_AHI7Xo-4le8tn8Y3DkyFWycgPG1FFLfoh_3mtUPyZ41R3e_2bf5mtneLBjwwPfbg6c4hoOE-I1oXCs-LKeogB7O9Has9kKPad64OKOVtLPeGIF4ihLA1BvbuDfBQy7m8d2Rf0-waCyLzSACSyB1i-83KfNsJhyKcVt7LmnLSTRIIAVWu75quPMJs-b9TBj5PbOvtMHNo_-edQYrJSCUp3aRRby255OzEx3HsuZULfVnWGGJqYmUP4_mpjcEFUCrt2aoUvXDvhPilwH-7kGUqP0dTVJkqqbCiv9t5FwRA08-cwGnFeeUESuTU-Mfp91_R5LbiK-zIubAvKEpVFlp9LHcllqpDzkJyi2Am89-fo19LcvVA0sUhVWuj9yflSY8VYaTh2l2weNmfHJql2MDOmRsqZ3awN-5gcP3Z6sa7OB05tzw4a0susxiinfpj81lskAS7Zmk7oJ0qCYOLYksUoNUw8",
		                "summary": []
		            },
		            {
		                "id": "msg_68cb981cbeec819081688c29f26510980ce9065a56366ed3",
		                "type": "message",
		                "status": "completed",
		                "content": [
		                    {
		                        "type": "output_text",
		                        "annotations": [],
		                        "logprobs": [],
		                        "text": "Hi! How can I help you today?"
		                    }
		                ],
		                "role": "assistant"
		            }
		        ],
		        "parallel_tool_calls": true,
		        "previous_response_id": null,
		        "prompt_cache_key": null,
		        "reasoning": {
		            "effort": "minimal",
		            "summary": "detailed"
		        },
		        "safety_identifier": null,
		        "service_tier": "priority",
		        "store": false,
		        "temperature": 1.0,
		        "text": {
		            "format": {
		                "type": "text"
		            },
		            "verbosity": "medium"
		        },
		        "tool_choice": "auto",
		        "tools": [],
		        "top_logprobs": 0,
		        "top_p": 1.0,
		        "truncation": "disabled",
		        "usage": {
		            "input_tokens": 8,
		            "input_tokens_details": {
		                "cached_tokens": 0
		            },
		            "output_tokens": 15,
		            "output_tokens_details": {
		                "reasoning_tokens": 0
		            },
		            "total_tokens": 23
		        },
		        "user": null,
		        "metadata": {}
		    }
		}
	*/

	// Request for second turn has input of 1st turn + response.output from the model response
	secondTurnRequest := `
{
    "include": [
        "reasoning.encrypted_content"
    ],
    "input": [
        {
            "role": "user",
            "content": [
                {
                    "text": "hi there",
                    "type": "input_text"
                }
            ],
            "type": "message"
        },
        {
            "id": "rs_68cb981c89c08190bcdb6a385e4805b10ce9065a56366ed3",
            "type": "reasoning",
            "encrypted_content": "gAAAAABoy5gcU0fXx-0obxCQLnknIM54qLdwX1qkg7nNBAFcyzcJ-3LJK5NjpGUWE26yhhqqYK9DjiwHyMPlgQ2_n7crUzarOFctjkrA3oKPT7SDL_9nfrjb3_1FMAK6YD4esCpTGu866J4mFHAKLwxNwB6N4tprcsl3BhAxH9spaGdAjdblYRHKAKZ5xC9QaogaUBNmLLqHMs2ld04nMv7E_YkfyqKdCekHJg22PpzFo9tGurqbFtdQkBR5ECJpFdRIHMeminYaNrfaMGeilICYk3ILDf0wRKsb9PVjthJLmTh427tLYTTKT9f2hKtJCKwubVBQfVp0QrY-KXSdzq_S6tru5EckGtOqd5fpAnhvjcbjicXqym1Rl_7bm7YSr9K1HOO6RUGcS3gYyCfcGJVhzfz9s040Z3cRn4wdlNZfG-qOUf_AHI7Xo-4le8tn8Y3DkyFWycgPG1FFLfoh_3mtUPyZ41R3e_2bf5mtneLBjwwPfbg6c4hoOE-I1oXCs-LKeogB7O9Has9kKPad64OKOVtLPeGIF4ihLA1BvbuDfBQy7m8d2Rf0-waCyLzSACSyB1i-83KfNsJhyKcVt7LmnLSTRIIAVWu75quPMJs-b9TBj5PbOvtMHNo_-edQYrJSCUp3aRRby255OzEx3HsuZULfVnWGGJqYmUP4_mpjcEFUCrt2aoUvXDvhPilwH-7kGUqP0dTVJkqqbCiv9t5FwRA08-cwGnFeeUESuTU-Mfp91_R5LbiK-zIubAvKEpVFlp9LHcllqpDzkJyi2Am89-fo19LcvVA0sUhVWuj9yflSY8VYaTh2l2weNmfHJql2MDOmRsqZ3awN-5gcP3Z6sa7OB05tzw4a0susxiinfpj81lskAS7Zmk7oJ0qCYOLYksUoNUw8",
            "summary": []
        },
        {
            "id": "msg_68cb981cbeec819081688c29f26510980ce9065a56366ed3",
            "type": "message",
            "status": "completed",
            "content": [
                {
                    "type": "output_text",
                    "annotations": [],
                    "logprobs": [],
                    "text": "Hi! How can I help you today?"
                }
            ],
            "role": "assistant"
        }
    ],
    "model": "gpt-5-2025-08-07",
    "parallel_tool_calls": true,
    "service_tier": "priority",
    "store": false,
    "reasoning": {
        "effort": "minimal"
    },
    "stream": true
}`

	req := &responses.ResponseNewParams{}
	err := req.UnmarshalJSON([]byte(secondTurnRequest))
	if err != nil {
		t.Error(err)
	}
	if len(req.Input.OfInputItemList) != 3 {
		t.Errorf("Expected 3 inputs, got %d", len(req.Input.OfInputItemList))
	}
	if req.Input.OfInputItemList[2].OfOutputMessage == nil {
		t.Errorf("Expected non-nil OfOutputMessage")
	}
	if req.Input.OfInputItemList[2].OfMessage != nil {
		t.Errorf("Expected nil OfMessage")
	}
}

Output

Failed
=== RUN   Test_Unmarshalling_Output_Text1
    qe_responses_handler_test.go:1375: Expected non-nil OfOutputMessage
    qe_responses_handler_test.go:1378: Expected nil OfMessage
--- FAIL: Test_Unmarshalling_Output_Text1 (0.00s)

SDK version : github.com/openai/openai-go/v2 v2.5.0

Impact

Assistant context is getting dropped in multi turn.

Temporary workaround

We are converting

{
            "id": "msg_68cb981cbeec819081688c29f26510980ce9065a56366ed3",
            "type": "message",
            "status": "completed",
            "content": [
                {
                    "type": "output_text",
                    "annotations": [],
                    "logprobs": [],
                    "text": "Hi! How can I help you today?"
                }
            ],
            "role": "assistant"
 }

to

{
            "id": "msg_68cb981cbeec819081688c29f26510980ce9065a56366ed3",
            "type": "message",
            "status": "completed",
            "content": "Hi! How can I help you today?",
            "role": "assistant"
        }

harshit-gupta-glean avatar Sep 18 '25 05:09 harshit-gupta-glean

most likely its due to the registration order of the Discriminator type = message

https://github.com/openai/openai-go/blob/881da8d3528ff0b1d5ccfaca43ee5f2dd4d6cb12/responses/response.go#L7003-L7005

harshit-gupta-glean avatar Sep 18 '25 09:09 harshit-gupta-glean