Skip to content

opensemantic.base import fails on pydantic <2.12 (missing codegen imports: Keyword, ObjectStatement, DataStatement, QuantityStatement) #131

Description

@simontaurus

Summary

import osw.model.entity (and thus osw.express) fails on pydantic 2.11 with:

pydantic.errors.PydanticUndefinedAnnotation: name 'Keyword' is not defined

Currently masked by requiring pydantic>=2.12.0 (commit pins it), but the underlying defect is in the generated opensemantic/base/_model.py and could resurface if pydantic changes forward-ref resolution again.

Root cause

opensemantic/base/_model.py is datamodel-codegen output that:

  1. uses from __future__ import annotations (all annotations are string forward-refs);
  2. imports its base classes from opensemantic.core (Entity, Item, Place, DefinedTerm, ...) but not the field-value types those base classes reference;
  3. ends with a block of X.model_rebuild() calls.

Every base model subclasses core.Entity, whose fields reference core types not imported into base/_model.py:

  • keywords: list[Keyword]
  • statement fields referencing ObjectStatement, DataStatement, QuantityStatement

Under pydantic 2.11, model_rebuild() resolves inherited fields' forward refs against the subclass module globals (opensemantic.base._model), where those names are absent -> NameError -> PydanticUndefinedAnnotation. Pydantic 2.12 resolves them against the defining class's module (core) again, so the import succeeds.

Reproduction (verified)

Same packages (opensemantic==0.2.1, opensemantic.base==0.42.7..., opensemantic.core==0.56.0...), only pydantic changed:

pydantic import opensemantic.base
2.11.3 FAIL: PydanticUndefinedAnnotation: name 'Keyword' is not defined
2.12.0 OK

Durable fix (in opensemantic-python codegen)

The generator must import every core type referenced by inherited field annotations, not only the direct base classes. Minimal verified fix is adding the four names to the from opensemantic.core import (...) block in base/_model.py:

Keyword, ObjectStatement, DataStatement, QuantityStatement

Same latent bug affects any generated module that subclasses core.Entity across a module boundary (other base/* namespaces, base/v1/_model.py).

Options (cleanest first):

  1. Emit explicit imports for inherited field-value types.
  2. from opensemantic.core import * before the model_rebuild() block (needs core.__all__).
  3. Pass explicit namespace: X.model_rebuild(_types_namespace={**vars(opensemantic.core), **globals()}).

Note

The real fix belongs in opensemantic-python (generated code). This issue tracks it from the osw-python side since that is where it surfaced. The pydantic>=2.12.0 pin is the current stopgap.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions