-
Notifications
You must be signed in to change notification settings - Fork 495
Support proper conversion from MCP's image content to OpenAI's image content part #210
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: main
Are you sure you want to change the base?
Conversation
) | ||
if content.resource.mimeType.startswith("image/"): | ||
return ChatCompletionContentPartImageParam( | ||
type="image_url", |
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.
Is it always base64 based btw, or can it be a URI as well?
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.
Yup, MCP's ImageContent only provides base64 encoded data.
…_content_to_mcp_content
) | ||
) | ||
else: | ||
raise ValueError(f"Unexpected content type: {c.type}") | ||
raise ValueError(f"Unexpected content type: {c["type"]}") |
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.
There's a syntax error in this f-string where double quotes are nested inside double quotes when accessing the dictionary key. This will cause a Python syntax error at runtime.
Consider using single quotes inside the f-string:
raise ValueError(f"Unexpected content type: {c['type']}")
Or alternatively, use single quotes for the f-string itself:
raise ValueError(f'Unexpected content type: {c["type"]}')
raise ValueError(f"Unexpected content type: {c["type"]}") | |
raise ValueError(f"Unexpected content type: {c['type']}") |
Spotted by Diamond
Is this helpful? React 👍 or 👎 to let us know.
""" | ||
import re | ||
|
||
match = re.match(r"data:(image/\w+);base64,(.*)", image_url) |
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.
The regex pattern r"data:(image/\w+);base64,(.*)"
is too restrictive for image MIME types. The \w+
only matches alphanumeric characters and underscores, but valid MIME types like image/svg+xml
contain additional characters such as +
.
Consider updating to a more inclusive pattern like:
r"data:(image/[\w.+-]+);base64,(.*)"
This will properly handle all standard image MIME types including SVG, PNG, JPEG, and others that may contain special characters in their subtype.
match = re.match(r"data:(image/\w+);base64,(.*)", image_url) | |
match = re.match(r"data:(image/[\w.+-]+);base64,(.*)", image_url) |
Spotted by Diamond
Is this helpful? React 👍 or 👎 to let us know.
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.
Approving to unblock @StreetLamb. Please update with conflicts resolved and merge at your convenience!
Fixes #201