Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing LangChain into a Python OCI Function failing #29468

Open
5 tasks done
bhnorman11 opened this issue Jan 29, 2025 · 5 comments
Open
5 tasks done

Importing LangChain into a Python OCI Function failing #29468

bhnorman11 opened this issue Jan 29, 2025 · 5 comments
Labels
Ɑ: core Related to langchain-core

Comments

@bhnorman11
Copy link

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

from langchain_community.chat_models.oci_generative_ai import ChatOCIGenAI
from langchain_community.utilities import SQLDatabase
from langchain_experimental.sql import SQLDatabaseSequentialChain
from langchain.prompts.prompt import PromptTemplate

Error Message and Stack Trace (if applicable)

"01JJKPZFJS1BT0C20ZJ0011W8X - fn - ERROR - 'NoneType' object is not callable:
File "/python/fdk/runner.py", line 74, in handle_request\n response_data = await with_deadline(ctx, handler_code, body)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n
File "/python/fdk/runner.py", line 54, in with_deadline\n raise ex\n File "/python/fdk/runner.py", line 47, in with_deadline\n handle_func = handler_code.handler()\n ^^^^^^^^^^^^^^^^^^^^^^\n
File "/python/fdk/customer_code.py", line 94, in handler\n mod = self._delayed_module_class.get_module()\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n
File "/python/fdk/customer_code.py", line 59, in get_module\n .load_module()\n ^^^^^^^^^^^^^\n
File "", line 605, in _check_name_wrapper\n File "", line 1121, in load_module\n
File "", line 945, in load_module\n
File "", line 290, in _load_module_shim\n
File "", line 721, in _load\n
File "", line 690, in _load_unlocked\n
File "", line 940, in exec_module\n
File "", line 241, in _call_with_frames_removed\n
File "/function/func.py", line 6, in \n from langchain_community.chat_models.oci_generative_ai import ChatOCIGenAI\n
File "/python/langchain_community/chat_models/oci_generative_ai.py", line 18, in \n from langchain_core.callbacks import CallbackManagerForLLMRun\n
File "/python/langchain_core/callbacks/init.py", line 23, in \n from langchain_core.callbacks.manager import (\n
File "/python/langchain_core/callbacks/manager.py", line 23, in \n from langsmith.run_helpers import get_tracing_context\n
File "/python/langsmith/run_helpers.py", line 56, in \n _PARENT_RUN_TREE = contextvars.ContextVar[Optional[run_trees.RunTree]](\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"

Description

I'm trying to use LangChain in a Python OCI Function and I'm getting the following error when trying to import anything from LangChain.

I get the error for all of these import statements (have tried each individually, same result) with or without using any of these imports further in my code.

Some context on my environment:

The Function is deploying successfully, error only occurs at invocation.
Building in Python 3.11
My requirements.txt has the following:

fdk>=0.1.86
oci
oracledb
langchain-community==0.3.15
langchain-experimental==0.3.4
langchain==0.3.15
sqlalchemy

I've tried pinning the versions as above and not pinning the versions, same error. I've also successfully run these imports along with the rest of my code locally using the above pinned versions in Python 3.11, and it works perfectly fine.

I've also tried using earlier LangChain versions along with using a Python 3.9 and Python 3.8 OCI Function environment, same issue.

It seems therefore to be a problem running these particular Python imports in the OCI Function environment. Taking out these imports allows my code to work ok.

System Info

System Information

OS: Windows
OS Version: 10.0.26100
Python Version: 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]

Package Information

langchain_core: 0.3.31
langchain: 0.3.15
langchain_community: 0.3.15
langsmith: 0.3.1
langchain-job: Installed. No version info available.
langchain_anthropic: 0.3.4
langchain_astradb: 0.5.2
langchain_aws: 0.2.7
langchain_chroma: 0.2.0
langchain_cohere: 0.3.5
langchain_elasticsearch: 0.3.2
langchain_experimental: 0.3.4
langchain_google_calendar_tools: 0.0.1
langchain_google_community: 2.0.4
langchain_google_genai: 2.0.9
langchain_google_vertexai: 2.0.7
langchain_groq: 0.2.3
langchain_milvus: 0.1.7
langchain_mistralai: 0.2.4
langchain_mongodb: 0.4.0
langchain_nvidia: Installed. No version info available.
langchain_nvidia_ai_endpoints: 0.3.7
langchain_ollama: 0.2.1
langchain_openai: 0.2.14
langchain_pinecone: 0.2.2
langchain_tests: 0.3.9
langchain_text_splitters: 0.3.5
langchain_unstructured: 0.1.5
langchainhub: 0.1.21

Optional packages not installed

langserve

Other Dependencies

aiohttp: 3.11.11
anthropic: 0.45.0
anthropic[vertexai]: Installed. No version info available.
astrapy: 1.5.2
async-timeout: 5.0.1
beautifulsoup4: 4.12.3
boto3: 1.34.162
chromadb: 0.5.23
cohere: 5.13.11
dataclasses-json: 0.6.7
db-dtypes: Installed. No version info available.
defusedxml: 0.7.1
elasticsearch[vectorstore-mmr]: Installed. No version info available.
fastapi: 0.115.7
filetype: 1.2.0
gapic-google-longrunning: Installed. No version info available.
google-api-core: 2.24.0
google-api-python-client: 2.159.0
google-api-python-client>=2.104.0: Installed. No version info available.
google-auth-httplib2: 0.2.0
google-auth-oauthlib: 1.2.1
google-auth-oauthlib>=1.1.0: Installed. No version info available.
google-cloud-aiplatform: 1.78.0
google-cloud-bigquery: 3.29.0
google-cloud-bigquery-storage: Installed. No version info available.
google-cloud-contentwarehouse: Installed. No version info available.
google-cloud-core: 2.4.1
google-cloud-discoveryengine: Installed. No version info available.
google-cloud-documentai: Installed. No version info available.
google-cloud-documentai-toolbox: Installed. No version info available.
google-cloud-speech: Installed. No version info available.
google-cloud-storage: 2.19.0
google-cloud-texttospeech: Installed. No version info available.
google-cloud-translate: Installed. No version info available.
google-cloud-vision: Installed. No version info available.
google-generativeai: 0.8.4
googlemaps: Installed. No version info available.
groq: 0.15.0
grpcio: 1.70.0
httpx: 0.28.1
httpx-sse: 0.4.0
jsonpatch: 1.33
langchain-core>=0.3: Installed. No version info available.
langchain-text-splitters>=0.3: Installed. No version info available.
langchain>=0.0.335: Installed. No version info available.
langchain>=0.3: Installed. No version info available.
langsmith-pyo3: Installed. No version info available.
numpy: 1.26.4
numpy>=1.26: Installed. No version info available.
ollama: 0.3.3
openai: 1.60.0
orjson: 3.10.15
packaging: 24.2
pandas: 2.2.2
pillow: 10.4.0
pinecone: 5.4.2
protobuf>=4.25.0: Installed. No version info available.
pyarrow: 17.0.0
pydantic: 2.9.2
pydantic-settings: 2.7.1
pymilvus: 2.4.9
pymongo>=4.6.1: Installed. No version info available.
pytest: 8.3.4
pytest-asyncio: 0.25.2
pytest-socket: 0.7.0
pytz>=2023.3.post1: Installed. No version info available.
PyYAML: 6.0.2
requests: 2.32.3
requests-toolbelt: 1.0.0
rich: 13.9.4
SQLAlchemy: 2.0.37
syrupy: 4.8.1
tabulate: 0.9.0
tenacity: 9.0.0
tiktoken: 0.7.0
tokenizers: 0.20.3
types-requests: 2.32.0.20241016
typing-extensions: 4.12.2
unstructured-client: 0.25.9
unstructured[all-docs]: Installed. No version info available.
zstandard: 0.23.0

@dosubot dosubot bot added the Ɑ: core Related to langchain-core label Jan 29, 2025
@keenborder786
Copy link
Contributor

Please change your import to:

from langchain_community.chat_models.oci_generative_ai import ChatOCIGenAI
from langchain_community.utilities import SQLDatabase
from langchain_experimental.sql import SQLDatabaseSequentialChain
from langchain_core.prompts import PromptTemplate
``

@bhnorman11
Copy link
Author

Please change your import to:

from langchain_community.chat_models.oci_generative_ai import ChatOCIGenAI
from langchain_community.utilities import SQLDatabase
from langchain_experimental.sql import SQLDatabaseSequentialChain
from langchain_core.prompts import PromptTemplate
``

Thanks for your response. Have corrected the import, have received the same error on function invocation.

@NaotoNaka
Copy link

I dug into this because I had the same error, and it seems that the issue might be a compatibility problem among Docker, fdk, and LangChain rather than an issue with LangChain itself.

I worked around the problem by using a venv inside the Docker image for an extra layer of isolation.

I didn't use the Cloud Shell because this Dockerfile only worked when I built it locally and then pushed (error code at the bottom).

FROM fnproject/python:3.11 as build-stage
WORKDIR /function

COPY requirements.txt .
RUN python3 -m venv venv && \
    venv/bin/pip install --upgrade pip && \
    venv/bin/pip install --no-cache-dir -r requirements.txt && \
    rm -rf ~/.cache/pip /tmp*

COPY . .

FROM fnproject/python:3.11
WORKDIR /function

COPY --from=build-stage /function/venv venv
COPY --from=build-stage /function .

ENV PYTHONPATH=/function
ENV PATH="/function/venv/bin:$PATH"
RUN venv/bin/python -c "from langchain_community.chat_models.oci_generative_ai import ChatOCIGenAI"

ENTRYPOINT ["/function/venv/bin/fdk", "/function/func.py", "handler"]

I then uploaded the image to OCI Container Registry with docker push. I hope this helps :)

This might be not the right place to post, but FYI, the following code is the error that i encountered when i fn -v deploy in Cloud Shell:

=> ERROR [stage-1 5/5] RUN venv/bin/python -c "from langchain_community.chat_models.oci_generative_ai import ChatOCIGenAI"                                                                                                                                            3.0s 
------                                                                                                                                                                                                                                                                      
 > [stage-1 5/5] RUN venv/bin/python -c "from langchain_community.chat_models.oci_generative_ai import ChatOCIGenAI":                                                                                                                                                       
2.750 Traceback (most recent call last):                                                                                                                                                                                                                                    
2.756   File "<string>", line 1, in <module>                                                                                                                                                                                                                                
2.756   File "/function/venv/lib64/python3.11/site-packages/langchain_community/chat_models/oci_generative_ai.py", line 20, in <module>                                                                                                                                     
2.761     from langchain_core.callbacks import CallbackManagerForLLMRun                                                                                                                                                                                                     
2.763   File "/function/venv/lib64/python3.11/site-packages/langchain_core/callbacks/__init__.py", line 22, in <module>
2.764     from langchain_core.callbacks.file import FileCallbackHandler
2.765   File "/function/venv/lib64/python3.11/site-packages/langchain_core/callbacks/file.py", line 7, in <module>
2.766     from langchain_core.agents import AgentAction, AgentFinish
2.767   File "/function/venv/lib64/python3.11/site-packages/langchain_core/agents.py", line 31, in <module>
2.768     from langchain_core.load.serializable import Serializable
2.769   File "/function/venv/lib64/python3.11/site-packages/langchain_core/load/__init__.py", line 3, in <module>
2.770     from langchain_core.load.dump import dumpd, dumps
2.771   File "/function/venv/lib64/python3.11/site-packages/langchain_core/load/dump.py", line 4, in <module>
2.772     from langchain_core.load.serializable import Serializable, to_json_not_implemented
2.772   File "/function/venv/lib64/python3.11/site-packages/langchain_core/load/serializable.py", line 12, in <module>
2.773     from pydantic import BaseModel, ConfigDict
2.774   File "/function/venv/lib64/python3.11/site-packages/pydantic/__init__.py", line 421, in __getattr__
2.775     module = import_module(module_name, package=package)
2.778              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2.779   File "/usr/lib64/python3.11/importlib/__init__.py", line 126, in import_module
2.780     return _bootstrap._gcd_import(name[level:], package, level)
2.782            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2.782   File "/function/venv/lib64/python3.11/site-packages/pydantic/main.py", line 29, in <module>
2.783     import pydantic_core
2.783   File "/function/venv/lib64/python3.11/site-packages/pydantic_core/__init__.py", line 6, in <module>
2.784     from ._pydantic_core import (
2.784 ImportError: /function/venv/lib64/python3.11/site-packages/pydantic_core/_pydantic_core.cpython-311-x86_64-linux-gnu.so: ELF load command alignment not page-aligned
------

 1 warning found (use docker --debug to expand):
 - FromAsCasing: 'as' and 'FROM' keywords' casing do not match (line 1)
Dockerfile:18
--------------------
  16 |     ENV PYTHONPATH=/function
  17 |     ENV PATH="/function/venv/bin:$PATH"
  18 | >>> RUN venv/bin/python -c "from langchain_community.chat_models.oci_generative_ai import ChatOCIGenAI"
  19 |     
  20 |     ENTRYPOINT ["/python/bin/fdk", "/function/func.py", "handler"]
--------------------
ERROR: failed to solve: process "/dev/.buildkit_qemu_emulator /bin/sh -c venv/bin/python -c \"from langchain_community.chat_models.oci_generative_ai import ChatOCIGenAI\"" did not complete successfully: exit code: 1


Fn: error running docker build: exit status 1

@bhnorman11
Copy link
Author

I dug into this because I had the same error, and it seems that the issue might be a compatibility problem among Docker, fdk, and LangChain rather than an issue with LangChain itself.

I worked around the problem by using a venv inside the Docker image for an extra layer of isolation.

I didn't use the Cloud Shell because this Dockerfile only worked when I built it locally and then pushed (error code at the bottom).

FROM fnproject/python:3.11 as build-stage
WORKDIR /function

COPY requirements.txt .
RUN python3 -m venv venv &&
venv/bin/pip install --upgrade pip &&
venv/bin/pip install --no-cache-dir -r requirements.txt &&
rm -rf ~/.cache/pip /tmp*

COPY . .

FROM fnproject/python:3.11
WORKDIR /function

COPY --from=build-stage /function/venv venv
COPY --from=build-stage /function .

ENV PYTHONPATH=/function
ENV PATH="/function/venv/bin:$PATH"
RUN venv/bin/python -c "from langchain_community.chat_models.oci_generative_ai import ChatOCIGenAI"

ENTRYPOINT ["/function/venv/bin/fdk", "/function/func.py", "handler"]
I then uploaded the image to OCI Container Registry with docker push. I hope this helps :)

This might be not the right place to post, but FYI, the following code is the error that i encountered when i fn -v deploy in Cloud Shell:

=> ERROR [stage-1 5/5] RUN venv/bin/python -c "from langchain_community.chat_models.oci_generative_ai import ChatOCIGenAI"                                                                                                                                            3.0s 
------                                                                                                                                                                                                                                                                      
 > [stage-1 5/5] RUN venv/bin/python -c "from langchain_community.chat_models.oci_generative_ai import ChatOCIGenAI":                                                                                                                                                       
2.750 Traceback (most recent call last):                                                                                                                                                                                                                                    
2.756   File "<string>", line 1, in <module>                                                                                                                                                                                                                                
2.756   File "/function/venv/lib64/python3.11/site-packages/langchain_community/chat_models/oci_generative_ai.py", line 20, in <module>                                                                                                                                     
2.761     from langchain_core.callbacks import CallbackManagerForLLMRun                                                                                                                                                                                                     
2.763   File "/function/venv/lib64/python3.11/site-packages/langchain_core/callbacks/__init__.py", line 22, in <module>
2.764     from langchain_core.callbacks.file import FileCallbackHandler
2.765   File "/function/venv/lib64/python3.11/site-packages/langchain_core/callbacks/file.py", line 7, in <module>
2.766     from langchain_core.agents import AgentAction, AgentFinish
2.767   File "/function/venv/lib64/python3.11/site-packages/langchain_core/agents.py", line 31, in <module>
2.768     from langchain_core.load.serializable import Serializable
2.769   File "/function/venv/lib64/python3.11/site-packages/langchain_core/load/__init__.py", line 3, in <module>
2.770     from langchain_core.load.dump import dumpd, dumps
2.771   File "/function/venv/lib64/python3.11/site-packages/langchain_core/load/dump.py", line 4, in <module>
2.772     from langchain_core.load.serializable import Serializable, to_json_not_implemented
2.772   File "/function/venv/lib64/python3.11/site-packages/langchain_core/load/serializable.py", line 12, in <module>
2.773     from pydantic import BaseModel, ConfigDict
2.774   File "/function/venv/lib64/python3.11/site-packages/pydantic/__init__.py", line 421, in __getattr__
2.775     module = import_module(module_name, package=package)
2.778              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2.779   File "/usr/lib64/python3.11/importlib/__init__.py", line 126, in import_module
2.780     return _bootstrap._gcd_import(name[level:], package, level)
2.782            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2.782   File "/function/venv/lib64/python3.11/site-packages/pydantic/main.py", line 29, in <module>
2.783     import pydantic_core
2.783   File "/function/venv/lib64/python3.11/site-packages/pydantic_core/__init__.py", line 6, in <module>
2.784     from ._pydantic_core import (
2.784 ImportError: /function/venv/lib64/python3.11/site-packages/pydantic_core/_pydantic_core.cpython-311-x86_64-linux-gnu.so: ELF load command alignment not page-aligned
------

 1 warning found (use docker --debug to expand):
 - FromAsCasing: 'as' and 'FROM' keywords' casing do not match (line 1)
Dockerfile:18
--------------------
  16 |     ENV PYTHONPATH=/function
  17 |     ENV PATH="/function/venv/bin:$PATH"
  18 | >>> RUN venv/bin/python -c "from langchain_community.chat_models.oci_generative_ai import ChatOCIGenAI"
  19 |     
  20 |     ENTRYPOINT ["/python/bin/fdk", "/function/func.py", "handler"]
--------------------
ERROR: failed to solve: process "/dev/.buildkit_qemu_emulator /bin/sh -c venv/bin/python -c \"from langchain_community.chat_models.oci_generative_ai import ChatOCIGenAI\"" did not complete successfully: exit code: 1


Fn: error running docker build: exit status 1

That's worked for me, thank you! I'm also deploying locally so not sure about that cloud editor error, though I have had to downgrade Pydantic to 2.9.2 previously due to other issues with it so perhaps that's part of it.

Have you tried to actually call ChatOCIGenAI() yet? Having trouble authenticating both using api key and resource principal.

@NaotoNaka
Copy link

That's worked for me

Glad to hear it worked! The Dockerfile error went away when i changed my oci environment, so langchain might not be the main reason for the error.

Having trouble authenticating

The genai is working beautifully here. Check the documentation once again, and if still persists, we can discuss this in another issue thread :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ɑ: core Related to langchain-core
Projects
None yet
Development

No branches or pull requests

3 participants