[Python] Error while creating Neo4jGraph object using langchain

Discussão em 'Python' iniciado por Stack, Maio 22, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to create a neo4j object using the following code. While I run the code, I receive error. Kindly help me fix this error. I have verified that the environment variables are read correctly. I am running this on my local Ubuntu machine.

    Code -

    from langchain_neo4j import Neo4jGraph
    import os
    from dotenv import load_dotenv
    load_dotenv()
    neo4j_uri = os.getenv('NEO4J_URI')
    print(f"Neo4j URI: {neo4j_uri}")
    graph = Neo4jGraph(url= neo4j_uri,username = os.getenv('NEO4J_USERNAME'),password = os.getenv('NEO4J_PASSWORD'))


    Output of the code-

    Neo4j URI: neo4j+s://53XXXe0e.databases.neo4j.io
    ---------------------------------------------------------------------------
    gaierror Traceback (most recent call last)
    File ~/python3.10_venv/lib/python3.10/site-packages/neo4j/_async_compat/network/_util.py:144, in NetworkUtil._dns_resolver(address, family)
    143 try:
    --> 144 info = NetworkUtil.get_address_info(
    145 address.host,
    146 address.port,
    147 family=family,
    148 type=socket.SOCK_STREAM,
    149 )
    150 except OSError as e:

    File ~/python3.10_venv/lib/python3.10/site-packages/neo4j/_async_compat/network/_util.py:130, in NetworkUtil.get_address_info(host, port, family, type, proto, flags)
    128 @staticmethod
    129 def get_address_info(host, port, *, family=0, type=0, proto=0, flags=0):
    --> 130 return socket.getaddrinfo(host, port, family, type, proto, flags)

    File /usr/lib/python3.10/socket.py:967, in getaddrinfo(host, port, family, type, proto, flags)
    966 addrlist = []
    --> 967 for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
    968 af, socktype, proto, canonname, sa = res

    gaierror: [Errno -2] Name or service not known

    The above exception was the direct cause of the following exception:

    ValueError Traceback (most recent call last)
    Cell In[1], line 6
    4 from dotenv import load_dotenv
    5 load_dotenv()
    ----> 6 graph = Neo4jGraph(
    7 url = os.getenv('NEO4J_URI'),
    8 username = os.getenv('NEO4J_USERNAME'),
    9 password = os.getenv('NEO4J_PASSWORD')
    10 )

    File ~/python3.10_venv/lib/python3.10/site-packages/langchain_neo4j/graphs/neo4j_graph.py:379, in Neo4jGraph.__init__(self, url, username, password, database, timeout, sanitize, refresh_schema, driver_config, enhanced_schema)
    377 # Verify connection
    378 try:
    --> 379 self._driver.verify_connectivity()
    380 except neo4j.exceptions.ConfigurationError:
    381 raise ValueError(
    382 "Could not connect to Neo4j database. "
    383 "Please ensure that the driver config is correct"
    384 )

    File ~/python3.10_venv/lib/python3.10/site-packages/neo4j/_sync/driver.py:1081, in Driver.verify_connectivity(self, **config)
    1074 experimental_warn(
    1075 "All configuration key-word arguments to "
    1076 "verify_connectivity() are experimental. They might be "
    1077 "changed or removed in any future version without prior "
    1078 "notice."
    1079 )
    1080 session_config = self._read_session_config(config)
    -> 1081 self._get_server_info(session_config)

    File ~/python3.10_venv/lib/python3.10/site-packages/neo4j/_sync/driver.py:1296, in Driver._get_server_info(self, session_config)
    1294 def _get_server_info(self, session_config) -> ServerInfo:
    1295 with self._session(session_config) as session:
    -> 1296 return session._get_server_info()

    File ~/python3.10_venv/lib/python3.10/site-packages/neo4j/_sync/work/session.py:183, in Session._get_server_info(self)
    181 def _get_server_info(self):
    182 assert not self._connection
    --> 183 self._connect(READ_ACCESS, liveness_check_timeout=0)
    184 server_info = self._connection.server_info
    185 self._disconnect()

    File ~/python3.10_venv/lib/python3.10/site-packages/neo4j/_sync/work/session.py:136, in Session._connect(self, access_mode, **acquire_kwargs)
    134 access_mode = self._config.default_access_mode
    135 try:
    --> 136 super()._connect(
    137 access_mode, auth=self._config.auth, **acquire_kwargs
    138 )
    139 except asyncio.CancelledError:
    140 self._handle_cancellation(message="_connect")

    File ~/python3.10_venv/lib/python3.10/site-packages/neo4j/_sync/work/workspace.py:167, in Workspace._connect(self, access_mode, auth, **acquire_kwargs)
    159 else:
    160 # This is the first time we open a connection to a server in a
    161 # cluster environment for this session without explicitly
    (...)
    164 # we shall use this database explicitly for all subsequent
    165 # actions within this session.
    166 log.debug("[#0000] _: <WORKSPACE> resolve home database")
    --> 167 self._pool.update_routing_table(
    168 database=self._config.database,
    169 imp_user=self._config.impersonated_user,
    170 bookmarks=self._get_bookmarks(),
    171 auth=auth,
    172 acquisition_timeout=acquisition_timeout,
    173 database_callback=self._set_cached_database,
    174 )
    175 acquire_kwargs_ = {
    176 "access_mode": access_mode,
    177 "timeout": acquisition_timeout,
    (...)
    181 "liveness_check_timeout": None,
    182 }
    183 acquire_kwargs_.update(acquire_kwargs)

    File ~/python3.10_venv/lib/python3.10/site-packages/neo4j/_sync/io/_pool.py:900, in Neo4jPool.update_routing_table(self, database, imp_user, bookmarks, auth, acquisition_timeout, database_callback)
    890 existing_routers = set(routing_table.routers)
    892 prefer_initial_routing_address = self.routing_tables[
    893 database
    894 ].initialized_without_writers
    896 if (
    897 prefer_initial_routing_address
    898 and
    899 # TODO: Test this state
    --> 900 self._update_routing_table_from(
    901 self.address,
    902 database=database,
    903 imp_user=imp_user,
    904 bookmarks=bookmarks,
    905 auth=auth,
    906 acquisition_timeout=acquisition_timeout,
    907 database_callback=database_callback,
    908 )
    909 ):
    910 # Why is only the first initial routing address used?
    911 return
    912 if self._update_routing_table_from(
    913 *(existing_routers - {self.address}),
    914 database=database,
    (...)
    919 database_callback=database_callback,
    920 ):

    File ~/python3.10_venv/lib/python3.10/site-packages/neo4j/_sync/io/_pool.py:830, in Neo4jPool._update_routing_table_from(self, database, imp_user, bookmarks, auth, acquisition_timeout, database_callback, *routers)
    822 log.debug(
    823 (
    824 "[#0000] _: <POOL> "
    (...)
    827 ", ".join(map(repr, routers)),
    828 )
    829 for router in routers:
    --> 830 for address in NetworkUtil.resolve_address(
    831 router, resolver=self.pool_config.resolver
    832 ):
    833 new_routing_table = self.fetch_routing_table(
    834 address=address,
    835 acquisition_timeout=acquisition_timeout,
    (...)
    839 auth=auth,
    840 )
    841 if new_routing_table is not None:

    File ~/python3.10_venv/lib/python3.10/site-packages/neo4j/_async_compat/network/_util.py:196, in NetworkUtil.resolve_address(address, family, resolver)
    194 yield address_dns_resolved
    195 else:
    --> 196 for address_dns_resolved in NetworkUtil._dns_resolver(
    197 address, family=family
    198 ):
    199 log.debug(
    200 "[#0000] _: <RESOLVE> dns resolver out: %s",
    201 address_dns_resolved,
    202 )
    203 yield address_dns_resolved

    File ~/python3.10_venv/lib/python3.10/site-packages/neo4j/_async_compat/network/_util.py:151, in NetworkUtil._dns_resolver(address, family)
    144 info = NetworkUtil.get_address_info(
    145 address.host,
    146 address.port,
    147 family=family,
    148 type=socket.SOCK_STREAM,
    149 )
    150 except OSError as e:
    --> 151 raise ValueError(f"Cannot resolve address {address}") from e
    152 return _resolved_addresses_from_info(info, address._host_name)

    ValueError: Cannot resolve address 53XXXe0e.databases.neo4j.io:7687


    Clearing DNS with sudo resolvectl flush-caches, did not help. At this point I am clueless what could be wrong.

    Continue reading...

Compartilhe esta Página