symfony/process · error · InvalidArgumentException

" " only accepts strings, Traversable objects or stream…

Error message

"%s" only accepts strings, Traversable objects or stream resources.

What it means

ProcessUtils::validateInput() validates the $input argument supplied to Process::setInput(), setStdin (write), etc. Only strings, Traversable objects and stream resources are accepted; anything else (e.g. int, array, object without Traversable) throws InvalidArgumentException naming the caller method.

Solutions

  1. Cast the value to string before passing: setInput((string) $value)
  2. Convert arrays/objects to a string (json_encode, serialize, or iterate into a string) before use
  3. If you have a Traversable, pass it directly — it is wrapped in an IteratorIterator automatically
  4. If the value is a resource, keep it as an open stream resource rather than reading it into memory

Example fix

// before
$process->setInput(12345);
// after
$process->setInput((string) 12345);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!is_string($input) && !$input instanceof \Traversable && !is_resource($input)) { throw new \TypeError('Process input must be string, Traversable or resource'); }

Type guard

function isValidProcessInput(mixed $v): bool { return is_string($v) || $v instanceof \Traversable || is_resource($v); }

Try / catch

try { $process->setInput($input); } catch (\InvalidArgumentException $e) { $process->setInput((string) $input); }

Prevention

When it happens

Trigger: Calling Process::setInput($value) or $process->write($value) (or a constructor path that resolves input) with an unsupported type such as an int, float, bool, array, or a plain non-Traversable object.

Common situations: Passing an integer/numeric value from user input, passing a stdClass or DTO object expecting it to be stringified, or passing an array of bytes instead of a string.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of symfony/process@99b85026db (2026-09-14). Data as JSON: /api/errors/35f42b357036fb1f. Report an issue: GitHub.

Appendix: source

Thrown at ProcessUtils.php:59

    {
        if (null !== $input) {
            if (\is_resource($input)) {
                return $input;
            }
            if (\is_scalar($input)) {
                return (string) $input;
            }
            if ($input instanceof Process) {
                return $input->getIterator($input::ITER_SKIP_ERR);
            }
            if ($input instanceof \Iterator) {
                return $input;
            }
            if ($input instanceof \Traversable) {
                return new \IteratorIterator($input);
            }

            throw new InvalidArgumentException(\sprintf('"%s" only accepts strings, Traversable objects or stream resources.', $caller));
        }

        return $input;
    }
}

View on GitHub (pinned to 99b85026db)