I am making a Python LangGraph application in Jupyter Notebook. I have a node that is giving me problems. Short story short, I created a node that detects if the message is a list and it look them up in in the system files and return them to the next node. 

So far so good, the problem I am having is that the node is not working as intended, and I don't understand why. I checked the functions inside and they work perfectly by themselves, but when using them inside the node they don't work. Help me find why and solve this problem please.

# This is the text that proves it works:

import ast
import asyncio
from functools import partial

def check_list(string, *args):
    try:
        if isinstance(string, list):
            return True
        else:
            result = ast.literal_eval(string)
            return isinstance(result, list)
    except (ValueError, SyntaxError):
        return False
    
async def is_python_list(string):
    loop = asyncio.get_event_loop()
    partial_check_list = partial(check_list, string)
    return await loop.run_in_executor(None, partial_check_list)

response = AIMessage("['sql.md', 'delete.md', 'index.md', 'select.md']")
# this also works:  response = AIMessage(['sql.md', 'delete.md', 'index.md', 'select.md'])

if (await is_python_list(response.content)):
    context = "OK"
else:
    context = "NO RULES"        

print(f"Rules found: {context}") # Rules Found: OK

Now this is the node in question, the state graph and the output on console:

import ast
import asyncio
import re
from concurrent.futures import ThreadPoolExecutor
from functools import partial

class RulesFinder:
    def __init__(self, config, name="rulesfinder"):
        self.prompt_template = ChatPromptTemplate.from_template(config['prompts']['rulesfinder']['template'])
        self.llm = ChatOpenAI(
            model=config['nodes']['rulesfinder']['model'],
            openai_api_key=...
            openai_api_base=...
        )
        self.name = name

    def read_file(self, filename, *args):
        with open(filename, 'r') as f:
            return f.read()

    async def async_read_file(self, filename):
        loop = asyncio.get_event_loop()
        contents = await loop.run_in_executor(None, self.read_file, filename)
        return contents
        
    async def retrieve_rules(self, directory, response):
        rules_contents = []
        for file_name in response:
            contents = await self.async_read_file(os.path.join(directory, file_name))
            rules_contents.append(contents)
        return rules_contents
    
    def check_list(string, *args):
        try:
            if isinstance(string, list):
                return True
            else:
                result = ast.literal_eval(string)
                return isinstance(result, list)
        except (ValueError, SyntaxError):
            return False
    
    async def is_python_list(self, string):
        loop = asyncio.get_event_loop()
        partial_check_list = partial(self.check_list, string)
        return await loop.run_in_executor(None, partial_check_list)
    
    async def run(self, state):
        logger.info(f"Ejecutando nodo rulesfinder con estado: {state}")
        message_content = state["messages"][0].content if state["messages"] else ""
        index = await self.async_read_file('./rules/index.json')
        formatted_prompt = self.prompt_template.format_prompt(index=index, message=message_content).to_messages()
        response = await self.llm.ainvoke(formatted_prompt)
        logger.info(f"RulesFinder Response: {response}")

        if (await self.is_python_list(response.content)):
            context = await self.retrieve_rules('./rules', response.content)
        else:
            context = "No Rules"        
        
        logger.info(f"RulesFinder Rules Found: {context}")
        
        # ...
        return state

This is the terminal output:

2024-10-21 08:44:50,239 - INFO - RulesFinder Response: content="['sql.md', 'delete.md', 'index.md', 'select.md', 'sql_intructions.md', 'error_handling.md']" additional_kwargs={'refusal': ''} response_metadata={...} id='...' usage_metadata={...}
2024-10-21 09:10:08,350 - INFO - RulesFinder Reglas Found: No Rules