-
Couldn't load subscription status.
- Fork 183
ROB-2116: prevent tool calls responses that are too big #956
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
Merged
nherment
merged 8 commits into
master
from
ROB-2116_prevent_tool_call_results_that_are_too_big
Sep 11, 2025
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
788497f
feat: prevent tool calls responses that are too big
nherment 3bff6de
Merge branch 'master' into ROB-2116_prevent_tool_call_results_that_ar…
nherment bd67fdc
fix: improve message for tool that violates allowed token size
nherment bb54aee
chore: address PR comments
nherment 0c6ff75
Merge branch 'master' into ROB-2116_prevent_tool_call_results_that_ar…
nherment 500cdee
Merge branch 'master' into ROB-2116_prevent_tool_call_results_that_ar…
nherment 51d33ba
chore: address PR comments
nherment b51963b
fix: tests
nherment File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import json | ||
| from typing import Optional | ||
| from pydantic import BaseModel | ||
|
|
||
| from holmes.core.tools import StructuredToolResult, StructuredToolResultStatus | ||
|
|
||
|
|
||
| class TruncationMetadata(BaseModel): | ||
| tool_call_id: str | ||
| start_index: int | ||
| end_index: int | ||
|
|
||
|
|
||
| class TruncationResult(BaseModel): | ||
| truncated_messages: list[dict] | ||
| truncations: list[TruncationMetadata] | ||
|
|
||
|
|
||
| def format_tool_result_data(tool_result: StructuredToolResult) -> str: | ||
| tool_response = tool_result.data | ||
| if isinstance(tool_result.data, str): | ||
| tool_response = tool_result.data | ||
| else: | ||
| try: | ||
| if isinstance(tool_result.data, BaseModel): | ||
| tool_response = tool_result.data.model_dump_json(indent=2) | ||
| else: | ||
| tool_response = json.dumps(tool_result.data, indent=2) | ||
| except Exception: | ||
| tool_response = str(tool_result.data) | ||
| if tool_result.status == StructuredToolResultStatus.ERROR: | ||
| tool_response = f"{tool_result.error or 'Tool execution failed'}:\n\n{tool_result.data or ''}".strip() | ||
| return tool_response | ||
|
|
||
|
|
||
| class ToolCallResult(BaseModel): | ||
| tool_call_id: str | ||
| tool_name: str | ||
| description: str | ||
| result: StructuredToolResult | ||
| size: Optional[int] = None | ||
|
|
||
| def as_tool_call_message(self): | ||
| content = format_tool_result_data(self.result) | ||
| if self.result.params: | ||
| content = ( | ||
| f"Params used for the tool call: {json.dumps(self.result.params)}. The tool call output follows on the next line.\n" | ||
| + content | ||
| ) | ||
| return { | ||
| "tool_call_id": self.tool_call_id, | ||
| "role": "tool", | ||
| "name": self.tool_name, | ||
| "content": content, | ||
| } | ||
|
|
||
| def as_tool_result_response(self): | ||
| result_dump = self.result.model_dump() | ||
| result_dump["data"] = self.result.get_stringified_data() | ||
|
|
||
| return { | ||
| "tool_call_id": self.tool_call_id, | ||
| "tool_name": self.tool_name, | ||
| "description": self.description, | ||
| "role": "tool", | ||
| "result": result_dump, | ||
| } | ||
|
|
||
| def as_streaming_tool_result_response(self): | ||
| result_dump = self.result.model_dump() | ||
| result_dump["data"] = self.result.get_stringified_data() | ||
|
|
||
| return { | ||
| "tool_call_id": self.tool_call_id, | ||
| "role": "tool", | ||
| "description": self.description, | ||
| "name": self.tool_name, | ||
| "result": result_dump, | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.