Skip to content

Commit

Permalink
Merge pull request #717 from jimkring/patch-3
Browse files Browse the repository at this point in the history
handle empty tool call function arguments
  • Loading branch information
willbakst authored Nov 26, 2024
2 parents 64c7ea5 + 72b2d95 commit 4365d60
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 36 deletions.
5 changes: 3 additions & 2 deletions mirascope/beta/openai/realtime/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def from_tool_call(cls, tool_call: FunctionCallArguments) -> OpenAIRealtimeTool:
Args:
tool_call: The OpenAI tool call from which to construct this tool instance.
"""
model_json = jiter.from_json(tool_call["arguments"].encode())
model_json["tool_call"] = tool_call.copy()
model_json = {"tool_call": tool_call.copy()}
if args := tool_call.get("arguments", None):
model_json |= jiter.from_json(args.encode())
return cls.model_validate(model_json)
5 changes: 3 additions & 2 deletions mirascope/core/azure/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def from_tool_call(cls, tool_call: ChatCompletionsToolCall) -> AzureTool:
Args:
tool_call: The Azure tool call from which to construct this tool instance.
"""
model_json = jiter.from_json(tool_call.function.arguments.encode())
model_json["tool_call"] = tool_call
model_json = {"tool_call": tool_call}
if args := tool_call.function.arguments:
model_json |= jiter.from_json(args.encode())
return cls.model_validate(model_json)
7 changes: 3 additions & 4 deletions mirascope/core/gemini/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ def from_tool_call(cls, tool_call: FunctionCall) -> GeminiTool:
Args:
tool_call: The Gemini tool call from which to construct this tool instance.
"""
if not tool_call.args:
raise ValueError("Tool call doesn't have any arguments.")
model_json: dict[str, Any] = dict(tool_call.args.items())
model_json["tool_call"] = tool_call
model_json = {"tool_call": tool_call}
if tool_call.args:
model_json |= dict(tool_call.args.items())
return cls.model_validate(model_json)
5 changes: 3 additions & 2 deletions mirascope/core/groq/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def from_tool_call(cls, tool_call: ChatCompletionMessageToolCall) -> GroqTool:
Args:
tool_call: The Groq tool call from which to construct this tool instance.
"""
model_json = jiter.from_json(tool_call.function.arguments.encode())
model_json["tool_call"] = tool_call.model_dump()
model_json = {"tool_call": tool_call}
if args := tool_call.function.arguments:
model_json |= jiter.from_json(args.encode())
return cls.model_validate(model_json)
9 changes: 5 additions & 4 deletions mirascope/core/mistral/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ def from_tool_call(cls, tool_call: ToolCall) -> MistralTool:
Args:
tool_call: The Mistral tool call from which to construct this tool instance.
"""
model_json = tool_call.function.arguments
if isinstance(model_json, str):
model_json = jiter.from_json(model_json.encode())
model_json["tool_call"] = tool_call.model_dump()
model_json = {"tool_call": tool_call}
if args := tool_call.function.arguments:
model_json |= (
jiter.from_json(args.encode()) if isinstance(args, str) else args
)
return cls.model_validate(model_json)
5 changes: 3 additions & 2 deletions mirascope/core/openai/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def from_tool_call(cls, tool_call: ChatCompletionMessageToolCall) -> OpenAITool:
Args:
tool_call: The OpenAI tool call from which to construct this tool instance.
"""
model_json = jiter.from_json(tool_call.function.arguments.encode())
model_json["tool_call"] = tool_call.model_dump()
model_json = {"tool_call": tool_call}
if args := tool_call.function.arguments:
model_json |= jiter.from_json(args.encode())
return cls.model_validate(model_json)
7 changes: 3 additions & 4 deletions mirascope/core/vertex/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ def from_tool_call(cls, tool_call: FunctionCall) -> VertexTool:
Args:
tool_call: The Vertex tool call from which to construct this tool instance.
"""
if not tool_call.args:
raise ValueError("Tool call doesn't have any arguments.")
model_json: dict[str, Any] = dict(tool_call.args.items())
model_json["tool_call"] = tool_call
model_json = {"tool_call": tool_call}
if tool_call.args:
model_json |= dict(tool_call.args.items())
return cls.model_validate(model_json)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "mirascope"
version = "1.10.0"
version = "1.10.1"
description = "LLM abstractions that aren't obstructions"
readme = "README.md"
license = { file = "LICENSE" }
Expand Down
6 changes: 0 additions & 6 deletions tests/core/gemini/test_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,3 @@ class Nested(GeminiTool):
"with \\$defs.",
):
Nested.tool_schema()


def test_gemini_tool_no_args() -> None:
"""Tests the `GeminiTool` class with no arguments."""
with pytest.raises(ValueError, match="Tool call doesn't have any arguments."):
FormatBook.from_tool_call(FunctionCall(name="FormatBook", args={}))
6 changes: 0 additions & 6 deletions tests/core/vertex/test_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,3 @@ class Nested(VertexTool):
"with \\$defs.",
):
Nested.tool_schema()


def test_vertex_tool_no_args() -> None:
"""Tests the `VertexTool` class with no arguments."""
with pytest.raises(ValueError, match="Tool call doesn't have any arguments."):
FormatBook.from_tool_call(FunctionCall({"name": "FormatBook", "args": {}}))
6 changes: 3 additions & 3 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4365d60

Please sign in to comment.