{"record":{"id":"e47ead2bad9e3fb2","repo":"moodle/moodle","slug":"stream-context-must-be-an-array","errorCode":null,"errorMessage":"stream_context must be an array","messagePattern":"stream_context must be an array","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"public/lib/guzzlehttp/guzzle/src/Handler/StreamHandler.php","lineNumber":317,"sourceCode":"        $params = [];\n        $context = $this->getDefaultContext($request);\n\n        if (isset($options['on_headers']) && !\\is_callable($options['on_headers'])) {\n            throw new \\InvalidArgumentException('on_headers must be callable');\n        }\n\n        if (!empty($options)) {\n            foreach ($options as $key => $value) {\n                $method = \"add_{$key}\";\n                if (isset($methods[$method])) {\n                    $this->{$method}($request, $context, $value, $params);\n                }\n            }\n        }\n\n        if (isset($options['stream_context'])) {\n            if (!\\is_array($options['stream_context'])) {\n                throw new \\InvalidArgumentException('stream_context must be an array');\n            }\n            $context = \\array_replace_recursive($context, $options['stream_context']);\n        }\n\n        // Microsoft NTLM authentication only supported with curl handler\n        if (isset($options['auth'][2]) && 'ntlm' === $options['auth'][2]) {\n            throw new \\InvalidArgumentException('Microsoft NTLM authentication only supported with curl handler');\n        }\n\n        $uri = $this->resolveHost($request, $options);\n\n        $contextResource = $this->createResource(\n            static function () use ($context, $params) {\n                return \\stream_context_create($context, $params);\n            }\n        );\n\n        return $this->createResource(","sourceCodeStart":299,"sourceCodeEnd":335,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/lib/guzzlehttp/guzzle/src/Handler/StreamHandler.php#L299-L335","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\n$client->request('GET', '/u', [\n    'stream_context' => stream_context_create(['ssl' => ['verify_peer' => true]]), // resource: wrong type\n]);\n// after\n$client->request('GET', '/u', [\n    'stream_context' => ['ssl' => ['verify_peer' => true]], // plain array\n]);","handlingStrategy":"type-guard","validationCode":"if (isset($opts['stream_context']) && !is_array($opts['stream_context'])) {\n    throw new \\InvalidArgumentException('stream_context must be an array');\n}","typeGuard":"function isStreamContextArray($v): bool { return $v === null || is_array($v); }","tryCatchPattern":"try { $r = $client->request($m, $u, $o); } catch (\\InvalidArgumentException $e) { if (str_contains($e->getMessage(), 'stream_context')) { /* fix/strip option */ } throw $e; }","preventionTips":["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."],"tags":["guzzle","php","configuration","stream-context"],"backgroundTag":"invalid-argument-value","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}