Skip to content
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

[BREAKING_CHANGES] fix: support ThreadMessage.Content as an array #821

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,21 @@ func TestRun(t *testing.T) {
_, err = client.CancelRun(ctx, threadID, runID)
checks.NoError(t, err, "CancelRun error")

prompt := "Hello, World!"
_, err = client.CreateThreadAndRun(ctx, openai.CreateThreadAndRunRequest{
RunRequest: openai.RunRequest{
AssistantID: assistantID,
},
Thread: openai.ThreadRequest{
Messages: []openai.ThreadMessage{
{
Role: openai.ThreadMessageRoleUser,
Content: "Hello, World!",
Role: openai.ThreadMessageRoleUser,
Content: []openai.ThreadMessageContent{
{
Type: "text",
Text: prompt,
},
},
},
},
},
Expand Down
20 changes: 15 additions & 5 deletions thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,21 @@ const (
)

type ThreadMessage struct {
Role ThreadMessageRole `json:"role"`
Content string `json:"content"`
FileIDs []string `json:"file_ids,omitempty"`
Attachments []ThreadAttachment `json:"attachments,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
Role ThreadMessageRole `json:"role"`
Content []ThreadMessageContent `json:"content"`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thank you for the PR!

The documentation says it is either a string or array: https://platform.openai.com/docs/api-reference/messages/createMessage#messages-createmessage-content

So I think we need a new type here that supports proper marshalling

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we send an array of one with text: { ... } instead of a new type?

Attachments []ThreadAttachment `json:"attachments,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
}

type ThreadMessageContent struct {
Type string `json:"type"`
ImageFile *ThreadMessageImageFile `json:"image_file,omitempty"`
Text string `json:"text,omitempty"`
}

type ThreadMessageImageFile struct {
FileID string `json:"file_id"`
Detail *string `json:"detail,omitempty"`
}

type ThreadAttachment struct {
Expand Down
20 changes: 16 additions & 4 deletions thread_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,17 @@ func TestThread(t *testing.T) {

ctx := context.Background()

prompt := "Hello, World!"
_, err := client.CreateThread(ctx, openai.ThreadRequest{
Messages: []openai.ThreadMessage{
{
Role: openai.ThreadMessageRoleUser,
Content: "Hello, World!",
Role: openai.ThreadMessageRoleUser,
Content: []openai.ThreadMessageContent{
{
Type: "text",
Text: prompt,
},
},
},
},
})
Expand Down Expand Up @@ -153,11 +159,17 @@ func TestAzureThread(t *testing.T) {

ctx := context.Background()

prompt := "Hello, World!"
_, err := client.CreateThread(ctx, openai.ThreadRequest{
Messages: []openai.ThreadMessage{
{
Role: openai.ThreadMessageRoleUser,
Content: "Hello, World!",
Role: openai.ThreadMessageRoleUser,
Content: []openai.ThreadMessageContent{
{
Type: "text",
Text: prompt,
},
},
},
},
})
Expand Down
Loading