symfony/http-foundation · error · InvalidArgumentException

Expected a scalar value as a 2nd argument to

Error message

Expected a scalar value as a 2nd argument to "%s()", "%s" given.

What it means

InputBag::get() validates that the $default (2nd argument) is a scalar or Stringable, throwing InvalidArgumentException otherwise. Because get() is typed to return string|int|float|bool|null, a non-scalar default (array, object, null resource) would violate the return contract, so Symfony rejects it up front with a message naming the method and the offending type via get_debug_type().

Solutions

  1. Use a scalar or null default, e.g. ->get('key', 'default')
  2. If you need a complex default, fetch then check: $v = $bag->get('key'); $v ??= $complexDefault;
  3. For array input use $request->request->all('key') or InputBag::all() instead of get() with an array default
  4. If you need Stringable support, pass an object implementing Stringable (allowed) rather than a raw object

Example fix

// before
$page = $request->request->get('filters', ['status' => 'active']); // throws
// after
$filters = $request->request->all('filters') ?: ['status' => 'active'];
Defensive patterns

Strategy: type-guard

Validate before calling

$default = $default ?? null;
if (null !== $default && !is_scalar($default) && !$default instanceof Stringable) {
    $default = null; // drop non-scalar default before calling get()
}

Type guard

function isScalarOrDefault(mixed $d): bool {
    return $d === null || is_scalar($d) || $d instanceof \Stringable;
}

Try / catch

try {
    $value = $request->request->get($key, $default);
} catch (\InvalidArgumentException $e) {
    $value = $request->request->get($key);
    $value = $value ?? $defaultFallback;
}

Prevention

When it happens

Trigger: Calling $request->request->get('key', ['a','b']) or ->get('key', new \DateTime()) — i.e. any array/object default passed as the 2nd argument to InputBag::get(). Note server->get()/query->get() on ParameterBag don't have this restriction; only InputBag (request, json input) does.

Common situations: Migrating code from ParameterBag to InputBag after Symfony 5.1+ where defaults that were arrays/objects previously worked; using a config array as a default for a request input value; copy-pasted get() calls with object defaults.

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/http-foundation@5aea19cd67 (2026-09-13). Data as JSON: /api/errors/c9a367193984ef37. Report an issue: GitHub.

Appendix: source

Thrown at InputBag.php:40

 * @author Saif Eddin Gmati <azjezz@protonmail.com>
 */
final class InputBag extends ParameterBag
{
    /**
     * Returns a scalar input value by name.
     *
     * @template TDefault of string|int|float|bool|null
     *
     * @param TDefault $default The default value if the input key does not exist
     *
     * @return TDefault|TInput
     *
     * @throws BadRequestException if the input contains a non-scalar value
     */
    public function get(string $key, mixed $default = null): string|int|float|bool|null
    {
        if (null !== $default && !\is_scalar($default) && !$default instanceof \Stringable) {
            throw new \InvalidArgumentException(\sprintf('Expected a scalar value as a 2nd argument to "%s()", "%s" given.', __METHOD__, get_debug_type($default)));
        }

        $value = parent::get($key, $this);

        if (null !== $value && $this !== $value && !\is_scalar($value) && !$value instanceof \Stringable) {
            throw new BadRequestException(\sprintf('Input value "%s" contains a non-scalar value.', $key));
        }

        return $this === $value ? $default : $value;
    }

    /**
     * Replaces the current input values by a new set.
     */
    public function replace(array $inputs = []): void
    {
        $this->parameters = [];
        $this->add($inputs);

View on GitHub (pinned to 5aea19cd67)