Skip to content

Commit 6ea26eb

Browse files
jxnlclaude
andauthored
fix: Python 3.13 compatibility and import path corrections
Co-authored-by: Claude <noreply@anthropic.com>
1 parent af2bc73 commit 6ea26eb

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

instructor/processing/multimodal.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,19 @@ def from_gs_url(cls, data_uri: str, timeout: int = 30) -> Image:
151151
def from_raw_base64(cls, data: str) -> Image:
152152
try:
153153
decoded = base64.b64decode(data)
154-
import imghdr
155154

156-
img_type = imghdr.what(None, decoded)
155+
# Detect image type from file signature (magic bytes)
156+
# This replaces imghdr which was removed in Python 3.13
157+
img_type = None
158+
if decoded.startswith(b'\xff\xd8\xff'):
159+
img_type = 'jpeg'
160+
elif decoded.startswith(b'\x89PNG\r\n\x1a\n'):
161+
img_type = 'png'
162+
elif decoded.startswith(b'GIF87a') or decoded.startswith(b'GIF89a'):
163+
img_type = 'gif'
164+
elif decoded.startswith(b'RIFF') and decoded[8:12] == b'WEBP':
165+
img_type = 'webp'
166+
157167
if img_type:
158168
media_type = f"image/{img_type}"
159169
if media_type in VALID_MIME_TYPES:

instructor/providers/gemini/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ def handle_vertexai_tools(
961961
def handle_vertexai_json(
962962
response_model: type[Any] | None, new_kwargs: dict[str, Any]
963963
) -> tuple[type[Any] | None, dict[str, Any]]:
964-
from instructor.client_vertexai import vertexai_process_json_response
964+
from instructor.providers.vertexai.client import vertexai_process_json_response
965965

966966
"""
967967
Handle Vertex AI JSON mode.

tests/test_response_model_conversion.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414

1515
def get_system_prompt(user_tool_definition, mode):
1616
if mode == instructor.Mode.ANTHROPIC_JSON:
17-
return user_tool_definition["system"]
17+
system = user_tool_definition["system"]
18+
# Handle both string and list[dict] formats
19+
if isinstance(system, list):
20+
return "".join(block.get("text", "") for block in system)
21+
return system
1822
elif mode == instructor.Mode.GEMINI_JSON:
1923
return "\n".join(user_tool_definition["contents"][0]["parts"])
2024
elif mode == instructor.Mode.VERTEXAI_JSON:

0 commit comments

Comments
 (0)