Skip to content

Commit

Permalink
Showing 4 changed files with 25 additions and 3 deletions.
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
@@ -54,6 +54,7 @@ class FunctionArgInfo:
type: type
default: Any
choices: tuple | None
is_optional: bool


@dataclass(frozen=True)
@@ -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(
Original file line number Diff line number Diff line change
@@ -64,13 +64,19 @@ def create_ai_function_info(
inner_type = typing.get_args(arg_info.type)[0]
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
@@ -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)}")

0 comments on commit 33fdc53

Please sign in to comment.