<This PR > adds structure to the `Observation` cl...
# dev-pr-review
s
This PR adds structure to the
Observation
class to make sure tools are consistent with error handling and llm serialization. Unfortunately this PR changes lots of files. If this PR is too big to merge before a release, I can break it into pieces, or wait until after the release. Proposed Observation structure:
Copy code
class Observation(Schema, ABC):
    """Base schema for output observation."""

    output: list[TextContent | ImageContent] = Field(
        default_factory=list,
        description=(
            "Output returned from the tool converted to LLM Ready "
            "TextContent or ImageContent"
        ),
    )
    error: str | None = Field(
        default=None, description="Error message if operation failed"
    )

    @property
    def command(self) -> str | None:
        """
        The command that was executed to produce this observation.
        Subclasses can override to provide the actual command run.
        """
        return None

    @property
    def has_error(self) -> bool:
        """
        Check if the observation indicates an error.
        """
        return bool(self.error)

    @property
    def result_status(self) -> ObservationStatus:
        """
        Get the observation result status based on presence of error."""
        return ObservationStatus.ERROR if self.has_error else ObservationStatus.SUCCESS

    def format_error(self) -> TextContent:
        """Format the error message for LLM display."""
        return TextContent(text=f"Tool Execution Error: {self.error}")

    @property
    def to_llm_content(self) -> Sequence[TextContent | ImageContent]:
        """
        Default content formatting for converting observation to LLM readable content.
        Subclasses can override to provide richer content (e.g., images, diffs).
        Errors can be partial so both output and error are included if present.
        """
        llm_content: list[TextContent | ImageContent] = []
        if self.command:
            llm_content.append(TextContent(text=f"Executed Command: {self.command}\n"))
        if self.error:
            llm_content.append(self.format_error())
        if self.output:
            llm_content.extend(self.output)
        return llm_content
❤️ 2
c
I don't think I'd have time today for an in-depth look, but FWIW this is an awesome initiative! I think we need something like this. I'm kind of happy it was a bit of a mess, because observations grew organically. (which is a good thing). Now we can see what they grew into.
s
Thanks! your quick review was right btw, removing command from the base class. Something I am unsure of is
output: list[ImageContent | TextContent]
because its an overkill signature for most tools... but it's the only signature that works for all tools.
c
I... don't fully understand that either, because TextContent (and ImageContext) are helpers of Message class, which is for sending to the LLM. How could / should tools ever even know them? It's like, another layer... 🤔
Maybe we could return whatever we had previously, in MCPObservation, and convert to a list of these later