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
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,7 @@ fn ensure_anthropic_tool_input_object(arguments: Value) -> Value {

// Encodes normalized tool definitions into Anthropic tool JSON.
fn encode_anthropic_tools(tools: &[Tool]) -> Result<Value> {
let mut names = std::collections::HashSet::new();
Ok(Value::Array(
tools
.iter()
Expand All @@ -1277,6 +1278,12 @@ fn encode_anthropic_tools(tools: &[Tool]) -> Result<Value> {
"MCP and provider tool encoding is not implemented".into(),
)
})?;
if !names.insert(tool.name.as_str()) {
return Err(TranslationError::InvalidValue {
path: "$.tools".to_string(),
message: format!("duplicate tool name: {:?}", tool.name),
});
}
let mut item = json!({
"name": tool.name,
"description": tool.description.clone().unwrap_or_default(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,7 @@ fn media_source_text(source: &MediaSource) -> String {

/// Encodes normalized tool definitions into OpenAI tool JSON.
pub(crate) fn encode_openai_tools(tools: &[Tool]) -> Result<Value> {
let mut names = std::collections::HashSet::new();
Ok(Value::Array(
tools
.iter()
Expand All @@ -1211,6 +1212,12 @@ pub(crate) fn encode_openai_tools(tools: &[Tool]) -> Result<Value> {
"MCP and provider tool encoding is not implemented".into(),
)
})?;
if !names.insert(tool.name.as_str()) {
return Err(TranslationError::InvalidValue {
path: "$.tools".to_string(),
message: format!("duplicate tool name: {:?}", tool.name),
});
}
let mut function = json!({
"name": tool.name,
"description": tool.description.clone().unwrap_or_default(),
Expand Down
48 changes: 48 additions & 0 deletions tests/test_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,54 @@ def test_response_ir_restores_request_tool_extensions(method) -> None:
ValueError,
id="request",
),
pytest.param(
"translate_request",
(
"openai_responses",
"openai_chat",
{
"tools": [
{
"type": "namespace",
"name": "a__b",
"tools": [{"type": "function", "name": "c", "parameters": {}}],
},
{
"type": "namespace",
"name": "a",
"tools": [{"type": "function", "name": "b__c", "parameters": {}}],
},
],
},
),
{},
TranslationError,
id="namespace-tool-name-collision",
),
pytest.param(
"translate_request",
(
"openai_responses",
"anthropic_messages",
{
"tools": [
{
"type": "namespace",
"name": "a__b",
"tools": [{"type": "function", "name": "c", "parameters": {}}],
},
{
"type": "namespace",
"name": "a",
"tools": [{"type": "function", "name": "b__c", "parameters": {}}],
},
],
},
),
{},
TranslationError,
id="namespace-tool-name-collision-anthropic",
),
pytest.param(
"encode_response",
("openai_chat", {}),
Expand Down
Loading