{"record":{"id":"b6c2a3bcf38ac27e","repo":"JuliusBrussee/caveman","slug":"expected-an-existing-native-llamaindex-llm","errorCode":null,"errorMessage":"Expected an existing native LlamaIndex LLM","messagePattern":"Expected an existing native LlamaIndex LLM","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/middleware/python/caveman_middleware/llama_index.py","lineNumber":352,"sourceCode":"            return await self.iterator.aclose()\n\n\nclass CavemanLLM(FunctionCallingLLM):\n    \"\"\"Delegate to an existing native LLM. Model-only calls are recovery-free.\n\n    Public provider methods retain native serialization, retries, callbacks,\n    response objects, and tool parsing. This class never runs a tool loop.\n    \"\"\"\n    wrapped: LLM = Field(exclude=True)\n    runtime: Any = Field(exclude=True)\n    scope: Any = Field(exclude=True)\n    protocol: str | None = Field(exclude=True)\n    passthrough_reason: str = Field(exclude=True)\n    registration: Any = Field(default=None, exclude=True)\n\n    def __init__(self, wrapped: LLM, *, runtime, scope, registration=None):\n        if not isinstance(wrapped, LLM):\n            raise TypeError(\"Expected an existing native LlamaIndex LLM\")\n        # Copy public prompt settings so inherited predict/structured helpers\n        # build exactly the same native input as the caller's LLM.\n        settings = {name: getattr(wrapped, name) for name in LLM.model_fields}\n        supported = _check_version(runtime)\n        protocol = _protocol(wrapped, runtime) if supported else None\n        known_provider = (type(wrapped).__module__, type(wrapped).__name__) in {\n            (\"llama_index.llms.openai.base\", \"OpenAI\"), (\"llama_index.llms.anthropic.base\", \"Anthropic\")}\n        super().__init__(wrapped=wrapped, runtime=runtime, scope=scope, protocol=protocol,\n                         passthrough_reason=\"unsupported_version\" if not supported or (known_provider and protocol is None) else \"unsupported_provider\",\n                         registration=registration, **settings)\n\n    @property\n    def metadata(self):\n        return self.wrapped.metadata\n\n    def _passive(self, reason):\n        if owner.get() is not None:\n            return None","sourceCodeStart":334,"sourceCodeEnd":370,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/middleware/python/caveman_middleware/llama_index.py#L334-L370","documentation":"Raised in CavemanLLM.__init__: the object passed as the model to wrap is not an existing native LlamaIndex LLM instance. CavemanLLM is a pure delegate that forwards public provider methods to a real native LLM, so it must be constructed with one.","triggerScenarios":"Calling the wrapper with a model identifier string instead of an instantiated LLM, with None, or with an object from a different/incompatible LlamaIndex installation.","commonSituations":"Migrating from other frameworks where you pass a model name, forgetting to instantiate the provider class, or having multiple llama-index packages where LLM classes diverge.","solutions":["Instantiate the provider LLM first, e.g. OpenAI(model=\"gpt-4o\"), and pass that object","Verify the object is an instance of llama_index.core.llms.LLM before wrapping","Ensure a single compatible llama-index-core installation is in the environment"],"exampleFix":"// before\nllm = caveman_llm(\"gpt-4o\", runtime=rt, scope=scope)\n// after\nfrom llama_index.llms.openai import OpenAI\nllm = caveman_llm(OpenAI(model=\"gpt-4o\"), runtime=rt, scope=scope)","handlingStrategy":"type-guard","validationCode":"from llama_index.core.llms import LLM\nif not isinstance(wrapped, LLM):\n    raise TypeError(\"pass an instantiated llama_index LLM, not a model name\")","typeGuard":"from llama_index.core.llms import LLM\ndef is_llama_llm(obj) -> bool:\n    return isinstance(obj, LLM)","tryCatchPattern":"try:\n    wrapped_llm = caveman_llm(provider_llm, runtime=rt, scope=scope)\nexcept TypeError as e:\n    log.error(\"wrap requires an LLM instance: %s\", e)\n    raise","preventionTips":["Instantiate provider LLMs (e.g. OpenAI(model=...)) before wrapping","Pin a single llama-index-core version in your environment","Type-check the wrapped object in setup code"],"tags":["python","type-error","llamaindex","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"}