@@ -11,6 +11,33 @@ import {
1111} from "@helicone-package/prompts/types" ;
1212import { ResponsesToolDefinition } from "../../../types/responses" ;
1313
14+ /**
15+ * Azure has a 40 character limit on tool_call_id.
16+ * This function truncates long IDs deterministically so that:
17+ * 1. IDs <= 40 chars are unchanged
18+ * 2. IDs > 40 chars are shortened to prefix + hash suffix
19+ * The same input always produces the same output, ensuring tool_calls
20+ * and their corresponding tool responses match.
21+ */
22+ const AZURE_TOOL_CALL_ID_LIMIT = 40 ;
23+
24+ function truncateToolCallId ( id : string ) : string {
25+ if ( id . length <= AZURE_TOOL_CALL_ID_LIMIT ) {
26+ return id ;
27+ }
28+ // Use a simple deterministic hash for the suffix
29+ let hash = 0 ;
30+ for ( let i = 0 ; i < id . length ; i ++ ) {
31+ const char = id . charCodeAt ( i ) ;
32+ hash = ( ( hash << 5 ) - hash ) + char ;
33+ hash = hash & hash ; // Convert to 32-bit integer
34+ }
35+ const hashStr = Math . abs ( hash ) . toString ( 36 ) ;
36+ // Keep prefix + underscore + hash, ensuring total <= 40 chars
37+ const prefixLength = AZURE_TOOL_CALL_ID_LIMIT - hashStr . length - 1 ;
38+ return `${ id . substring ( 0 , prefixLength ) } _${ hashStr } ` ;
39+ }
40+
1441function mapRole ( role : string ) : "system" | "user" | "assistant" | "tool" | "function" {
1542 if ( role === "developer" ) return "system" ;
1643 if ( role === "system" || role === "user" || role === "assistant" ) return role ;
@@ -88,7 +115,7 @@ function convertInputToMessages(input: ResponsesRequestBody["input"]) {
88115 > ( input , i , "function_call" ) ;
89116
90117 const toolCalls = functionCalls . map ( ( fc , idx ) => ( {
91- id : fc . id || fc . call_id || `call_${ i + idx } ` ,
118+ id : truncateToolCallId ( fc . id || fc . call_id || `call_${ i + idx } ` ) ,
92119 type : "function" as const ,
93120 function : {
94121 name : fc . name ,
@@ -110,7 +137,7 @@ function convertInputToMessages(input: ResponsesRequestBody["input"]) {
110137 const fco = item ;
111138 messages . push ( {
112139 role : "tool" ,
113- tool_call_id : fco . call_id ,
140+ tool_call_id : truncateToolCallId ( fco . call_id ) ,
114141 content : fco . output ?? "" ,
115142 } ) ;
116143 continue ;
0 commit comments