1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Posted at

目的

Lambd関数内で以下のlangchain関連のパッケージを使っていたのですが、ある日を境に表題のエラーが発生するようになりました。

from langchain_aws import ChatBedrock
from langchain_core.messages import HumanMessage
from langchain.prompts import PromptTemplate
エラー内容の全貌
[ERROR] TypeError: ForwardRef._evaluate() missing 1 required keyword-only argument: 'recursive_guard'
Traceback (most recent call last):
  File "/var/lang/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/var/task/lambda_function.py", line 12, in <module>
    from langchain_aws import ChatBedrock
  File "/opt/python/lib/python3.12/site-packages/langchain_aws/__init__.py", line 1, in <module>
    from langchain_aws.chat_models import BedrockChat, ChatBedrock
  File "/opt/python/lib/python3.12/site-packages/langchain_aws/chat_models/__init__.py", line 1, in <module>
    from langchain_aws.chat_models.bedrock import BedrockChat, ChatBedrock
  File "/opt/python/lib/python3.12/site-packages/langchain_aws/chat_models/bedrock.py", line 19, in <module>
    from langchain_core.callbacks import (
  File "/opt/python/lib/python3.12/site-packages/langchain_core/callbacks/__init__.py", line 22, in <module>
    from langchain_core.callbacks.manager import (
  File "/opt/python/lib/python3.12/site-packages/langchain_core/callbacks/manager.py", line 29, in <module>
    from langsmith.run_helpers import get_run_tree_context
  File "/opt/python/lib/python3.12/site-packages/langsmith/run_helpers.py", line 38, in <module>
    from langsmith import client as ls_client
  File "/opt/python/lib/python3.12/site-packages/langsmith/client.py", line 52, in <module>
    from langsmith import env as ls_env
  File "/opt/python/lib/python3.12/site-packages/langsmith/env/__init__.py", line 3, in <module>
    from langsmith.env._runtime_env import (
  File "/opt/python/lib/python3.12/site-packages/langsmith/env/_runtime_env.py", line 9, in <module>
    from langsmith.utils import get_docker_compose_command
  File "/opt/python/lib/python3.12/site-packages/langsmith/utils.py", line 29, in <module>
    from langsmith import schemas as ls_schemas
  File "/opt/python/lib/python3.12/site-packages/langsmith/schemas.py", line 69, in <module>
    class Example(ExampleBase):
  File "/opt/python/lib/python3.12/site-packages/pydantic/v1/main.py", line 286, in __new__
    cls.__try_update_forward_refs__()
  File "/opt/python/lib/python3.12/site-packages/pydantic/v1/main.py", line 807, in __try_update_forward_refs__
    update_model_forward_refs(cls, cls.__fields__.values(), cls.__config__.json_encoders, localns, (NameError,))
  File "/opt/python/lib/python3.12/site-packages/pydantic/v1/typing.py", line 554, in update_model_forward_refs
    update_field_forward_refs(f, globalns=globalns, localns=localns)
  File "/opt/python/lib/python3.12/site-packages/pydantic/v1/typing.py", line 520, in update_field_forward_refs
    field.type_ = evaluate_forwardref(field.type_, globalns, localns or None)
  File "/opt/python/lib/python3.12/site-packages/pydantic/v1/typing.py", line 66, in evaluate_forwardref
    return cast(Any, type_)._evaluate(globalns, localns, set())

今回はその解決方法の備忘録になります。

原因

エラー名でググるとピンポイントでヒットしました。
https://artificialintelligencearticle.com/facing-forwardref-_evaluate-missing-1-required-keyword-only-argument-recursive_guard-by-aniket-bakre-jul-2024/

LambdaのPythonバージョンが3.12.4にアップグレードされたのが原因のようです。
実際にLambdaで扱われているPythonのバージョンを見ると、確かに3.12.4が使われていますね。

import sys

def lambda_handler(event, context):
    print(f"Python version: {sys.version}")
    # Python version: 3.12.4 (main, Jul 31 2024, 22:52:18) [GCC 11.4.1 20230605 (Red Hat 11.4.1-2)]

加えて、CloudWatch からLambdaの実行履歴を確認すると、8/30を境にPythonのバージョンが更新(v29 -> v30)されていることが確認できます。
image.png

解決策

今回はLambdaのランタイム設定より、Pythonのバージョンを固定する形で対応します。

・Lambdaのコンソール画面より「ランタイム管理設定を編集」を押下します。
image.png

・ランタイムバージョンの更新を「手動」に設定し、先ほどのCloudWatchから確認できたランタイムバージョンARNを記載します。
image.png

これで以前まで動いていたランタイムバージョンが使えるようになり、コードのエラーも出なくなりました。

まとめ

LangChain周りのパッケージはバージョンの相互依存が結構しつこいイメージでしたが、今回もそれに振り回された感じですね。

ランタイムバージョンの手動管理はセキュリティの面では推奨されないので、また時期見て調整は必要ですのでお気をつけください。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?