Skip to content

Conversation

@kangkangkk
Copy link

  1. Added MIMEType field to ChatMessageImageURL struct

  2. allowing users to specify the MIME type of image URLs in multi-content messages.

  3. relevant API doc
    https://platform.openai.com/docs/api-reference/uploads/create

kangjunhao added 2 commits December 25, 2025 20:00
- allowing users to specify the MIME type of image URLs in multi-content messages.
@codecov
Copy link

codecov bot commented Dec 25, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.61%. Comparing base (5d7a276) to head (609949d).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

Copilot AI left a 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 MIMEType field to ChatMessageImageURL struct with proper JSON tag mime_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.

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

Successfully merging this pull request may close these issues.

1 participant