Skip to content

Commit 2e173ca

Browse files
feat: add dict-like access to ToolFunction and ToolCall for improved integration with downstream hooks
1 parent ecbe9f9 commit 2e173ca

1 file changed

Lines changed: 36 additions & 2 deletions

File tree

tinyagent/core/openai_responses_adapter.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ def __init__(self, name: str, arguments: str):
3434
def to_dict(self) -> Dict[str, Any]:
3535
return {"name": self.name, "arguments": self.arguments}
3636

37+
# Provide dict-like access for downstream hooks expecting Chat-style dicts
38+
def get(self, key: str, default: Any = None) -> Any:
39+
if key == "name":
40+
return self.name
41+
if key == "arguments":
42+
return self.arguments
43+
return default
44+
45+
def __getitem__(self, key: str) -> Any:
46+
val = self.get(key)
47+
if val is None:
48+
raise KeyError(key)
49+
return val
50+
3751

3852
class ToolCall:
3953
"""Represents a single tool call with id + function field."""
@@ -45,6 +59,20 @@ def __init__(self, call_id: str, function: ToolFunction):
4559
def to_dict(self) -> Dict[str, Any]:
4660
return {"id": self.id, "function": self.function.to_dict()}
4761

62+
# Provide dict-like access for downstream hooks expecting Chat-style dicts
63+
def get(self, key: str, default: Any = None) -> Any:
64+
if key == "id":
65+
return self.id
66+
if key == "function":
67+
return self.function
68+
return default
69+
70+
def __getitem__(self, key: str) -> Any:
71+
val = self.get(key)
72+
if val is None:
73+
raise KeyError(key)
74+
return val
75+
4876

4977
class OpenAIResponsesAdapter:
5078
"""
@@ -140,9 +168,15 @@ def to_responses_request(
140168
"output": str(output),
141169
})
142170

143-
# Now set input: for chaining send only tool outputs; for initial turn send the last user string
171+
# Now set input:
172+
# - If chaining and we have tool results, send only function_call_output items
173+
# - If chaining but no tool results, pass the last user string to continue the thread
174+
# - If initial turn, pass the last user string
144175
if previous_response_id:
145-
req["input"] = results_items if results_items else ""
176+
if results_items:
177+
req["input"] = results_items
178+
else:
179+
req["input"] = (user_messages[-1] if user_messages else "")
146180
else:
147181
# Prefer last user message content as a simple string to avoid noisy history
148182
if user_messages:

0 commit comments

Comments
 (0)