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

RC1 : Python API : Circular API import #2308

Closed
Arron-Clague opened this issue Jun 24, 2024 · 9 comments · Fixed by #2485
Closed

RC1 : Python API : Circular API import #2308

Arron-Clague opened this issue Jun 24, 2024 · 9 comments · Fixed by #2485
Labels
bug Something isn't working

Comments

@Arron-Clague
Copy link

Hi there :

Im using the 1.0.0.RC1 build : if I make a call to send a message I get :

2024-06-24 14:17:20 - Error running graph: Error building Component Empty Prompt:

cannot import name 'process_prompt_template' from partially initialized module 'langflow.base.prompts.api_utils' (most likely due to a circular import) (/home/koni/.local/lib/python3.10/site-packages/langflow/base/prompts/api_utils.py)

Thanks,

Arron

@dosubot dosubot bot added the bug Something isn't working label Jun 24, 2024
Copy link

dosubot bot commented Jun 24, 2024

To resolve the circular import issue when calling send a message using the 1.0.0.RC1 build of the Python API, you can use the __getattr__ function to lazily import the necessary classes. This approach is already implemented in the provided code to avoid circular imports.

Here's the relevant part of the code that handles this:

from typing import Any

from .constants import (
    AgentExecutor,
    BaseChatMemory,
    BaseLanguageModel,
    BaseLLM,
    BaseLoader,
    BaseMemory,
    BaseOutputParser,
    BasePromptTemplate,
    BaseRetriever,
    Callable,
    Chain,
    ChatPromptTemplate,
    Data,
    Document,
    Embeddings,
    NestedDict,
    Object,
    Prompt,
    PromptTemplate,
    TextSplitter,
    Tool,
    VectorStore,
)
from .range_spec import RangeSpec


def _import_template_field():
    from langflow.template.field.base import TemplateField

    return TemplateField


def __getattr__(name: str) -> Any:
    # This is to avoid circular imports
    if name == "TemplateField":
        return _import_template_field()
    elif name == "RangeSpec":
        return RangeSpec
    # The other names should work as if they were imported from constants
    # Import the constants module langflow.field_typing.constants
    from . import constants

    return getattr(constants, name)


__all__ = [
    "NestedDict",
    "Data",
    "Tool",
    "PromptTemplate",
    "Chain",
    "BaseChatMemory",
    "BaseLLM",
    "BaseLanguageModel",
    "BaseLoader",
    "BaseMemory",
    "BaseOutputParser",
    "BaseRetriever",
    "VectorStore",
    "Embeddings",
    "TextSplitter",
    "Document",
    "AgentExecutor",
    "Object",
    "Callable",
    "BasePromptTemplate",
    "ChatPromptTemplate",
    "Prompt",
    "RangeSpec",
    "TemplateField",
]

This code ensures that the TemplateField and RangeSpec classes are only imported when they are actually accessed, thus preventing circular import issues [1][2].

To continue talking to Dosu, mention @dosu.

@nicoloboschi
Copy link
Collaborator

This has been fixed in the latest releases. 1.0.5 doesn't have the problem

@yuchieh8968
Copy link

yuchieh8968 commented Jul 2, 2024

Hello, I am currently encountering this issue while using the latest release. Are there any fixes for this? Many thanks.

@Nitewriter
Copy link

Also experiencing this issue with 1.0.5.

@nicoloboschi
Copy link
Collaborator

@yuchieh8968 @Nitewriter you need to remove and re-add all the broken components after the upgrade

@Nitewriter
Copy link

@nicoloboschi are you referring to components in the flow or python packages? I'm using the Memory Chatbot template as a simple example flow and attempting to run the exported JSON using the sample code provided in the UI.

from langflow.load import run_flow_from_json
TWEAKS = {
  "Memory-hzITR": {},
  "Prompt-viXZL": {},
  "ChatInput-tgsUe": {},
  "OpenAIModel-J2hiT": {},
  "ChatOutput-LH91h": {}
}

result = run_flow_from_json(flow="Memory Chatbot.json",
                            input_value="message",
                            fallback_to_env_vars=True, # False by default
                            tweaks=TWEAKS)

@nicoloboschi nicoloboschi reopened this Jul 2, 2024
@nicoloboschi
Copy link
Collaborator

OK it wasn't clear you were using run_flow_from_json. I'll take a look and try to reproduce then.

@Nitewriter
Copy link

@nicoloboschi Oh shoot, ok I think I'm connecting the dots. The components have the imports that are causing the cycle and the template may have outdated components that I used as an example. I'll try recreating that flow from a blank template.

@Nitewriter
Copy link

new-memory-chat.json

@nicoloboschi Ok, I did try with a clean start on the flow. I'm still seeing the cycle issue. Let me know if there is anything I can provide to help track down the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants