crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

[BUG] Runtime input prompts contaminate stdout reserved for run results

Open Akaliup opened this issue 1 month ago • 0 comments

Description

The shared crewai_cli.input_prompt.prompt_for_inputs helper documents that prompt chrome goes to stderr, leaving stdout for the run result. Its title, subtitle and description use err=True, but its click.prompt call does not. The field prompt and ANSI suffix appear on stdout before the result, so a consumer cannot parse the serialized output even when the process succeeds.

This was found by source inspection and reproduced offline on main@c6e500ab3730ccf3badb640c6e0170e5c6aa3882; it is not a reported production incident. JSON crew and declarative-flow input resolution both use this helper. This report concerns that helper's I/O contract, not a claim that every output from the complete crewai run command is JSON.

Steps to Reproduce

Run against an installed checkout:

import json
import subprocess
import sys

script = (
    "import json; from crewai_cli.input_prompt import prompt_for_inputs; "
    "print(json.dumps(prompt_for_inputs(['topic'], "
    "title='Crew inputs', subtitle='Required values')))"
)
result = subprocess.run(
    [sys.executable, "-c", script],
    input="AI\n",
    capture_output=True,
    text=True,
    check=False,
)
print(result.returncode)
print(repr(result.stdout))
print(repr(result.stderr))
json.loads(result.stdout)

No model, API key, network service or project is required.

Expected behavior

The return value remains {"topic": "AI"}. All prompt text is written to stderr; the wrapper's stdout is exactly {"topic": "AI"}\n and remains usable after normal input or an input abort.

Screenshots/Code snippets

Observed on Windows with Click 8.4.2:

exit: 0
stdout: '  topic > \x1b[0m{"topic": "AI"}\n'
stderr: '\n  Crew inputs\n  Required values\n'
JSON consumer rejects stdout: Expecting value: line 1 column 3 (char 2)

Operating System

Native Windows; the exact Windows product edition is not claimed.

Python Version

3.12.8

crewAI Version

crewai-cli 1.15.28, editable current-main checkout at the SHA above; Click 8.4.2.

crewAI Tools Version

Not involved in this reproduction.

Virtual Environment

uv venv using current lockfile dependencies, synchronized with uv sync --locked --package crewai --group dev --python <Python-3.12.8>.

Evidence

Three real-subprocess regressions for one field, multiple fields with coercion/non-ASCII input, and EOF abort yield 3 failed, 8 passed on the unchanged production file. Existing tests replace click.prompt and therefore do not observe its real stream behavior.

Possible Solution

Route the Click prompt to stderr. On Windows, err=True alone is insufficient with the tested Click version: _readline_prompt writes the last prompt character through input() on stdout. A short redirect_stdout(sys.stderr) context around only the click.prompt call handles that path and restores the caller's stdout on return or exception. Coercion and subsequent result output remain outside the context. This is a process-wide redirection scoped to synchronous input collection, not a thread-local logging mechanism.

Additional context

All-state PR and Issue searches for prompt_for_inputs/input_prompt and stderr/stdout did not identify a matching fix when prepared. Merged PR #6466 introduced the current shared helper; its implementation also lacks the stderr routing on the field prompt.

AI assistance: Codex checked related work, reproduced the behavior, prepared a focused patch and tests, and ran local checks. Please apply the required llm-generated label; this external account cannot add repository labels.

Akaliup avatar Oct 20 '26 00:10 Akaliup