Skip to content

fix: handle None agent name when generating history session ID#1114

Open
jnMetaCode wants to merge 1 commit intoMervinPraison:mainfrom
jnMetaCode:fix-agent-name-none-crash
Open

fix: handle None agent name when generating history session ID#1114
jnMetaCode wants to merge 1 commit intoMervinPraison:mainfrom
jnMetaCode:fix-agent-name-none-crash

Conversation

@jnMetaCode
Copy link

@jnMetaCode jnMetaCode commented Mar 8, 2026

Problem

When creating an agent with instructions but without an explicit name, and memory history is enabled (history=True), the agent crashes with:

AttributeError: 'NoneType' object has no attribute 'encode'

This happens at line 999 in agent.py:

_agent_hash = _hl.md5(name.encode()).hexdigest()[:8]

When no name is provided, name remains None, and calling .encode() on it fails.

Fix

Use a fallback string when name is None:

_agent_hash = _hl.md5((name or "agent").encode()).hexdigest()[:8]

This ensures a valid session ID is always generated, even for unnamed agents.

How to reproduce

from praisonaiagents import Agent

agent = Agent(instructions="Do something", history=True)
# Crashes with AttributeError before this fix

Summary by CodeRabbit

  • Bug Fixes
    • Improved session ID generation for agents created without explicit names, ensuring more reliable behavior when history tracking is enabled.

When an agent is created with instructions but no explicit name, and
memory history is enabled, name can be None. Calling .encode() on None
raises AttributeError. Fall back to "agent" when name is None.

Signed-off-by: JiangNan <1394485448@qq.com>
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical bug where agents initialized with memory history enabled but without an explicit name would crash due to an AttributeError. The fix ensures that a valid session ID is always generated by providing a fallback name, thereby improving the robustness and stability of agent creation and preventing application failures in such scenarios.

Highlights

  • Bug Fix: Addressed an AttributeError that occurred when generating a history session ID for agents created without an explicit name, by providing a default name "agent" if the agent's name is None.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/praisonai-agents/praisonaiagents/agent/agent.py
    • Fixed an issue where the agent's name could be None when generating the agent hash for history session IDs, leading to an AttributeError.
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@qodo-code-review
Copy link

Review Summary by Qodo

Fix AttributeError when agent name is None with history enabled

🐞 Bug fix

Grey Divider

Walkthroughs

Description
• Handle None agent name when generating history session ID
• Use fallback "agent" string to prevent AttributeError
• Ensures valid session ID generation for unnamed agents
Diagram
flowchart LR
  A["Agent created without name"] -->|history=True| B["Generate session ID"]
  B -->|name is None| C["Use fallback 'agent' string"]
  C -->|encode and hash| D["Valid session ID created"]
Loading

Grey Divider

File Changes

1. src/praisonai-agents/praisonaiagents/agent/agent.py 🐞 Bug fix +1/-1

Handle None agent name in session ID generation

• Fixed AttributeError by adding fallback value for None agent name
• Changed name.encode() to (name or "agent").encode()
• Ensures session ID generation works for unnamed agents with history enabled

src/praisonai-agents/praisonaiagents/agent/agent.py


Grey Divider

Qodo Logo

@qodo-code-review
Copy link

qodo-code-review bot commented Mar 8, 2026

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider


Action required

1. Unnamed history collision 🐞 Bug ⛨ Security
Description
When history is enabled and no explicit session_id is provided, falling back to the constant string
"agent" makes all unnamed agents compute the same history session_id, causing them to load/append to
the same persisted chat history. This can mix conversations between unrelated agents and leak prior
prompts/responses into new runs.
Code

src/praisonai-agents/praisonaiagents/agent/agent.py[R999-1001]

+            _agent_hash = _hl.md5((name or "agent").encode()).hexdigest()[:8]
            session_id = f"history_{_agent_hash}"
            _history_session_id = session_id
Evidence
The new code hashes a constant fallback string when name is falsy, producing a constant session_id
for all unnamed agents in the specific branch where history is enabled and both session_id and
_history_session_id are unset. That session_id is then used to load and inject persisted messages
from the JSON session store, which is keyed solely by session_id, so different agents end up sharing
the same on-disk session file/history.

src/praisonai-agents/praisonaiagents/agent/agent.py[988-1001]
src/praisonai-agents/praisonaiagents/agent/agent.py[1192-1206]
src/praisonai-agents/praisonaiagents/agent/agent.py[4226-4238]
src/praisonai-agents/praisonaiagents/session/store.py[236-240]
src/praisonai-agents/praisonaiagents/session/README.md[27-31]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`history=True` with no explicit `session_id` currently auto-generates a session id by hashing `(name or &quot;agent&quot;)`. When `name` is `None` (a supported state), this becomes a constant session id shared by all unnamed agents, causing persisted history mixing / leakage.

### Issue Context
- The history branch executes only when `_history_enabled` is true and both `session_id` and `_history_session_id` are unset.
- The generated `session_id` is used as the key for JSON session persistence and for injecting past chat history into subsequent prompts.

### Fix Focus Areas
- Ensure unnamed agents do **not** converge on a single constant history session id.
- Prefer a unique value when `name` is missing (e.g., `uuid.uuid4().hex` or an already-available per-instance `agent_id`).
- Keep deterministic behavior for explicitly named agents.

- src/praisonai-agents/praisonaiagents/agent/agent.py[988-1001]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

ⓘ The new review experience is currently in Beta. Learn more

Grey Divider

Qodo Logo

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 8, 2026

📝 Walkthrough

Walkthrough

A single-line modification in the Agent initialization method improves hash derivation robustness. When history is enabled without an explicit session ID or name, the code now uses a fallback value to ensure consistent session ID generation instead of potentially failing with a None value.

Changes

Cohort / File(s) Summary
Agent Hash Derivation
src/praisonai-agents/praisonaiagents/agent/agent.py
Modified hash computation to use (name or "agent") fallback, ensuring session ID derivation remains consistent even when agent name is not explicitly provided.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 A fluffy agent hops with glee,
No name? No problem, just use "agent," you see!
With fallback in place, the session runs true,
Hash derivatives dance the whole journey through! 🌟

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main fix: handling None agent names when generating history session IDs, which directly matches the code change and PR objectives.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses an AttributeError that occurs when an agent is created with history=True but without an explicit name. The fix correctly handles the None value for the agent name by providing a default fallback string "agent" before encoding it to generate a history session ID. The change is simple, effective, and resolves the described bug.

Note: Security Review did not run due to the size of the PR.

Comment on lines +999 to 1001
_agent_hash = _hl.md5((name or "agent").encode()).hexdigest()[:8]
session_id = f"history_{_agent_hash}"
_history_session_id = session_id

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action required

1. Unnamed history collision 🐞 Bug ⛨ Security

When history is enabled and no explicit session_id is provided, falling back to the constant string
"agent" makes all unnamed agents compute the same history session_id, causing them to load/append to
the same persisted chat history. This can mix conversations between unrelated agents and leak prior
prompts/responses into new runs.
Agent Prompt
### Issue description
`history=True` with no explicit `session_id` currently auto-generates a session id by hashing `(name or "agent")`. When `name` is `None` (a supported state), this becomes a constant session id shared by all unnamed agents, causing persisted history mixing / leakage.

### Issue Context
- The history branch executes only when `_history_enabled` is true and both `session_id` and `_history_session_id` are unset.
- The generated `session_id` is used as the key for JSON session persistence and for injecting past chat history into subsequent prompts.

### Fix Focus Areas
- Ensure unnamed agents do **not** converge on a single constant history session id.
- Prefer a unique value when `name` is missing (e.g., `uuid.uuid4().hex` or an already-available per-instance `agent_id`).
- Keep deterministic behavior for explicitly named agents.

- src/praisonai-agents/praisonaiagents/agent/agent.py[988-1001]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/praisonai-agents/praisonaiagents/agent/agent.py`:
- Around line 997-1001: The current logic collapses all unnamed agents into one
persisted history session by using (name or "agent") when computing _agent_hash;
instead, when name is None use a stable-but-unique identifier per agent instance
(e.g., incorporate the agent object's id or generate a uuid4) rather than the
constant "agent". Update the block that sets session_id/_history_session_id (the
variables _history_enabled, session_id, _history_session_id and the md5-based
_agent_hash) so that if name is None you derive the hash from a unique token
(like id(self) or a new uuid) combined with any existing identifying info, then
compute the md5 and set session_id = f"history_{_agent_hash}" to avoid unrelated
unnamed agents sharing the same history file.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 86a49c6d-686b-4c67-a296-9f6b19ffaae4

📥 Commits

Reviewing files that changed from the base of the PR and between 16f9325 and 02b9c64.

📒 Files selected for processing (1)
  • src/praisonai-agents/praisonaiagents/agent/agent.py

Comment on lines 997 to 1001
elif _history_enabled and session_id is None and _history_session_id is None:
import hashlib as _hl
_agent_hash = _hl.md5(name.encode()).hexdigest()[:8]
_agent_hash = _hl.md5((name or "agent").encode()).hexdigest()[:8]
session_id = f"history_{_agent_hash}"
_history_session_id = session_id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Don't collapse all unnamed agents into one persisted history session.

(name or "agent") avoids the None.encode() crash, but it also makes every instructions-only agent hash to the same history_<hash>. src/praisonai-agents/tests/unit/agents/test_agent_name_none.py:16-23 documents that name=None is a normal state here, and src/praisonai-agents/praisonaiagents/session/store.py:236-270 uses session_id as the storage key, so unrelated unnamed agents will now read/write the same history file.

Suggested fix
         elif _history_enabled and session_id is None and _history_session_id is None:
             import hashlib as _hl
-            _agent_hash = _hl.md5((name or "agent").encode()).hexdigest()[:8]
+            _session_seed = name or instructions or role or goal or backstory or "agent"
+            _agent_hash = _hl.md5(_session_seed.encode()).hexdigest()[:8]
             session_id = f"history_{_agent_hash}"
             _history_session_id = session_id
🧰 Tools
🪛 Ruff (0.15.4)

[error] 999-999: Probable use of insecure hash functions in hashlib: md5

(S324)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/praisonai-agents/praisonaiagents/agent/agent.py` around lines 997 - 1001,
The current logic collapses all unnamed agents into one persisted history
session by using (name or "agent") when computing _agent_hash; instead, when
name is None use a stable-but-unique identifier per agent instance (e.g.,
incorporate the agent object's id or generate a uuid4) rather than the constant
"agent". Update the block that sets session_id/_history_session_id (the
variables _history_enabled, session_id, _history_session_id and the md5-based
_agent_hash) so that if name is None you derive the hash from a unique token
(like id(self) or a new uuid) combined with any existing identifying info, then
compute the md5 and set session_id = f"history_{_agent_hash}" to avoid unrelated
unnamed agents sharing the same history file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant