-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: Add MIMEType field to ChatMessageImageURL #1095
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
base: master
Are you sure you want to change the base?
Conversation
- allowing users to specify the MIME type of image URLs in multi-content messages.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1095 +/- ##
==========================================
+ Coverage 99.59% 99.61% +0.02%
==========================================
Files 34 34
Lines 2209 1841 -368
==========================================
- Hits 2200 1834 -366
+ Misses 6 4 -2
Partials 3 3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds support for specifying MIME types in image URLs for multi-content chat messages by introducing a new MIMEType field to the ChatMessageImageURL struct.
Changes:
- Added
MIMETypefield toChatMessageImageURLstruct with proper JSON tagmime_type - Added test case to verify the new field can be used in chat completion requests
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| chat.go | Added MIMEType string field to ChatMessageImageURL struct with json:"mime_type,omitempty" tag |
| chat_test.go | Added TestChatMessageImageURLMIMEType test function to verify usage of the new MIMEType field |
Comments suppressed due to low confidence (1)
chat_test.go:908
- The TestMultipartChatMessageSerialization test should be updated to include test cases for the new MIMEType field. Consider adding a test case that verifies JSON serialization and deserialization of ChatMessageImageURL with the MIMEType field set to ensure backward compatibility and proper JSON marshaling with the "mime_type" tag.
func TestMultipartChatMessageSerialization(t *testing.T) {
jsonText := `[{"role":"system","content":"system-message"},` +
`{"role":"user","content":[{"type":"text","text":"nice-text"},` +
`{"type":"image_url","image_url":{"url":"URL","detail":"high"}}]}]`
var msgs []openai.ChatCompletionMessage
err := json.Unmarshal([]byte(jsonText), &msgs)
if err != nil {
t.Fatalf("Expected no error: %s", err)
}
if len(msgs) != 2 {
t.Errorf("unexpected number of messages")
}
if msgs[0].Role != "system" || msgs[0].Content != "system-message" || msgs[0].MultiContent != nil {
t.Errorf("invalid user message: %v", msgs[0])
}
if msgs[1].Role != "user" || msgs[1].Content != "" || len(msgs[1].MultiContent) != 2 {
t.Errorf("invalid user message")
}
parts := msgs[1].MultiContent
if parts[0].Type != "text" || parts[0].Text != "nice-text" {
t.Errorf("invalid text part: %v", parts[0])
}
if parts[1].Type != "image_url" || parts[1].ImageURL.URL != "URL" || parts[1].ImageURL.Detail != "high" {
t.Errorf("invalid image_url part")
}
s, err := json.Marshal(msgs)
if err != nil {
t.Fatalf("Expected no error: %s", err)
}
res := strings.ReplaceAll(string(s), " ", "")
if res != jsonText {
t.Fatalf("invalid message: %s", string(s))
}
invalidMsg := []openai.ChatCompletionMessage{
{
Role: "user",
Content: "some-text",
MultiContent: []openai.ChatMessagePart{
{
Type: "text",
Text: "nice-text",
},
},
},
}
_, err = json.Marshal(invalidMsg)
if !errors.Is(err, openai.ErrContentFieldsMisused) {
t.Fatalf("Expected error: %s", err)
}
err = json.Unmarshal([]byte(`["not-a-message"]`), &msgs)
if err == nil {
t.Fatalf("Expected error")
}
emptyMultiContentMsg := openai.ChatCompletionMessage{
Role: "user",
MultiContent: []openai.ChatMessagePart{},
}
s, err = json.Marshal(emptyMultiContentMsg)
if err != nil {
t.Fatalf("Unexpected error")
}
res = strings.ReplaceAll(string(s), " ", "")
if res != `{"role":"user"}` {
t.Fatalf("invalid message: %s", string(s))
}
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Added MIMEType field to ChatMessageImageURL struct
allowing users to specify the MIME type of image URLs in multi-content messages.
relevant API doc
https://platform.openai.com/docs/api-reference/uploads/create