44
55from ldai .tracker import LDAIConfigTracker
66
7+ # Type alias for tool custom parameters: maps tool name -> custom params dict
8+ ToolCustomParametersMap = Dict [str , Dict [str , Any ]]
9+
10+
11+ def _get_tool_custom_parameter (
12+ tool_custom_parameters : Optional ['ToolCustomParametersMap' ],
13+ tool_name : str ,
14+ key : str ,
15+ ) -> Any :
16+ """Retrieve a custom parameter for a specific tool.
17+
18+ :param tool_custom_parameters: The tool custom parameters map.
19+ :param tool_name: The name of the tool.
20+ :param key: The custom parameter key to look up.
21+ :return: The parameter value, or None if not found.
22+ """
23+ if tool_custom_parameters is None :
24+ return None
25+ tool_params = tool_custom_parameters .get (tool_name )
26+ if tool_params is None :
27+ return None
28+ return tool_params .get (key )
29+
30+
31+ def _serialize_tool_custom_parameters (
32+ result : Dict [str , Any ],
33+ tool_custom_parameters : Optional ['ToolCustomParametersMap' ],
34+ ) -> None :
35+ """Serialize tool_custom_parameters into the result dict.
36+
37+ Injects tools into ``result['model']['parameters']['tools']``.
38+ Iteration order follows dict insertion order (Python 3.7+); if the
39+ original flag payload order matters, the caller must ensure it is
40+ preserved at parse time.
41+
42+ :param result: The mutable dict being built by to_dict().
43+ :param tool_custom_parameters: The tool custom parameters map.
44+ """
45+ if tool_custom_parameters is None :
46+ return
47+ model = result .get ('model' ) or {}
48+ params = model .get ('parameters' ) or {}
49+ tools_list = []
50+ for name , custom_params in tool_custom_parameters .items ():
51+ tool_entry : Dict [str , Any ] = {'name' : name }
52+ if custom_params :
53+ tool_entry ['customParameters' ] = custom_params
54+ tools_list .append (tool_entry )
55+ params ['tools' ] = tools_list
56+ model ['parameters' ] = params
57+ result ['model' ] = model
58+
759
860@dataclass
961class LDMessage :
@@ -208,43 +260,33 @@ class AICompletionConfigDefault(AIConfigDefault):
208260 """
209261 messages : Optional [List [LDMessage ]] = None
210262 judge_configuration : Optional [JudgeConfiguration ] = None
211- tool_custom_parameters : Optional [Dict [ str , Dict [ str , Any ]] ] = None
263+ tool_custom_parameters : Optional [ToolCustomParametersMap ] = None
212264
213- def get_tool_custom_parameter (self , tool_name : str , key : str ) -> Any :
214- """
215- Retrieve a custom parameter for a specific tool.
216-
217- :param tool_name: The name of the tool.
218- :param key: The custom parameter key to look up.
219- :return: The parameter value, or None if not found.
220- """
221- if self .tool_custom_parameters is None :
222- return None
223- tool_params = self .tool_custom_parameters .get (tool_name )
224- if tool_params is None :
225- return None
226- return tool_params .get (key )
265+ def get_tool_custom_parameter (
266+ self , tool_name : str , key : str ,
267+ ) -> Any :
268+ """Retrieve a custom parameter for a specific tool."""
269+ return _get_tool_custom_parameter (
270+ self .tool_custom_parameters , tool_name , key ,
271+ )
227272
228273 def to_dict (self ) -> dict :
229- """
230- Render the given default values as an AICompletionConfigDefault-compatible dictionary object.
274+ """Render the given default values as an AICompletionConfigDefault-compatible dictionary object.
275+
276+ Note: tool_custom_parameters are serialized into
277+ ``model.parameters.tools`` so that they are carried inside the
278+ variation dict used as the evaluation fallback. This is how
279+ completion-config defaults reach ``_completion_config`` — there
280+ is no separate ``or default.tool_custom_parameters`` fallback
281+ like agent configs have.
231282 """
232283 result = self ._base_to_dict ()
233284 result ['messages' ] = [message .to_dict () for message in self .messages ] if self .messages else None
234285 if self .judge_configuration is not None :
235286 result ['judgeConfiguration' ] = self .judge_configuration .to_dict ()
236- if self .tool_custom_parameters is not None :
237- model = result .get ('model' ) or {}
238- params = model .get ('parameters' ) or {}
239- tools_list = []
240- for name , custom_params in self .tool_custom_parameters .items ():
241- tool_entry : Dict [str , Any ] = {'name' : name }
242- if custom_params :
243- tool_entry ['customParameters' ] = custom_params
244- tools_list .append (tool_entry )
245- params ['tools' ] = tools_list
246- model ['parameters' ] = params
247- result ['model' ] = model
287+ _serialize_tool_custom_parameters (
288+ result , self .tool_custom_parameters ,
289+ )
248290 return result
249291
250292
@@ -255,22 +297,15 @@ class AICompletionConfig(AIConfig):
255297 """
256298 messages : Optional [List [LDMessage ]] = None
257299 judge_configuration : Optional [JudgeConfiguration ] = None
258- tool_custom_parameters : Optional [Dict [ str , Dict [ str , Any ]] ] = None
300+ tool_custom_parameters : Optional [ToolCustomParametersMap ] = None
259301
260- def get_tool_custom_parameter (self , tool_name : str , key : str ) -> Any :
261- """
262- Retrieve a custom parameter for a specific tool.
263-
264- :param tool_name: The name of the tool.
265- :param key: The custom parameter key to look up.
266- :return: The parameter value, or None if not found.
267- """
268- if self .tool_custom_parameters is None :
269- return None
270- tool_params = self .tool_custom_parameters .get (tool_name )
271- if tool_params is None :
272- return None
273- return tool_params .get (key )
302+ def get_tool_custom_parameter (
303+ self , tool_name : str , key : str ,
304+ ) -> Any :
305+ """Retrieve a custom parameter for a specific tool."""
306+ return _get_tool_custom_parameter (
307+ self .tool_custom_parameters , tool_name , key ,
308+ )
274309
275310 def to_dict (self ) -> dict :
276311 """
@@ -294,22 +329,15 @@ class AIAgentConfigDefault(AIConfigDefault):
294329 """
295330 instructions : Optional [str ] = None
296331 judge_configuration : Optional [JudgeConfiguration ] = None
297- tool_custom_parameters : Optional [Dict [ str , Dict [ str , Any ]] ] = None
332+ tool_custom_parameters : Optional [ToolCustomParametersMap ] = None
298333
299- def get_tool_custom_parameter (self , tool_name : str , key : str ) -> Any :
300- """
301- Retrieve a custom parameter for a specific tool.
302-
303- :param tool_name: The name of the tool.
304- :param key: The custom parameter key to look up.
305- :return: The parameter value, or None if not found.
306- """
307- if self .tool_custom_parameters is None :
308- return None
309- tool_params = self .tool_custom_parameters .get (tool_name )
310- if tool_params is None :
311- return None
312- return tool_params .get (key )
334+ def get_tool_custom_parameter (
335+ self , tool_name : str , key : str ,
336+ ) -> Any :
337+ """Retrieve a custom parameter for a specific tool."""
338+ return _get_tool_custom_parameter (
339+ self .tool_custom_parameters , tool_name , key ,
340+ )
313341
314342 def to_dict (self ) -> Dict [str , Any ]:
315343 """
@@ -320,18 +348,9 @@ def to_dict(self) -> Dict[str, Any]:
320348 result ['instructions' ] = self .instructions
321349 if self .judge_configuration is not None :
322350 result ['judgeConfiguration' ] = self .judge_configuration .to_dict ()
323- if self .tool_custom_parameters is not None :
324- model = result .get ('model' ) or {}
325- params = model .get ('parameters' ) or {}
326- tools_list = []
327- for name , custom_params in self .tool_custom_parameters .items ():
328- tool_entry : Dict [str , Any ] = {'name' : name }
329- if custom_params :
330- tool_entry ['customParameters' ] = custom_params
331- tools_list .append (tool_entry )
332- params ['tools' ] = tools_list
333- model ['parameters' ] = params
334- result ['model' ] = model
351+ _serialize_tool_custom_parameters (
352+ result , self .tool_custom_parameters ,
353+ )
335354 return result
336355
337356
@@ -342,22 +361,15 @@ class AIAgentConfig(AIConfig):
342361 """
343362 instructions : Optional [str ] = None
344363 judge_configuration : Optional [JudgeConfiguration ] = None
345- tool_custom_parameters : Optional [Dict [str , Dict [str , Any ]]] = None
346-
347- def get_tool_custom_parameter (self , tool_name : str , key : str ) -> Any :
348- """
349- Retrieve a custom parameter for a specific tool.
350-
351- :param tool_name: The name of the tool.
352- :param key: The custom parameter key to look up.
353- :return: The parameter value, or None if not found.
354- """
355- if self .tool_custom_parameters is None :
356- return None
357- tool_params = self .tool_custom_parameters .get (tool_name )
358- if tool_params is None :
359- return None
360- return tool_params .get (key )
364+ tool_custom_parameters : Optional [ToolCustomParametersMap ] = None
365+
366+ def get_tool_custom_parameter (
367+ self , tool_name : str , key : str ,
368+ ) -> Any :
369+ """Retrieve a custom parameter for a specific tool."""
370+ return _get_tool_custom_parameter (
371+ self .tool_custom_parameters , tool_name , key ,
372+ )
361373
362374 def to_dict (self ) -> Dict [str , Any ]:
363375 """
0 commit comments