Skip to content

Commit 754d73a

Browse files
whheclaude
andcommitted
fix(examples): improve gemini.py CLI experience
- Support --acp flag with --experimental-acp fallback for older versions - Suppress Gemini CLI stderr noise in non-debug mode - Drain stdin before terminating subprocess for cleaner shutdown - Catch KeyboardInterrupt in main() to avoid noisy traceback Co-Authored-By: Claude Sonnet 4 <noreply@anthropic.com>
1 parent e9c6e9f commit 754d73a

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

docs/quickstart.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Spin up a working ACP agent/client loop in minutes. Keep this page beside the te
1616

1717
- Python 3.10–3.14 with `pip` or `uv`
1818
- An ACP-capable client such as Zed (recommended for validation)
19-
- Optional: the Gemini CLI (`gemini --experimental-acp`) for the bridge example
19+
- Optional: the Gemini CLI (`gemini --acp`; use `--experimental-acp` for older versions) for the bridge example
2020

2121
## Step 1 — Install the SDK
2222

@@ -145,7 +145,7 @@ Run it with `run_agent()` inside an async entrypoint and wire it to your client.
145145
- [`examples/echo_agent.py`](https://github.com/agentclientprotocol/python-sdk/blob/main/examples/echo_agent.py) for the smallest streaming agent
146146
- [`examples/agent.py`](https://github.com/agentclientprotocol/python-sdk/blob/main/examples/agent.py) for an implementation that negotiates capabilities and streams richer updates
147147
- [`examples/duet.py`](https://github.com/agentclientprotocol/python-sdk/blob/main/examples/duet.py) to see `spawn_agent_process` in action alongside the interactive client
148-
- [`examples/gemini.py`](https://github.com/agentclientprotocol/python-sdk/blob/main/examples/gemini.py) to drive the Gemini CLI (`--acp`) directly from Python
148+
- [`examples/gemini.py`](https://github.com/agentclientprotocol/python-sdk/blob/main/examples/gemini.py) to drive the Gemini CLI (`--acp`; use `--experimental-acp` for older versions) directly from Python
149149

150150
Need builders for common payloads? `acp.helpers` mirrors the Go/TS helper APIs:
151151

examples/gemini.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ async def run(argv: list[str]) -> int: # noqa: C901
328328
*cmd,
329329
stdin=asyncio.subprocess.PIPE,
330330
stdout=asyncio.subprocess.PIPE,
331-
stderr=None,
331+
# Suppress Gemini CLI diagnostic noise unless debugging
332+
stderr=None if args.debug else asyncio.subprocess.DEVNULL,
332333
)
333334
except FileNotFoundError as exc:
334335
print(f"Failed to start Gemini CLI: {exc}", file=sys.stderr)
@@ -412,6 +413,10 @@ def _print_request_error(stage: str, err: RequestError) -> None:
412413
async def _shutdown(proc: asyncio.subprocess.Process, conn: ClientSideConnection) -> None:
413414
with contextlib.suppress(Exception):
414415
await asyncio.wait_for(conn.close(), timeout=2)
416+
if proc.stdin is not None:
417+
with contextlib.suppress(Exception):
418+
proc.stdin.close()
419+
await asyncio.wait_for(proc.stdin.wait_closed(), timeout=2)
415420
if proc.returncode is None:
416421
proc.terminate()
417422
try:
@@ -424,7 +429,10 @@ async def _shutdown(proc: asyncio.subprocess.Process, conn: ClientSideConnection
424429

425430
def main(argv: list[str] | None = None) -> int:
426431
args = sys.argv if argv is None else argv
427-
return asyncio.run(run(list(args)))
432+
try:
433+
return asyncio.run(run(list(args)))
434+
except KeyboardInterrupt:
435+
return 1
428436

429437

430438
if __name__ == "__main__":

0 commit comments

Comments
 (0)