Skip to content

Commit 06e324f

Browse files
committed
fix: add deprecated invoke_model shims and translate tool names to LD config keys in OpenAIAgentRunner
1 parent b6ff570 commit 06e324f

2 files changed

Lines changed: 60 additions & 3 deletions

File tree

packages/ai-providers/server-ai-openai/src/ldai_openai/openai_agent_runner.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from ldai_openai.openai_helper import (
99
get_ai_usage_from_response,
1010
get_tool_calls_from_run_items,
11+
is_agent_tool_instance,
1112
registry_value_to_agent_tool,
1213
)
1314

@@ -42,6 +43,7 @@ def __init__(
4243
self._instructions = instructions
4344
self._tool_definitions = tool_definitions
4445
self._tools = tools
46+
self._tool_name_map: Dict[str, str] = {}
4547

4648
async def run(
4749
self,
@@ -87,8 +89,10 @@ async def run(
8789
result = await Runner.run(agent, str(input), max_turns=25)
8890

8991
tool_calls = [
90-
tool_name
91-
for _agent_name, tool_name in get_tool_calls_from_run_items(result.new_items)
92+
ld_name
93+
for _agent_name, tool_fn_name in get_tool_calls_from_run_items(result.new_items)
94+
for ld_name in [self._tool_name_map.get(tool_fn_name)]
95+
if ld_name is not None
9296
]
9397

9498
return RunnerResult(
@@ -108,8 +112,14 @@ async def run(
108112
)
109113

110114
def _build_agent_tools(self) -> List[Any]:
111-
"""Build tool instances from LD tool definitions and registry."""
115+
"""Build tool instances from LD tool definitions and registry.
116+
117+
Also populates ``self._tool_name_map`` so observed tool-call names
118+
from the runtime can be translated back to their LD config keys for
119+
metric reporting.
120+
"""
112121
tools = []
122+
self._tool_name_map = {}
113123
for td in self._tool_definitions:
114124
if not isinstance(td, dict):
115125
continue
@@ -119,6 +129,12 @@ def _build_agent_tools(self) -> List[Any]:
119129

120130
tool_fn = self._tools.get(name)
121131
if tool_fn:
132+
# Map runtime tool name → LD config key for metrics (function __name__
133+
# for callables; identity for native tool instances — see get_tool_calls_from_run_items).
134+
if is_agent_tool_instance(tool_fn):
135+
self._tool_name_map[tool_fn.name] = name
136+
else:
137+
self._tool_name_map[tool_fn.__name__] = name
122138
tools.append(registry_value_to_agent_tool(tool_fn))
123139
continue
124140

packages/ai-providers/server-ai-openai/src/ldai_openai/openai_model_runner.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,44 @@ def _extract_content(response: Any) -> str:
151151
if message and message.content:
152152
return message.content
153153
return ''
154+
155+
async def invoke_model(self, messages: List[LDMessage]) -> 'ModelResponse':
156+
"""
157+
.. deprecated::
158+
Use :meth:`run` instead.
159+
"""
160+
import warnings
161+
162+
from ldai.providers.types import ModelResponse
163+
warnings.warn(
164+
"invoke_model is deprecated; use run() instead",
165+
DeprecationWarning,
166+
stacklevel=2,
167+
)
168+
result = await self.run(messages)
169+
return ModelResponse(
170+
message=LDMessage(role='assistant', content=result.content),
171+
metrics=result.metrics,
172+
)
173+
174+
async def invoke_structured_model(
175+
self, messages: List[LDMessage], schema: Any
176+
) -> 'StructuredResponse':
177+
"""
178+
.. deprecated::
179+
Use :meth:`run` with ``output_type`` instead.
180+
"""
181+
import warnings
182+
183+
from ldai.providers.types import StructuredResponse
184+
warnings.warn(
185+
"invoke_structured_model is deprecated; use run(output_type=...) instead",
186+
DeprecationWarning,
187+
stacklevel=2,
188+
)
189+
result = await self.run(messages, output_type=schema)
190+
return StructuredResponse(
191+
data=result.parsed or {},
192+
raw_response=result.content,
193+
metrics=result.metrics,
194+
)

0 commit comments

Comments
 (0)