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