moodle/moodle · error · InvalidArgumentException
stream_context must be an array
Error message
stream_context must be an array
What it means
Guzzle's StreamHandler (the fallback HTTP handler using PHP stream functions) builds a stream context for each request. The 'stream_context' request option, if set, must be an array of context options/params that gets merged into the default context via array_replace_recursive. Passing anything else (string resource, callable result, null-like value from config) makes the handler throw InvalidArgumentException because stream_context_create cannot consume it.
Solutions
- Pass a plain PHP array of context options, e.g. ['http' => ['protocol_version' => 1.1], 'ssl' => ['verify_peer' => true]], not a resource created by stream_context_create().
- If you already have a context resource, extract the options array instead (or refactor to use the array directly with Guzzle).
- Validate the option value with is_array($options['stream_context']) before passing it to the request/client.
- Ensure middleware or an options-normalizer isn't overwriting the array with a string.
- Consider switching to the CurlHandler/CurlMultiHandler, which consumes curl options rather than stream contexts.
Example fix
// before
$client->request('GET', '/u', [
'stream_context' => stream_context_create(['ssl' => ['verify_peer' => true]]), // resource: wrong type
]);
// after
$client->request('GET', '/u', [
'stream_context' => ['ssl' => ['verify_peer' => true]], // plain array
]); Defensive patterns
Strategy: type-guard
Validate before calling
if (isset($opts['stream_context']) && !is_array($opts['stream_context'])) {
throw new \InvalidArgumentException('stream_context must be an array');
} Type guard
function isStreamContextArray($v): bool { return $v === null || is_array($v); } Try / catch
try { $r = $client->request($m, $u, $o); } catch (\InvalidArgumentException $e) { if (str_contains($e->getMessage(), 'stream_context')) { /* fix/strip option */ } throw $e; } Prevention
- Never pass a stream context resource; pass the options array instead.
- Validate client option arrays at boot, not per request.
- Centralize option construction in one factory function.
- Add a static assertion that all 'stream_context' values are arrays.
When it happens
Trigger: Passing the 'stream_context' request option with a non-array value, e.g. 'stream_context' => stream_context_create([...]) (a resource, not an array), a JSON string, or a misconfigured option via a client config array.
Common situations: Copying code that created a context resource with stream_context_create() and then passing that resource into the request options; framework integrations that serialize user config into client options; typos where a nested array collapses to a string.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- GuzzleHttp requires cURL, the allow_url_fopen ini setting…
- Invalid crypto_method request option: unknown version…
- Invalid verify request option
- No system CA bundle could be found in any of the the common…
- SSL CA bundle not found: $value
AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14).
Data as JSON: /api/errors/e47ead2bad9e3fb2.
Report an issue: GitHub.
Appendix: source
Thrown at public/lib/guzzlehttp/guzzle/src/Handler/StreamHandler.php:317
$params = [];
$context = $this->getDefaultContext($request);
if (isset($options['on_headers']) && !\is_callable($options['on_headers'])) {
throw new \InvalidArgumentException('on_headers must be callable');
}
if (!empty($options)) {
foreach ($options as $key => $value) {
$method = "add_{$key}";
if (isset($methods[$method])) {
$this->{$method}($request, $context, $value, $params);
}
}
}
if (isset($options['stream_context'])) {
if (!\is_array($options['stream_context'])) {
throw new \InvalidArgumentException('stream_context must be an array');
}
$context = \array_replace_recursive($context, $options['stream_context']);
}
// Microsoft NTLM authentication only supported with curl handler
if (isset($options['auth'][2]) && 'ntlm' === $options['auth'][2]) {
throw new \InvalidArgumentException('Microsoft NTLM authentication only supported with curl handler');
}
$uri = $this->resolveHost($request, $options);
$contextResource = $this->createResource(
static function () use ($context, $params) {
return \stream_context_create($context, $params);
}
);
return $this->createResource(View on GitHub (pinned to 48ea8c3322)