Skip to content
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

fix: maximum observation length + error #919

Merged
merged 2 commits into from
Mar 20, 2025
Merged
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
12 changes: 10 additions & 2 deletions src/codegen/extensions/tools/observation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from langchain_core.messages import ToolMessage
from pydantic import BaseModel, Field

from codegen.shared.logging.get_logger import get_logger

logger = get_logger(__name__)


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

def render_as_string(self) -> str:
def render_as_string(self, max_tokens: int = 8000) -> str:
"""Render the observation as a string.

This is used for string representation and as the content field
in the ToolMessage. Subclasses can override this to customize
their string output format.
"""
return json.dumps(self.model_dump(), indent=2)
rendered = json.dumps(self.model_dump(), indent=2)
if 3 * len(rendered) > max_tokens:
logger.error(f"Observation is too long to render: {len(rendered) * 3} tokens")
return rendered[:max_tokens] + "\n\n...truncated...\n\n"
return rendered

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