[BUG] Agent.copy() shares the LLM's additional_params and token usage
メンテナーはふだん 1 日以内に返信
評価
調査の方向性
まず Agent.copy() と issue に記載された LLM のコピー処理を特定し、既存の agent コピーのテストを読んでください。agent をコピーし、コピー側の additional_params と _token_usage を変更しても元の値が変わらず、HTTP クライアントが共有されたままであることを確認する回帰テストを追加してください。回帰テストが通れば完了です。
索引モデルが issue の本文から書いたものです。
説明
Description
Agent.copy() documents itself as a deep copy, but it copies the LLM with copy.copy. The two agents get different LLM objects that still share additional_params and _token_usage. Mutating either dict on the copy changes the original agent.
Steps to Reproduce
- Build an Agent with an LLM instance.
- Set llm.additional_params["bucket"] to an empty list.
- Call agent.copy().
- Append 1 to the copy's additional_params["bucket"] and add 5 to the copy's _token_usage["total_tokens"].
- Read those two values on the original agent.
Expected behavior
The original agent's bucket stays [] and its total_tokens stays 0. The copy has its own additional_params and _token_usage dicts.
Screenshots/Code snippets
from crewai import Agent, LLM
llm = LLM(model="gpt-4o-mini")
llm.additional_params["bucket"] = []
original = Agent(
role="test role",
goal="test goal",
backstory="test backstory",
llm=llm,
)
copied = original.copy()
copied.llm.additional_params["bucket"].append(1)
copied.llm._token_usage["total_tokens"] += 5
assert copied.llm is not original.llm
assert original.llm.additional_params["bucket"] == []
assert original.llm._token_usage["total_tokens"] == 0
Operating System
Windows 11
Python Version
3.12
crewAI Version
1.15.44
crewAI Tools Version
N/A
Virtual Environment
Venv
Evidence
On current main the script fails after printing:
llm identical False
params identical True
usage identical True
original bucket [1]
original tokens 5
copy.copy creates a new LLM object but does not copy its mutable dicts. additional_params and _token_usage stay shared with the original. The HTTP client is intentionally left shared, because it holds a connection pool and locks that deepcopy cannot clone after the first request.
Possible Solution
After the shallow copy, deepcopy additional_params and _token_usage onto the copied LLM. Leave the client in place.
Additional context
A regression test is test_agent_copy_isolates_llm_mutable_state.
- 主要言語
- Python
- スター
- 60.9k
- フォーク
- 9.2k
- 平均マージ
- 1日 9時間
- マージ済み PR(30日)
- 226
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
crewAIInc/crewAI のほかの issue
-
bug
難易度 1/5 1時間未満 初心者へのやさしさ 89/100
メンテナーはふだん 1 日以内に返信
-
[BUG] CrewJSONEncoder saves enums as "Status.DONE" instead of their value対応中かも @nurlanmnn が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
[FEATURE] Document a single-agent workflow with skills対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 67/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
メンテナーはふだん 1 日以内に返信
-
feature-request
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
crewAIInc/crewAI#9200 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
crewAIInc/crewAI の issue をすべて見る
似ている issue
-
Implement parse_url and accept http URLs for CPAN backend対応中かも @patelshivani2283-lab が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 83/100
fedora-infra/anitya#2203 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
Kaggle/kaggle-cli#1254 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
jingyaogong/minimind#947 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
berntpopp/litvar-link#173 ·
メンテナーはふだん 1 日以内に返信