@@ -1406,273 +1406,6 @@ func TestFinishReasonTranslation(t *testing.T) {
14061406 }
14071407}
14081408
1409- // TestToolParameterDereferencing tests the JSON schema dereferencing functionality
1410- // for tool parameters when translating from OpenAI to GCP Anthropic.
1411- func TestToolParameterDereferencing (t * testing.T ) {
1412- tests := []struct {
1413- name string
1414- openAIReq * openai.ChatCompletionRequest
1415- expectedTools []anthropic.ToolUnionParam
1416- expectedToolChoice anthropic.ToolChoiceUnionParam
1417- expectErr bool
1418- expectedErrMsg string
1419- }{
1420- {
1421- name : "tool with complex nested $ref - successful dereferencing" ,
1422- openAIReq : & openai.ChatCompletionRequest {
1423- Tools : []openai.Tool {
1424- {
1425- Type : "function" ,
1426- Function : & openai.FunctionDefinition {
1427- Name : "complex_tool" ,
1428- Description : "Tool with complex nested references" ,
1429- Parameters : map [string ]any {
1430- "type" : "object" ,
1431- "$defs" : map [string ]any {
1432- "BaseType" : map [string ]any {
1433- "type" : "object" ,
1434- "properties" : map [string ]any {
1435- "id" : map [string ]any {
1436- "type" : "string" ,
1437- },
1438- "required" : []any {"id" },
1439- },
1440- },
1441- "NestedType" : map [string ]any {
1442- "allOf" : []any {
1443- map [string ]any {"$ref" : "#/$defs/BaseType" },
1444- map [string ]any {
1445- "properties" : map [string ]any {
1446- "name" : map [string ]any {
1447- "type" : "string" ,
1448- },
1449- },
1450- },
1451- },
1452- },
1453- },
1454- "properties" : map [string ]any {
1455- "nested" : map [string ]any {
1456- "$ref" : "#/$defs/NestedType" ,
1457- },
1458- },
1459- },
1460- },
1461- },
1462- },
1463- },
1464- expectedTools : []anthropic.ToolUnionParam {
1465- {
1466- OfTool : & anthropic.ToolParam {
1467- Name : "complex_tool" ,
1468- Description : anthropic .String ("Tool with complex nested references" ),
1469- InputSchema : anthropic.ToolInputSchemaParam {
1470- Type : "object" ,
1471- Properties : map [string ]any {
1472- "nested" : map [string ]any {
1473- "allOf" : []any {
1474- map [string ]any {
1475- "type" : "object" ,
1476- "properties" : map [string ]any {
1477- "id" : map [string ]any {
1478- "type" : "string" ,
1479- },
1480- "required" : []any {"id" },
1481- },
1482- },
1483- map [string ]any {
1484- "properties" : map [string ]any {
1485- "name" : map [string ]any {
1486- "type" : "string" ,
1487- },
1488- },
1489- },
1490- },
1491- },
1492- },
1493- },
1494- },
1495- },
1496- },
1497- },
1498- {
1499- name : "tool with invalid $ref - dereferencing error" ,
1500- openAIReq : & openai.ChatCompletionRequest {
1501- Tools : []openai.Tool {
1502- {
1503- Type : "function" ,
1504- Function : & openai.FunctionDefinition {
1505- Name : "invalid_ref_tool" ,
1506- Description : "Tool with invalid reference" ,
1507- Parameters : map [string ]any {
1508- "type" : "object" ,
1509- "properties" : map [string ]any {
1510- "location" : map [string ]any {
1511- "$ref" : "#/$defs/NonExistent" ,
1512- },
1513- },
1514- },
1515- },
1516- },
1517- },
1518- },
1519- expectErr : true ,
1520- expectedErrMsg : "failed to dereference tool parameters" ,
1521- },
1522- {
1523- name : "tool with circular $ref - dereferencing error" ,
1524- openAIReq : & openai.ChatCompletionRequest {
1525- Tools : []openai.Tool {
1526- {
1527- Type : "function" ,
1528- Function : & openai.FunctionDefinition {
1529- Name : "circular_ref_tool" ,
1530- Description : "Tool with circular reference" ,
1531- Parameters : map [string ]any {
1532- "type" : "object" ,
1533- "$defs" : map [string ]any {
1534- "A" : map [string ]any {
1535- "type" : "object" ,
1536- "properties" : map [string ]any {
1537- "b" : map [string ]any {
1538- "$ref" : "#/$defs/B" ,
1539- },
1540- },
1541- },
1542- "B" : map [string ]any {
1543- "type" : "object" ,
1544- "properties" : map [string ]any {
1545- "a" : map [string ]any {
1546- "$ref" : "#/$defs/A" ,
1547- },
1548- },
1549- },
1550- },
1551- "properties" : map [string ]any {
1552- "circular" : map [string ]any {
1553- "$ref" : "#/$defs/A" ,
1554- },
1555- },
1556- },
1557- },
1558- },
1559- },
1560- },
1561- expectErr : true ,
1562- expectedErrMsg : "failed to dereference tool parameters" ,
1563- },
1564- {
1565- name : "tool without $ref - no dereferencing needed" ,
1566- openAIReq : & openai.ChatCompletionRequest {
1567- Tools : []openai.Tool {
1568- {
1569- Type : "function" ,
1570- Function : & openai.FunctionDefinition {
1571- Name : "simple_tool" ,
1572- Description : "Simple tool without references" ,
1573- Parameters : map [string ]any {
1574- "type" : "object" ,
1575- "properties" : map [string ]any {
1576- "location" : map [string ]any {
1577- "type" : "string" ,
1578- },
1579- },
1580- "required" : []any {"location" },
1581- },
1582- },
1583- },
1584- },
1585- },
1586- expectedTools : []anthropic.ToolUnionParam {
1587- {
1588- OfTool : & anthropic.ToolParam {
1589- Name : "simple_tool" ,
1590- Description : anthropic .String ("Simple tool without references" ),
1591- InputSchema : anthropic.ToolInputSchemaParam {
1592- Type : "object" ,
1593- Properties : map [string ]any {
1594- "location" : map [string ]any {
1595- "type" : "string" ,
1596- },
1597- },
1598- Required : []string {"location" },
1599- },
1600- },
1601- },
1602- },
1603- },
1604- {
1605- name : "tool parameter dereferencing returns non-map type - casting error" ,
1606- openAIReq : & openai.ChatCompletionRequest {
1607- Tools : []openai.Tool {
1608- {
1609- Type : "function" ,
1610- Function : & openai.FunctionDefinition {
1611- Name : "problematic_tool" ,
1612- Description : "Tool with parameters that can't be properly dereferenced to map" ,
1613- // This creates a scenario where jsonSchemaDereference might return a non-map type
1614- // though this is a contrived example since normally the function should return map[string]any
1615- Parameters : map [string ]any {
1616- "$ref" : "#/$defs/StringType" , // This would resolve to a string, not a map
1617- "$defs" : map [string ]any {
1618- "StringType" : "not-a-map" , // This would cause the casting to fail
1619- },
1620- },
1621- },
1622- },
1623- },
1624- },
1625- expectErr : true ,
1626- expectedErrMsg : "failed to cast dereferenced tool parameters" ,
1627- },
1628- }
1629-
1630- for _ , tt := range tests {
1631- t .Run (tt .name , func (t * testing.T ) {
1632- tools , toolChoice , err := translateOpenAItoAnthropicTools (tt .openAIReq .Tools , tt .openAIReq .ToolChoice , tt .openAIReq .ParallelToolCalls )
1633-
1634- if tt .expectErr {
1635- require .Error (t , err )
1636- if tt .expectedErrMsg != "" {
1637- require .Contains (t , err .Error (), tt .expectedErrMsg )
1638- }
1639- return
1640- }
1641-
1642- require .NoError (t , err )
1643-
1644- if tt .openAIReq .Tools != nil {
1645- require .NotNil (t , tools )
1646- require .Len (t , tools , len (tt .expectedTools ))
1647-
1648- for i , expectedTool := range tt .expectedTools {
1649- actualTool := tools [i ]
1650- require .Equal (t , expectedTool .GetName (), actualTool .GetName ())
1651- require .Equal (t , expectedTool .GetType (), actualTool .GetType ())
1652- require .Equal (t , expectedTool .GetDescription (), actualTool .GetDescription ())
1653-
1654- expectedSchema := expectedTool .GetInputSchema ()
1655- actualSchema := actualTool .GetInputSchema ()
1656-
1657- require .Equal (t , expectedSchema .Type , actualSchema .Type )
1658- require .Equal (t , expectedSchema .Required , actualSchema .Required )
1659-
1660- // For properties, we'll do a deep comparison to verify dereferencing worked
1661- if expectedSchema .Properties != nil {
1662- require .NotNil (t , actualSchema .Properties )
1663- require .Equal (t , expectedSchema .Properties , actualSchema .Properties )
1664- }
1665- }
1666- }
1667-
1668- if tt .openAIReq .ToolChoice != nil {
1669- require .NotNil (t , toolChoice )
1670- require .Equal (t , * tt .expectedToolChoice .GetType (), * toolChoice .GetType ())
1671- }
1672- })
1673- }
1674- }
1675-
16761409// TestContentTranslationCoverage adds specific coverage for the openAIToAnthropicContent helper.
16771410func TestContentTranslationCoverage (t * testing.T ) {
16781411 tests := []struct {
0 commit comments