Skip to content

Commit 9933d6e

Browse files
committed
Apply changes from commit 3367e98
Original commit by Jay Hack: fix: maximum observation length + error (codegen-sh#919)
1 parent 078131d commit 9933d6e

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/codegen/extensions/tools/observation.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
from langchain_core.messages import ToolMessage
77
from pydantic import BaseModel, Field
88

9+
from codegen.shared.logging.get_logger import get_logger
10+
11+
logger = get_logger(__name__)
12+
913

1014
class Observation(BaseModel):
1115
"""Base class for all tool observations.
@@ -44,14 +48,18 @@ def __repr__(self) -> str:
4448
"""Get detailed string representation of the observation."""
4549
return f"{self.__class__.__name__}({self.model_dump_json()})"
4650

47-
def render_as_string(self) -> str:
51+
def render_as_string(self, max_tokens: int = 8000) -> str:
4852
"""Render the observation as a string.
4953
5054
This is used for string representation and as the content field
5155
in the ToolMessage. Subclasses can override this to customize
5256
their string output format.
5357
"""
54-
return json.dumps(self.model_dump(), indent=2)
58+
rendered = json.dumps(self.model_dump(), indent=2)
59+
if 3 * len(rendered) > max_tokens:
60+
logger.error(f"Observation is too long to render: {len(rendered) * 3} tokens")
61+
return rendered[:max_tokens] + "\n\n...truncated...\n\n"
62+
return rendered
5563

5664
def render(self, tool_call_id: Optional[str] = None) -> ToolMessage | str:
5765
"""Render the observation as a ToolMessage or string.

0 commit comments

Comments
 (0)