Skip to content

⚡️ Speed up method Message._string_serializer by 7% #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions openhands/core/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,17 @@ def serialize_model(self) -> dict:

def _string_serializer(self) -> dict:
# convert content to a single string
# Using list comprehension directly for performance
content = '\n'.join(
item.text for item in self.content if isinstance(item, TextContent)
)
message_dict: dict = {'content': content, 'role': self.role}
message_dict = {'content': content, 'role': self.role}

# add tool call keys if we have a tool call or response
return self._add_tool_call_keys(message_dict)
# Skip _add_tool_call_keys if not needed for efficiency
if self.tool_calls or self.tool_call_id:
self._add_tool_call_keys(message_dict)

return message_dict

def _list_serializer(self) -> dict:
content: list[dict] = []
Expand Down Expand Up @@ -135,24 +139,22 @@ def _add_tool_call_keys(self, message_dict: dict) -> dict:
"""
# an assistant message calling a tool
if self.tool_calls is not None:
message_dict['tool_calls'] = [
{
tool_calls_list = []
for tool_call in self.tool_calls:
tool_calls_list.append({
'id': tool_call.id,
'type': 'function',
'function': {
'name': tool_call.function.name,
'arguments': tool_call.function.arguments,
},
}
for tool_call in self.tool_calls
]

})
message_dict['tool_calls'] = tool_calls_list

# an observation message with tool response
if self.tool_call_id is not None:
assert (
self.name is not None
), 'name is required when tool_call_id is not None'
message_dict['tool_call_id'] = self.tool_call_id
message_dict['name'] = self.name

return message_dict