Skip to content

Commit df096ff

Browse files
committed
small fixes
1 parent c5dbb69 commit df096ff

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

examples/code_interpreter_demo.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
response = code_interpreter.run(
104104
code=code_to_run_script,
105105
language="python",
106-
files=[script_file], # Pass the script dictionary in a list
106+
files=[script_file], # Pass the script dictionary in a list
107107
)
108108

109109
# Print results
@@ -124,12 +124,13 @@
124124

125125
image_file = {
126126
"name": "tiny.png",
127-
"encoding": "base64", # Use base64 encoding for binary files
127+
"encoding": "base64", # Use base64 encoding for binary files
128128
"content": tiny_png_base64,
129129
}
130130

131131
# Code to check if the file exists and its size (Python doesn't inherently know image dimensions from bytes alone)
132-
code_to_check_file = """
132+
code_to_check_file = (
133+
"""
133134
import os
134135
import base64
135136
@@ -138,14 +139,17 @@
138139
# Read the raw bytes back
139140
with open(file_path, 'rb') as f:
140141
raw_bytes = f.read()
141-
original_bytes = base64.b64decode('""" + tiny_png_base64 + """')
142+
original_bytes = base64.b64decode('"""
143+
+ tiny_png_base64
144+
+ """')
142145
print(f"File '{file_path}' exists.")
143146
print(f"Size on disk: {os.path.getsize(file_path)} bytes.")
144147
print(f"Size of original decoded base64 data: {len(original_bytes)} bytes.")
145148
146149
else:
147150
print(f"File '{file_path}' does not exist.")
148151
"""
152+
)
149153

150154
response = code_interpreter.run(
151155
code=code_to_check_file,

src/together/resources/code_interpreter.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def run(
4141
client=self._client,
4242
)
4343

44-
data: Dict[str, str] = {
44+
data: Dict[str, Any] = {
4545
"code": code,
4646
"language": language,
4747
}
@@ -60,7 +60,9 @@ def run(
6060
except ValidationError as e:
6161
raise ValueError(f"Invalid file input format: {e}") from e
6262
except TypeError as e:
63-
raise ValueError(f"Invalid file input: Each item in 'files' must be a dictionary. Error: {e}") from e
63+
raise ValueError(
64+
f"Invalid file input: Each item in 'files' must be a dictionary. Error: {e}"
65+
) from e
6466

6567
data["files"] = serialized_files
6668

src/together/types/code_interpreter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from together.types.endpoints import TogetherJSONModel
88

9+
910
class FileInput(TogetherJSONModel):
1011
"""File input to be uploaded to the code interpreter session."""
1112

tests/unit/test_code_interpreter.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ def test_code_interpreter_run_with_files(mocker):
366366
response = interpreter.run(
367367
code='with open("test.txt") as f: print(f.read())',
368368
language="python",
369-
files=files_to_upload, # Pass the list of dictionaries directly
369+
files=files_to_upload, # Pass the list of dictionaries directly
370370
)
371371

372372
# Verify the response
@@ -394,13 +394,14 @@ def test_code_interpreter_run_with_files(mocker):
394394
"files": expected_files_payload,
395395
}
396396

397+
397398
def test_code_interpreter_run_with_invalid_file_dict_structure(mocker):
398399
"""Test that run raises ValueError for missing keys in file dict."""
399400
client = mocker.MagicMock()
400401
interpreter = CodeInterpreter(client)
401402

402403
invalid_files = [
403-
{"name": "test.txt", "content": "Missing encoding"} # Missing 'encoding'
404+
{"name": "test.txt", "content": "Missing encoding"} # Missing 'encoding'
404405
]
405406

406407
with pytest.raises(ValueError, match="Invalid file input format"):
@@ -410,13 +411,18 @@ def test_code_interpreter_run_with_invalid_file_dict_structure(mocker):
410411
files=invalid_files,
411412
)
412413

414+
413415
def test_code_interpreter_run_with_invalid_file_dict_encoding(mocker):
414416
"""Test that run raises ValueError for invalid encoding value."""
415417
client = mocker.MagicMock()
416418
interpreter = CodeInterpreter(client)
417419

418420
invalid_files = [
419-
{"name": "test.txt", "encoding": "utf-8", "content": "Invalid encoding"} # Invalid 'encoding' value
421+
{
422+
"name": "test.txt",
423+
"encoding": "utf-8",
424+
"content": "Invalid encoding",
425+
} # Invalid 'encoding' value
420426
]
421427

422428
with pytest.raises(ValueError, match="Invalid file input format"):
@@ -426,17 +432,21 @@ def test_code_interpreter_run_with_invalid_file_dict_encoding(mocker):
426432
files=invalid_files,
427433
)
428434

435+
429436
def test_code_interpreter_run_with_invalid_file_list_item(mocker):
430437
"""Test that run raises ValueError for non-dict item in files list."""
431438
client = mocker.MagicMock()
432439
interpreter = CodeInterpreter(client)
433440

434441
invalid_files = [
435442
{"name": "good.txt", "encoding": "string", "content": "Good"},
436-
"not a dictionary" # Invalid item type
443+
"not a dictionary", # Invalid item type
437444
]
438445

439-
with pytest.raises(ValueError, match="Invalid file input: Each item in 'files' must be a dictionary"):
446+
with pytest.raises(
447+
ValueError,
448+
match="Invalid file input: Each item in 'files' must be a dictionary",
449+
):
440450
interpreter.run(
441451
code="print('test')",
442452
language="python",

0 commit comments

Comments
 (0)