Skip to content

Support unmarshaling request structs #247

@mattmueller-stripe

Description

@mattmueller-stripe

Hey OpenAI team!

We're working on an OpenAI API-compatible proxy. Roughly:

user -> proxy -> openai api

To ease the maintainance burden, we'd like to import the request and response structs from this library into our proxy, specifically structs like:

type ChatCompletionNewParams struct {
	// A list of messages comprising the conversation so far. Depending on the
	// [model](https://platform.openai.com/docs/models) you use, different message
	// types (modalities) are supported, like
	// [text](https://platform.openai.com/docs/guides/text-generation),
	// [images](https://platform.openai.com/docs/guides/vision), and
	// [audio](https://platform.openai.com/docs/guides/audio).
	Messages param.Field[[]ChatCompletionMessageParamUnion] `json:"messages,required"`
	// ID of the model to use. See the
	// [model endpoint compatibility](https://platform.openai.com/docs/models#model-endpoint-compatibility)
	// table for details on which models work with the Chat API.
	Model param.Field[ChatModel] `json:"model,required"`
	// ...
}

(code).

This is possible today, however when we try to unmarshal the request into those structs, we get the following error:

json: cannot unmarshal string into Go struct field ChatCompletionNewParams.model of type param.Field[string]

I believe this is because param.Field doesn't implement a json.Unmarshaler.

Would it be possible to support unmarshaling on all the request structs:

var chatRequest openai.ChatCompletionNewParams
_ := json.Unmarshal(body, &chatRequest)

We'd also need marshaling on all the response structs, but I believe that's supported already!

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