Skip to content
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
7 changes: 5 additions & 2 deletions src/rius/normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ def error_type_from_exception_event(span: ReadableSpan) -> dict[str, Any]:
#: their own beside its schema, as ``llm.tools.{i}.tool.<member>``. Copies of
#: the schema's own fields, so they go wherever the schema goes.
_LLM_TOOL_SCHEMA_COPIES = ("name", "description")
_LLM_TOOL_SCHEMA_SUFFIX = ".tool.json_schema"


def normalize_tool_definitions(attributes: Mapping[str, Any] | None) -> dict[str, Any] | None:
Expand Down Expand Up @@ -641,9 +642,11 @@ def normalize_tool_definitions(attributes: Mapping[str, Any] | None) -> dict[str
return None
schemas.append(schema)
drop = set(indexed.values())
# Each copy is spelled from its schema key as written, so a copy
# goes only when its index is spelled exactly like its schema's.
drop.update(
f"llm.tools.{index}.tool.{member}"
for index in indexed
f"{key.removesuffix(_LLM_TOOL_SCHEMA_SUFFIX)}.tool.{member}"
for key in indexed.values()
for member in _LLM_TOOL_SCHEMA_COPIES
)
rebuilt = {k: v for k, v in attributes.items() if k not in drop}
Expand Down
34 changes: 34 additions & 0 deletions tests/test_normalization_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,40 @@ def test_a_name_without_a_schema_is_not_touched() -> None:
assert out["llm.tools.1.tool.name"] == "b"


def test_a_description_without_a_schema_is_not_touched() -> None:
attributes = {**_indexed([{"name": "a"}]), "llm.tools.1.tool.description": "orphan"}
out = normalize_tool_definitions(attributes)
assert out is not None
assert out["llm.tools.1.tool.description"] == "orphan"


def test_a_native_definitions_key_wins_and_the_name_and_description_still_go() -> None:
attributes = {
GEN_AI_TOOL_DEFINITIONS: '[{"name": "native"}]',
**_indexed(_ANTHROPIC_TOOLS),
"llm.tools.0.tool.name": "get_weather",
"llm.tools.0.tool.description": "Weather for a city",
}
assert normalize_tool_definitions(attributes) == {
GEN_AI_TOOL_DEFINITIONS: '[{"name": "native"}]'
}


def test_a_copy_goes_only_when_spelled_like_its_schema_key() -> None:
# The copy key is the schema key as written, so a zero-padded index
# takes its own padded copy and leaves the unpadded one alone.
attributes = {
"llm.tools.01.tool.json_schema": json.dumps({"name": "a"}),
"llm.tools.1.tool.name": "kept",
"llm.tools.01.tool.name": "a",
}
out = normalize_tool_definitions(attributes)
assert out is not None
assert _definitions(out) == [{"type": "function", "name": "a"}]
assert out["llm.tools.1.tool.name"] == "kept"
assert "llm.tools.01.tool.name" not in out


def test_an_unparseable_schema_leaves_the_name_and_description_too() -> None:
attributes = {
"llm.tools.0.tool.json_schema": "{not json",
Expand Down
Loading