Skip to content

Support for raw JSON objects in system message content #383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
subham73 opened this issue Apr 24, 2025 · 3 comments
Open

Support for raw JSON objects in system message content #383

subham73 opened this issue Apr 24, 2025 · 3 comments

Comments

@subham73
Copy link

I'm using the OpenAI Go SDK with a custom endpoint that hosts a model. According to the model’s JSON schema, the system message must include a JSON object like this:{ "assistant_name": "Alfred Pennyworth"}
In the Python SDK, I can pass the system message as follows:
{"role": "system", "content": [{"assistant_name": "Alfred Pennyworth"}]}.
And this correctly satisfies the models requirement.

However, in the Go SDK, the current union type for message content only accepts plain strings or an array of text parts. Is there an existing workaround or a suggested solution to pass raw JSON objects in system message

@jacobzim-stl
Copy link
Collaborator

jacobzim-stl commented Apr 24, 2025

The current way to do this is to use param.OverrideObj[T](any) where T is the struct you wish to overwrite.

For example for the ChatCompletionMessageParamUnion

msg := param.OverrideObj[openai.ChatCompletionMessageParamUnion](anyValue),

anyValue can be any type compatible with json.Marshal().

Full disclosure: we're likely going to change the name of param.OverrideObj quite relatively soon before GA. We'd be grateful for any ideas.

@subham73
Copy link
Author

@jacobzim-stl Thanks !! Using param.OverrideObj worked for my JSON structure.

sysMsg := param.OverrideObj[openai.ChatCompletionSystemMessageParam](map[string]interface{}{
		"content": []map[string]interface{}{
			{"assistant_name": "Alfred Pennyworth"},
		},
		"role": "system",
	})

However, when using param.OverrideObj with ChatCompletionMessageParamUnion, the output appears as null when marshaled to JSON.
eg.

func main() {
	customData := CustomStruct{
		City: "paradeep",
	}
	systemMsg := param.OverrideObj[openai.ChatCompletionMessageParamUnion](customData)
	userMsg := openai.UserMessage("Hello")

	messages := []openai.ChatCompletionMessageParamUnion{
		systemMsg,
		userMsg,
	}

	jsonBytes, err := json.Marshal(messages)
	fmt.Println(string(jsonBytes))
	fmt.Println(err)
}

output:

[null,{"content":"Hello","role":"user"}]
<nil>

Moreover, using .WithExtraFields. is another way to achieve the same .
eg.

sysmsg := openai.SystemMessage(name)
sysmsg.OfSystem.WithExtraFields(
	map[string]any{
		"content": []any{
			map[string]any{"assistant_name": name},
		},
	},
)

@subham73
Copy link
Author

@jacobzim-stl regarding changing the name of param.OverrideObj, it works well for me or maybe CustomObj. But making it more discoverable would be a great option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants