Python: Flatten hyperlight execute_code output (#5333)

* small fix for hyperlight

* improved sandbox dependency
This commit is contained in:
Eduard van Valkenburg
2026-04-20 10:29:40 +02:00
committed by GitHub
Unverified
parent 495e1dad6b
commit 69894eded8
7 changed files with 309 additions and 90 deletions
@@ -431,7 +431,7 @@ def _build_execution_contents(
outputs.append(Content.from_text(stderr, raw_representation=result))
if not outputs:
outputs.append(Content.from_text("Code executed successfully without output."))
return [Content.from_code_interpreter_tool_result(outputs=outputs, raw_representation=result)]
return outputs
error_details = stderr or "Unknown sandbox error"
outputs.append(
@@ -441,12 +441,16 @@ def _build_execution_contents(
raw_representation=result,
)
)
return [Content.from_code_interpreter_tool_result(outputs=outputs, raw_representation=result)]
return outputs
def _make_sandbox_callback(tool_obj: FunctionTool) -> Callable[..., Any]:
sandbox_tool = copy.copy(tool_obj)
sandbox_tool.result_parser = _passthrough_result_parser
# Auto-assign a passthrough parser so the raw return value round-trips through
# `ast.literal_eval` in the sandbox callback below. User-supplied parsers are
# left in place so callers can customize how results are exposed to the guest.
if sandbox_tool.result_parser is None:
sandbox_tool.result_parser = _passthrough_result_parser
def _callback(**kwargs: Any) -> Any:
async def _invoke() -> list[Content]:
@@ -765,6 +769,7 @@ class HyperlightExecuteCodeTool(FunctionTool):
return build_codeact_instructions(
tools=config.tools,
tools_visible_to_model=tools_visible_to_model,
filesystem_enabled=config.filesystem_enabled,
)
def create_run_tool(self) -> HyperlightExecuteCodeTool:
@@ -68,6 +68,7 @@ def build_codeact_instructions(
*,
tools: Sequence[FunctionTool],
tools_visible_to_model: bool,
filesystem_enabled: bool = False,
) -> str:
"""Build dynamic CodeAct instructions for the effective sandbox state."""
usage_note = (
@@ -77,12 +78,24 @@ def build_codeact_instructions(
else "Provider-owned sandbox tools are not exposed separately; use `execute_code` when you need them."
)
output_note = (
"To surface results from `execute_code`, end the code with `print(...)`; the sandbox does not "
"return the value of the last expression."
)
if filesystem_enabled:
output_note += (
" For larger artifacts, write them to `/output/<filename>` instead — returned files will be "
"attached to the tool result."
)
return f"""You have one primary tool: execute_code.
Prefer one execute_code call per request when possible.
Its tool description contains the current `call_tool(...)` guidance, sandbox
tool registry, and capability limits.
{output_note}
{usage_note}
"""