PraisonAI icon indicating copy to clipboard operation
PraisonAI copied to clipboard

import error

Open SkalaFrost opened this issue 6 months ago • 3 comments

I encountered an error while running the example. Package version: 0.0.99 Error message: ImportError: cannot import name 'Memory' from 'praisonaiagents.memory' (unknown location)

SkalaFrost avatar Jun 05 '25 04:06 SkalaFrost

@SkalaFrost, can you please provide me with the full code so I can try to test it?

Also, did you follow this documentation? https://docs.praison.ai/docs/concepts/memory

Thank you.

MervinPraison avatar Jun 05 '25 06:06 MervinPraison

Sure @MervinPraisonm code: `from praisonaiagents.agents.agents import Agent, Task, PraisonAIAgents
from praisonaiagents.llm.llm import LLM import os import json from dotenv import load_dotenv load_dotenv() def setup_llm(): config = { "model": os.getenv('OPENAI_MODEL', "gpt-4o-mini"), "api_key": os.getenv('OPENAI_API_KEY'), "temperature": 0.7, "max_tokens": 2000 } base_url = os.getenv('OPENAI_BASE_URL') if base_url: config["base_url"] = base_url return LLM(**config)

blog_agent = Agent( role="Blog Writer", goal="Write a blog post about AI", backstory="Expert at writing blog posts", llm=setup_llm(), ) blog_task = Task( description="Write a blog post about AI trends", expected_output="Well-written blog post about AI trends", agent=blog_agent ) agents = PraisonAIAgents( agents=[blog_agent], tasks=[blog_task], memory=True )
result = agents.start() print(result)`

Error: Traceback (most recent call last): File "\test.py", line 1, in <module> from praisonaiagents.agents.agents import Agent, Task, PraisonAIAgents ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "\.venv\Lib\site-packages\praisonaiagents\__init__.py", line 14, in <module> from .session import Session File "\.venv\Lib\site-packages\praisonaiagents\session.py", line 12, in <module> from .memory import Memory ImportError: cannot import name 'Memory' from 'praisonaiagents.memory' (unknown location)

SkalaFrost avatar Jun 05 '25 08:06 SkalaFrost

I get the same erroras SkalaFrost when trying to run the single agent example code.

dev-anthonyrae avatar Jun 05 '25 11:06 dev-anthonyrae

The praisonaiagents pip package is broken in latest versions (until 0.0.99 at least).

Try:

pip install praisonaiagents==0.0.96

and it will work.

DracoBlue avatar Jun 05 '25 16:06 DracoBlue

like memory, guardrail is also getting same import error

sureshtmca avatar Jun 05 '25 16:06 sureshtmca

@SkalaFrost @dev-anthonyrae @DracoBlue The bug has been fixed. Please upgrade to the latest version now and it should work

if you want to use litellm config and memory then you need to use

pip install -U "praisonaiagents[llm,memory]"

Please let me know if you still have issues. Happy to review and fix it quickly.

Here is the working code

from praisonaiagents import Agent, Task, PraisonAIAgents
import os
from dotenv import load_dotenv

load_dotenv()

llm_config = {
    "model": "openai/gpt-4o-mini",
    "api_key": os.getenv('OPENAI_API_KEY'),
    "temperature": 0.7,
    "max_tokens": 2000
}

blog_agent = Agent(
    role="Blog Writer",
    goal="Write a blog post about AI",
    backstory="Expert at writing blog posts",
    llm=llm_config,
)

blog_task = Task(
    description="Write a blog post about AI trends in 1 paragraph",
    expected_output="Well-written blog post about AI trends",
    agent=blog_agent
)

agents = PraisonAIAgents(
    agents=[blog_agent],
    tasks=[blog_task],
    memory=True
)

result = agents.start()

Docs :

How to use Models and config: https://docs.praison.ai/docs/models How to use Memory: https://docs.praison.ai/docs/concepts/memory

Note: I am also working on making the memory more efficient. So stay tuned. Thank you

MervinPraison avatar Jun 05 '25 16:06 MervinPraison

@sureshtmca can you please post your code you are running, then i could test it at my end. thank you

MervinPraison avatar Jun 05 '25 16:06 MervinPraison

@sureshtmca Please try this guardrail code

Note : I include 5 words as a guardrail test below. Remove that 5 words from the code and it will allow the agent to run.

This is just an example

from praisonaiagents import Agent

def validate_content(data):
    if len(str(data)) < 50:
        return False, "Content too short"
    return True, data

agent = Agent(
    instructions="You are a writer",
    guardrail=validate_content,
    max_guardrail_retries=1
)

agent.start("Write a welcome message with 5 words")

with multi agents

from praisonaiagents import Agent, Task, TaskOutput, PraisonAIAgents
from typing import Tuple, Any

def validate_content(task_output: TaskOutput) -> Tuple[bool, Any]:
    if len(task_output.raw) < 50:
        return False, "Content too short"
    return True, task_output

agent = Agent(
    instructions="You are a writer",
)

task = Task(
    description="Write a welcome message 5 words",
    guardrail=validate_content,
    agent=agent
)

praison_agents = PraisonAIAgents(agents=[agent], tasks=[task])

praison_agents.start()

MervinPraison avatar Jun 05 '25 16:06 MervinPraison

Same import error. Using uv as package manager.

uv init projectname
cd projectname
uv add ""praisonaiagents[llm,memory]"

main.py

from praisonaiagents import Agent, PraisonAIAgents
from config import small_llm_config


# Create a simple agent
summarise_agent = Agent(
    instructions="Summarise Photosynthesis",
    llm=small_llm_config,
    verbose=True,
)

# Run the agent
agents = PraisonAIAgents(agents=[summarise_agent])
agents.start()

error

Traceback (most recent call last):
  File "/Users/ucag/practice/south_extract/./summarize_agent.py", line 1, in <module>
    from praisonaiagents import Agent, PraisonAIAgents
  File "/Users/ucag/practice/south_extract/.venv/lib/python3.11/site-packages/praisonaiagents/__init__.py", line 14, in <module>
    from .session import Session
  File "/Users/ucag/practice/south_extract/.venv/lib/python3.11/site-packages/praisonaiagents/session.py", line 12, in <module>
    from .memory import Memory
ImportError: cannot import name 'Memory' from 'praisonaiagents.memory' (unknown location)

By the way, version resolved by uv is

"praisonaiagents[llm,memory]>=0.0.99",

Ucag avatar Jun 05 '25 16:06 Ucag

@claude, review this error and propose a solution fix for this

also in memory.py consider this, it should work both with litellm and without litellm

Here is the change below

-    # Use OpenAI client from main.py
-    from ..main import Client
+    # Use LiteLLM for consistency with the rest of the codebase
+    import litellm

-    response = client.chat.completions.create(
-        model=llm or "gpt-4o",
+    # Convert model name if it's in litellm format
+    model_name = llm or "gpt-4o-mini"
+    response = litellm.completion(
+        model=model_name,

MervinPraison avatar Jun 05 '25 16:06 MervinPraison

Claude finished @MervinPraison's task in 3m 14s —— View jobclaude/issue-605-20250605_165941Create PR ➔


Memory Import Error Fix Complete ✅

Todo List:

  • [x] Investigate current memory.py implementation and import structure
  • [x] Check session.py import statement
  • [x] Analyze package structure and init.py files
  • [x] Implement litellm compatibility changes as suggested
  • [x] Ensure backward compatibility for non-litellm usage
  • [x] Test import chain works correctly
  • [x] Commit the fix

Summary of Changes

Root Cause: The Memory class was properly defined but had dependency issues with OpenAI client imports that weren't handling optional dependencies correctly.

Fix Applied:

  1. Added litellm compatibility with fallback chain:

    • ✅ Added LITELLM_AVAILABLE flag for dependency detection
    • ✅ Implemented litellm → openai → graceful degradation pattern
    • ✅ Updated all 4 locations where embeddings/LLM calls are made
  2. Enhanced package exports:

    • ✅ Added Memory to main package __init__.py exports
    • ✅ Users can now import as from praisonaiagents import Memory
  3. Improved error handling:

    • ✅ Graceful degradation when neither litellm nor openai is available
    • ✅ Proper logging for dependency issues
    • ✅ Backward compatibility maintained

Files Modified:

  • src/praisonai-agents/praisonaiagents/memory/memory.py - Core fix with litellm compatibility
  • src/praisonai-agents/praisonaiagents/__init__.py - Added Memory export

Testing: Created comprehensive test suite (test_comprehensive_import.py) that verifies:

  • ✅ Original failing import: from praisonaiagents.agents.agents import Agent, Task, PraisonAIAgents
  • ✅ Direct memory import: from praisonaiagents.memory import Memory
  • ✅ Package root import: from praisonaiagents import Memory
  • ✅ Memory instantiation and basic operations

The fix maintains backward compatibility while adding the requested litellm integration with proper fallbacks. Users should now be able to install and use praisonaiagents without import errors.


claude[bot] avatar Jun 05 '25 16:06 claude[bot]

@SkalaFrost @dev-anthonyrae @DracoBlue @sureshtmca @Ucag Please upgrade to the latest version and the issue has been fixed.

If you still face issue, please raise the issue again or reopen this issue with the full code and output.

Thanks for your support.

MervinPraison avatar Jun 05 '25 21:06 MervinPraison

Thanks @MervinPraison for the prompt fix!

dev-anthonyrae avatar Jun 05 '25 22:06 dev-anthonyrae

i could not keep calm without saying this. the structure of output generated from Agents and the validation of output (without user's much efforts) with instruction on reflection, is just just beautiful. You are awesome @MervinPraison

sureshtmca avatar Jun 07 '25 12:06 sureshtmca