Skip to content

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.

fix(cli): keep runtime input prompts on stderr (#8297)

Akaliup37f0bd32ce80
diff --git a/lib/cli/src/crewai_cli/input_prompt.py b/lib/cli/src/crewai_cli/input_prompt.py
index a91fdee7e..49c4831ff 100644
--- a/lib/cli/src/crewai_cli/input_prompt.py
+++ b/lib/cli/src/crewai_cli/input_prompt.py
@@ -11,6 +11,7 @@ stdout carries only the run's result.
from __future__ import annotations
from collections.abc import Callable, Iterable
+from contextlib import redirect_stdout
import difflib
import json
import sys
@@ -76,9 +77,12 @@ def prompt_for_inputs(
for name in names:
if describe is not None and (hint := describe(name)):
click.secho(f" {hint}", dim=True, err=True)
- raw = click.prompt(
- click.style(f" {name}", fg="cyan"),
- prompt_suffix=click.style(" > ", fg="bright_white"),
- )
+ # Click's Windows readline path writes the last prompt character to stdout.
+ with redirect_stdout(sys.stderr):
+ raw = click.prompt(
+ click.style(f" {name}", fg="cyan"),
+ prompt_suffix=click.style(" > ", fg="bright_white"),
+ err=True,
+ )
collected[name] = coerce(name, raw) if coerce is not None else raw
return collected
diff --git a/lib/cli/tests/test_input_prompt.py b/lib/cli/tests/test_input_prompt.py
index 22bb334a9..2f2787c86 100644
--- a/lib/cli/tests/test_input_prompt.py
+++ b/lib/cli/tests/test_input_prompt.py
@@ -2,6 +2,11 @@
from __future__ import annotations
+import json
+import os
+import subprocess
+import sys
+
import pytest
from crewai_cli.input_prompt import (
@@ -36,7 +41,9 @@ def test_parse_inputs_json_rejects_non_object(capsys):
def test_closest_name_suggests_near_miss():
- assert closest_name("prospect_emai", ["prospect_email", "topic"]) == "prospect_email"
+ assert (
+ closest_name("prospect_emai", ["prospect_email", "topic"]) == "prospect_email"
+ )
def test_closest_name_returns_none_when_nothing_close():
@@ -80,3 +87,51 @@ def test_prompt_for_inputs_keeps_raw_string_without_coerce(monkeypatch):
)
assert result == {"topic": "AI"}
+
+
+ ("names", "input_text", "expected"),
+ [
+ (["topic"], "AI\n", {"topic": "AI"}),
+ (["count", "topic"], "42\n研究\n", {"count": 42, "topic": "研究"}),
+ (["topic"], "", None),
+ ],
+ ids=["single-input", "multiple-inputs", "abort"],
+)
+def test_prompt_for_inputs_keeps_prompt_chrome_on_stderr(
+ names: list[str], input_text: str, expected: dict[str, str | int] | None
+) -> None:
+ """Real Click prompts must leave stdout available for serialized results."""
+ script = (
+ "import json\n"
+ "import click\n"
+ "from crewai_cli.input_prompt import prompt_for_inputs\n"
+ f"names = {names!r}\n"
+ "try:\n"
+ " result = prompt_for_inputs(\n"
+ " names, title='Runtime inputs', subtitle='Required values',\n"
+ " describe=lambda name: f'Value for {name}',\n"
+ " coerce=lambda name, raw: int(raw) if name == 'count' else raw,\n"
+ " )\n"
+ "except click.Abort:\n"
+ " result = None\n"
+ "print(json.dumps(result, ensure_ascii=False))\n"
+ )
+ result = subprocess.run(
+ [sys.executable, "-c", script],
+ input=input_text,
+ capture_output=True,
+ text=True,
+ encoding="utf-8",
+ env={**os.environ, "PYTHONIOENCODING": "utf-8"},
+ check=False,
+ timeout=20,
+ )
+
+ assert result.returncode == 0, result.stderr
+ assert result.stdout == json.dumps(expected, ensure_ascii=False) + "\n"
+ assert "Runtime inputs" in result.stderr
+ assert "Required values" in result.stderr
+ for name in names:
+ assert f"Value for {name}" in result.stderr
+ assert f"{name} > " in result.stderr