feat(tools): ToolOutput / ToolError + ToolResult wire serialization#267
Merged
Conversation
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
4855157 to
86372c3
Compare
…ring Resolves #266. Adds two generic Pydantic types — `ToolOutput[Content]` and `ToolError[Content]` — for executable tools to return structured results. Both exposed at the top-level `celeste` namespace. Per #266's directive ("Use direct Pydantic APIs at the exact boundary; do not add a generic Celeste serialization helper"), tool-result wire serialization stays inline using direct Pydantic API at each call site: - `BaseModel` content → `content.model_dump_json()` (string-content providers) or `content.model_dump(mode="json")` (Google). - `str` content → passthrough. - `dict`/`list`/scalar content → `json.dumps(content, default=str)` so the LLM receives valid JSON, not Python `repr`. Move text-modality message lowering into the modality protocol layer (per #266): chatcompletions and openresponses now have `_chat_completions_text_messages` / `_openresponses_text_input_messages` helpers in `src/celeste/modalities/text/protocols/<protocol>/client.py`. The lower-level `src/celeste/protocols/<protocol>/tools.py` modules no longer import `Message` or `ToolResult`. Cohere fix (also #266): dump messages with `mode="json", serialize_as_any=True, exclude_none=True` so nested Pydantic `Message.content` is preserved instead of dumped as `{}`. Google left as-is — its inline `if isinstance(content, BaseModel): content.model_dump(mode="json")` already matches #266's directive. Removes `_debug_dump_wire` TEMP diagnostic from chatcompletions client (self-acknowledged temp code that fails bandit Medium severity check). Drops 4 unused supporting imports. Tests: 6 integration-shape tests for tool-result request payloads across all 5 providers (chatcompletions, openresponses, anthropic, google, cohere).
86372c3 to
e875571
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Resolves #266.
Summary
Two pieces:
src/celeste/tools.py):ToolOutput[Content]andToolError[Content]Pydantic generics for executable tools to return structured results. Both exposed at top-levelcelestenamespace.Changes (per #266)
Use direct Pydantic API at the exact boundary — no celeste serialization helper, no methods on
ToolResult:BaseModelcontent →content.model_dump_json()(string-content providers) orcontent.model_dump(mode="json")(Google)strcontent → passthroughdict/list/scalar →json.dumps(content, default=str)(fixes the existing Pythonreprbug for non-BaseModel non-str content)Move text-modality message lowering into the modality protocol layer:
_chat_completions_text_messages/_openresponses_text_input_messagesinsrc/celeste/modalities/text/protocols/<protocol>/client.pysrc/celeste/protocols/<protocol>/tools.pymodules no longer importMessageorToolResultCohere fix: dump messages with
mode="json", serialize_as_any=True, exclude_none=Trueso nested PydanticMessage.contentis preserved instead of dumped as{}.Google left as-is — its inline
if isinstance(content, BaseModel): content.model_dump(mode="json")already matches #266's directive.Removes
_debug_dump_wireTEMP diagnostic fromprotocols/chatcompletions/client.py(self-acknowledged temp code that fails bandit Medium severity check). Drops 4 unused supporting imports.Test plan
uv run pytest tests/unit_tests/ -n auto -q— 607 passedToolResult(content=ToolOutput(...))as JSON text ✓ToolResult(content=ToolOutput(...))as JSON text ✓ToolResult(content=ToolOutput(...))as JSON text ✓Message.contentinstead of dumping it as{}✓Coordination
Unblocks downstream celeste-tools — after this merges, celeste-tools can switch its
celeste-aidep from path install back to git source.