Skip to content

Commit 67f6e5c

Browse files
committed
fix: stop crash when parsing empty files and strings
Signed-off-by: Donnie Adams <[email protected]>
1 parent ce89f63 commit 67f6e5c

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

gptscript/gptscript.py

+4
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,16 @@ def run(
9797
async def parse(self, file_path: str, disable_cache: bool = False) -> list[Text | Tool]:
9898
out = await self._run_basic_command("parse", {"file": file_path, "disableCache": disable_cache})
9999
parsed_nodes = json.loads(out)
100+
if parsed_nodes is None or parsed_nodes.get("nodes", None) is None:
101+
return []
100102
return [Text(**node["textNode"]) if "textNode" in node else Tool(**node.get("toolNode", {}).get("tool", {})) for
101103
node in parsed_nodes.get("nodes", [])]
102104

103105
async def parse_tool(self, tool_def: str) -> list[Text | Tool]:
104106
out = await self._run_basic_command("parse", {"content": tool_def})
105107
parsed_nodes = json.loads(out)
108+
if parsed_nodes is None or parsed_nodes.get("nodes", None) is None:
109+
return []
106110
return [Text(**node["textNode"]) if "textNode" in node else Tool(**node.get("toolNode", {}).get("tool", {})) for
107111
node in parsed_nodes.get("nodes", [])]
108112

tests/fixtures/empty.gpt

Whitespace-only changes.

tests/test_gptscript.py

+13
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,19 @@ async def test_parse_simple_file(gptscript):
259259
"Unexpected output from parsing simple file"
260260

261261

262+
@pytest.mark.asyncio
263+
async def test_parse_empty_file(gptscript):
264+
wd = os.getcwd()
265+
tools = await gptscript.parse(wd + "/tests//fixtures/empty.gpt")
266+
assert len(tools) == 0, "Unexpected number of tools for parsing emtpy file"
267+
268+
269+
@pytest.mark.asyncio
270+
async def test_parse_empty_str(gptscript):
271+
tools = await gptscript.parse_tool("")
272+
assert len(tools) == 0, "Unexpected number of tools for parsing empty string"
273+
274+
262275
@pytest.mark.asyncio
263276
async def test_parse_tool_with_metadata(gptscript):
264277
wd = os.getcwd()

0 commit comments

Comments
 (0)