Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: ForwardRef._evaluate() missing 1 required keyword-only argument: 'recursive_guard'

Pls help. I need help setting up spacy inside jupyter enviornment

I am trying to use spacy to summarize youtube transcripts but finding lots of problems with spacy and python 3.12.4/3.12.3.
I started with 3.12.4, then installed python 3.12.3 onto conda environment py3.12.3.

I install spacy 3.7.6 into env with pip install stacy

Here are the `conda env list: 
base                     /opt/anaconda3
py3.11                *  /opt/anaconda3/envs/py3.11
py3.12.3                 /opt/anaconda3/envs/py3.12.3

I added python kernels via cmd below

jupyter kernelspec install py3.12.3
python -m ipykernel install --user --name=py3.12.3

On command line, import spacy is ok:

(py3.12.3) UID ~ % python           
Python 3.12.3 | packaged by Anaconda, Inc. | (main, May  6 2024, 14:43:12) [Clang 14.0.6 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import spacy
>>> quit()
(py3.12.3) UID ~ % pip freeze | grep spacy
spacy==3.7.6
spacy-legacy==3.0.12
spacy-loggers==1.0.5

Inside jupyter (run from base, restarted after installing py3.12.3), import spacy gives error below. Any help is welcomed???

1. (base) UID ~ % python -m ipykernel install --user --name=py3.12.3
0.00s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
Installed kernelspec py3.12.3 in /Users/UID/Library/Jupyter/kernels/py3.12.3


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[2], line 1
----> 1 import spacy # module will be used  to build NLP model
      2 from spacy.lang.en.stop_words import STOP_WORDS # module will be used  to build NLP model
      3 from string import punctuation

File /opt/anaconda3/lib/python3.12/site-packages/spacy/__init__.py:13
     10 # These are imported as part of the API
     11 from thinc.api import Config, prefer_gpu, require_cpu, require_gpu  # noqa: F401
---> 13 from . import pipeline  # noqa: F401
     14 from . import util
     15 from .about import __version__  # noqa: F401

File /opt/anaconda3/lib/python3.12/site-packages/spacy/pipeline/__init__.py:1
----> 1 from .attributeruler import AttributeRuler
      2 from .dep_parser import DependencyParser
      3 from .edit_tree_lemmatizer import EditTreeLemmatizer

File /opt/anaconda3/lib/python3.12/site-packages/spacy/pipeline/attributeruler.py:8
      6 from .. import util
      7 from ..errors import Errors
----> 8 from ..language import Language
      9 from ..matcher import Matcher
     10 from ..scorer import Scorer

File /opt/anaconda3/lib/python3.12/site-packages/spacy/language.py:43
     41 from .lang.tokenizer_exceptions import BASE_EXCEPTIONS, URL_MATCH
     42 from .lookups import load_lookups
---> 43 from .pipe_analysis import analyze_pipes, print_pipe_analysis, validate_attrs
     44 from .schemas import (
     45     ConfigSchema,
     46     ConfigSchemaInit,
   (...)
     49     validate_init_settings,
     50 )
     51 from .scorer import Scorer

File /opt/anaconda3/lib/python3.12/site-packages/spacy/pipe_analysis.py:6
      3 from wasabi import msg
      5 from .errors import Errors
----> 6 from .tokens import Doc, Span, Token
      7 from .util import dot_to_dict
      9 if TYPE_CHECKING:
     10     # This lets us add type hints for mypy etc. without causing circular imports

File /opt/anaconda3/lib/python3.12/site-packages/spacy/tokens/__init__.py:1
----> 1 from ._serialize import DocBin
      2 from .doc import Doc
      3 from .morphanalysis import MorphAnalysis

File /opt/anaconda3/lib/python3.12/site-packages/spacy/tokens/_serialize.py:14
     12 from ..errors import Errors
     13 from ..util import SimpleFrozenList, ensure_path
---> 14 from ..vocab import Vocab
     15 from ._dict_proxies import SpanGroups
     16 from .doc import DOCBIN_ALL_ATTRS as ALL_ATTRS

File /opt/anaconda3/lib/python3.12/site-packages/spacy/vocab.pyx:1, in init spacy.vocab()

File /opt/anaconda3/lib/python3.12/site-packages/spacy/tokens/doc.pyx:49, in init spacy.tokens.doc()

File /opt/anaconda3/lib/python3.12/site-packages/spacy/schemas.py:195
    191         obj = converted
    192     return validate(TokenPatternSchema, {"pattern": obj})
--> 195 class TokenPatternString(BaseModel):
    196     REGEX: Optional[Union[StrictStr, "TokenPatternString"]] = Field(None, alias="regex")
    197     IN: Optional[List[StrictStr]] = Field(None, alias="in")

File /opt/anaconda3/lib/python3.12/site-packages/pydantic/v1/main.py:286, in ModelMetaclass.__new__(mcs, name, bases, namespace, **kwargs)
    284 cls.__signature__ = ClassAttribute('__signature__', generate_model_signature(cls.__init__, fields, config))
    285 if resolve_forward_refs:
--> 286     cls.__try_update_forward_refs__()
    288 # preserve `__set_name__` protocol defined in https://peps.python.org/pep-0487
    289 # for attributes not in `new_namespace` (e.g. private attributes)
    290 for name, obj in namespace.items():

File /opt/anaconda3/lib/python3.12/site-packages/pydantic/v1/main.py:808, in BaseModel.__try_update_forward_refs__(cls, **localns)
    802 @classmethod
    803 def __try_update_forward_refs__(cls, **localns: Any) -> None:
    804     """
    805     Same as update_forward_refs but will not raise exception
    806     when forward references are not defined.
    807     """
--> 808     update_model_forward_refs(cls, cls.__fields__.values(), cls.__config__.json_encoders, localns, (NameError,))

File /opt/anaconda3/lib/python3.12/site-packages/pydantic/v1/typing.py:554, in update_model_forward_refs(model, fields, json_encoders, localns, exc_to_suppress)
    552 for f in fields:
    553     try:
--> 554         update_field_forward_refs(f, globalns=globalns, localns=localns)
    555     except exc_to_suppress:
    556         pass

File /opt/anaconda3/lib/python3.12/site-packages/pydantic/v1/typing.py:529, in update_field_forward_refs(field, globalns, localns)
    527 if field.sub_fields:
    528     for sub_f in field.sub_fields:
--> 529         update_field_forward_refs(sub_f, globalns=globalns, localns=localns)
    531 if field.discriminator_key is not None:
    532     field.prepare_discriminated_union_sub_fields()

File /opt/anaconda3/lib/python3.12/site-packages/pydantic/v1/typing.py:520, in update_field_forward_refs(field, globalns, localns)
    518 if field.type_.__class__ == ForwardRef:
    519     prepare = True
--> 520     field.type_ = evaluate_forwardref(field.type_, globalns, localns or None)
    521 if field.outer_type_.__class__ == ForwardRef:
    522     prepare = True

File /opt/anaconda3/lib/python3.12/site-packages/pydantic/v1/typing.py:66, in evaluate_forwardref(type_, globalns, localns)
     63 def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Any:
     64     # Even though it is the right signature for python 3.9, mypy complains with
     65     # `error: Too many arguments for "_evaluate" of "ForwardRef"` hence the cast...
---> 66     return cast(Any, type_)._evaluate(globalns, localns, set())

TypeError: ForwardRef._evaluate() missing 1 required keyword-only argument: 'recursive_guard'
like image 638
frankr6591 Avatar asked Aug 31 '25 23:08

frankr6591


1 Answers

Temporary solution is to downgrade python to version 3.12.3

linked gits:

https://github.com/pydantic/pydantic/issues/9609#issuecomment-2155832461

https://github.com/pydantic/pydantic/issues/9637#issuecomment-2162571909


Another solution is to:

Update your pydantic version from to v2.7.4, so even if you have Python 3.12.4, it will work:

pip install pydantic==2.7.4
like image 95
Talha Tayyab Avatar answered Sep 03 '25 13:09

Talha Tayyab