{"record":{"id":"839f99eab84b79f2","repo":"JuliusBrussee/caveman","slug":"use-middlewareruntime-with-the-synchronous-transport","errorCode":null,"errorMessage":"Use MiddlewareRuntime with the synchronous transport","messagePattern":"Use MiddlewareRuntime with the synchronous transport","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/middleware/python/caveman_middleware/google.py","lineNumber":426,"sourceCode":"            self.finish(\"completed\")\n        except BaseException:\n            self.finish(\"cancelled\")\n            raise\n        finally:\n            await self.aclose()\n\n    async def aclose(self):\n        try:\n            await self.stream.aclose()\n        finally:\n            self.finish(\"cancelled\")\n\n\nclass CavemanGoogleTransport(httpx.BaseTransport):\n    \"\"\"Public HTTPX transport; original-content request hooks run before it.\"\"\"\n    def __init__(self, *, runtime, scope, provider_base_url=\"https://generativelanguage.googleapis.com\", transport=None):\n        if not isinstance(runtime, MiddlewareRuntime):\n            raise TypeError(\"Use MiddlewareRuntime with the synchronous transport\")\n        self.runtime, self.scope, self.transport = runtime, scope, transport or httpx.HTTPTransport()\n        self.base = urlsplit(provider_base_url)\n\n    def handle_request(self, request):\n        url = urlsplit(str(request.url))\n        if (url.scheme, url.netloc) != (self.base.scheme, self.base.netloc) or not url.path.startswith(self.base.path.rstrip(\"/\") + \"/\"):\n            return self.transport.handle_request(request)\n        state = _prepare(request, self.runtime, self.scope)\n        if state is None:\n            return self.transport.handle_request(request)\n        attempt, plan = state\n        outgoing = _apply(request, state, self.runtime.optimize(**plan[2]) if plan else None)\n        attempt.observe(\"dispatch_intent\")\n        token = owner.set(attempt)\n        try:\n            response = self.transport.handle_request(outgoing)\n            if response.status_code >= 400:\n                attempt.observe(\"failed\")","sourceCodeStart":408,"sourceCodeEnd":444,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/middleware/python/caveman_middleware/google.py#L408-L444","documentation":"CavemanGoogleTransport is the synchronous HTTPX transport and only accepts a MiddlewareRuntime (sync) instance. Passing anything else — including AsyncMiddlewareRuntime, None, or a foreign object — triggers this TypeError at construction. It enforces that sync transports never run an async runtime's bounded execution path.","triggerScenarios":"Calling CavemanGoogleTransport(runtime=<not a MiddlewareRuntime>, scope=...) — e.g. passing an AsyncMiddlewareRuntime, a raw dict/config object, or forgetting the keyword and passing scope positionally into runtime.","commonSituations":"Copy-pasting the async transport setup into sync code; swapping transports after refactoring to async; constructing the transport from an unparsed config value that is not a runtime instance.","solutions":["Instantiate MiddlewareRuntime (not AsyncMiddlewareRuntime) and pass it as runtime=.","If you actually need async, use CavemanGoogleAsyncTransport instead.","Check for swapped/positional arguments so scope is not landing in runtime."],"exampleFix":"// before\ntransport = CavemanGoogleTransport(runtime=runtime.as_async(), scope=scope)\n// after\ntransport = CavemanGoogleTransport(runtime=MiddlewareRuntime(...), scope=scope)","handlingStrategy":"type-guard","validationCode":"if not isinstance(runtime, MiddlewareRuntime):\n    raise ValueError('CavemanGoogleTransport needs a sync MiddlewareRuntime')\ntransport = CavemanGoogleTransport(runtime=runtime, scope=scope)","typeGuard":"def is_sync_runtime(r): return isinstance(r, MiddlewareRuntime)","tryCatchPattern":"try:\n    transport = CavemanGoogleTransport(runtime=runtime, scope=scope)\nexcept TypeError:\n    transport = CavemanGoogleTransport(runtime=runtime.as_sync() if hasattr(runtime, 'as_sync') else MiddlewareRuntime(...), scope=scope)","preventionTips":["Keep sync and async runtime construction in separate, clearly named factory functions.","Type-annotate runtime parameters as MiddlewareRuntime so static checkers flag misuse.","Never pass runtimes positionally; always use runtime= keyword."],"tags":["type-mismatch","httpx","python","constructor"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}