cocoindex-io/cocoindex · error · AttributeError

module has no attribute

Error message

module {__name__!r} has no attribute {name!r}

What it means

The cocoindex.ops.code module defines module-level __getattr__ for backward-compatible aliases (e.g. CodeSource). Accessing any other attribute not defined in the module raises a plain AttributeError naming the missing attribute.

Solutions

  1. Use the current APIs: CodePattern.match_source / match_code, RecursiveSplitter.split, or index_terms.
  2. Import CodeSource for the deprecated code-source entry point (emits a DeprecationWarning).
  3. Check the module's actual exports with dir(cocoindex.ops.code) or the docs.

Example fix

// before
from cocoindex.ops.code import CodeSplitter
// after
from cocoindex.ops import RecursiveSplitter
splitter = RecursiveSplitter()
Defensive patterns

Strategy: try-catch

Validate before calling

import cocoindex.ops.code as c
assert hasattr(c, "SymbolToUse"), "symbol removed; check current API"

Try / catch

try:
    from cocoindex.ops.code import CodeSplitter
except (ImportError, AttributeError):
    from cocoindex.ops import RecursiveSplitter
    CodeSplitter = RecursiveSplitter

Prevention

When it happens

Trigger: from cocoindex.ops.code import SomeSymbol where SomeSymbol was removed/renamed (e.g. an old splitter or pattern API that moved to CodePattern.match_source / RecursiveSplitter.split).

Common situations: Upgrading cocoindex after the v1 redesign; following outdated docs/examples that reference removed symbols; IDE auto-imports picking the wrong module.

Understand the failure class

Background: "is deprecated and will be removed" — deprecation warnings for old API names, keywords, and options, and how to migrate before the removal release — this error's family across 29 libraries.

Related errors


AI-assisted analysis of cocoindex-io/cocoindex@e84aa99b32 (2026-09-08). Data as JSON: /api/errors/aaaa22bad82b66df. Report an issue: GitHub.

Appendix: source

Thrown at python/cocoindex/ops/code.py:338

def __getattr__(name: str) -> _typing.Any:
    # Deprecated alias for the removed ``CodeAst`` class, so annotations and
    # ``isinstance`` checks in older callers keep working. ``CodeSource`` is
    # the single handle now; note its constructor is lazy and tolerant where
    # ``CodeAst``'s was eager and raising, and the old ``.matches`` / ``.split``
    # / ``.index_terms`` methods live on the consumers instead
    # (``CodePattern.match_source`` / ``match_code``, ``RecursiveSplitter.split``,
    # ``index_terms``).
    if name == "CodeAst":
        _warnings.warn(
            "CodeAst is deprecated and now aliases CodeSource; use CodeSource "
            "with CodePattern.match_source / match_code, RecursiveSplitter.split, "
            "and index_terms",
            DeprecationWarning,
            stacklevel=2,
        )
        return CodeSource
    raise AttributeError(f"module {__name__!r} has no attribute {name!r}")

View on GitHub (pinned to e84aa99b32)