Skip to content
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

Handle optional func args in tool calls when set to None #1211

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lazy-boats-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-plugins-openai": patch
---

Handle optional func args in tool calls when set to `None`
6 changes: 6 additions & 0 deletions .changeset/wild-plants-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"livekit-plugins-openai": patch
"livekit-agents": patch
---

fix: Handle optional func args in tool calls when set to `None`
2 changes: 2 additions & 0 deletions livekit-agents/livekit/agents/llm/function_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class FunctionArgInfo:
type: type
default: Any
choices: tuple | None
is_optional: bool


@dataclass(frozen=True)
Expand Down Expand Up @@ -188,6 +189,7 @@ def _register_ai_function(self, fnc: Callable) -> None:
type=inner_th,
default=param.default,
choices=choices,
is_optional=is_optional,
)

self._fncs[metadata.name] = FunctionInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,19 @@ def create_ai_function_info(
inner_type = typing.get_args(arg_info.type)[0]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fetching the first argument does not work for type definitions like Union[None, str] or None | str, where the None is passed as the first argument. The validation should verify if arguments lengths is between 1 and 2 and then take the first matched non-None-type argument.

sanitized_value = [
_sanitize_primitive(
value=v, expected_type=inner_type, choices=arg_info.choices
value=v,
expected_type=inner_type,
choices=arg_info.choices,
is_optional=arg_info.is_optional,
)
for v in arg_value
]
else:
sanitized_value = _sanitize_primitive(
value=arg_value, expected_type=arg_info.type, choices=arg_info.choices
value=arg_value,
expected_type=arg_info.type,
choices=arg_info.choices,
is_optional=arg_info.is_optional,
)

sanitized_arguments[arg_info.name] = sanitized_value
Expand Down Expand Up @@ -147,8 +153,11 @@ def type2str(t: type) -> str:


def _sanitize_primitive(
*, value: Any, expected_type: type, choices: tuple | None
*, value: Any, expected_type: type, choices: tuple | None, is_optional: bool = False
) -> Any:
if is_optional and value is None:
return None

if expected_type is str:
if not isinstance(value, str):
raise ValueError(f"expected str, got {type(value)}")
Expand Down
Loading