Skip to content

Agent Zero AI framework

Preserve non-LLM mode when reloading result metadata

Alessandroc289a4359762
diff --git a/helpers/llm_result.py b/helpers/llm_result.py
index ab067db54..033371b40 100644
--- a/helpers/llm_result.py
+++ b/helpers/llm_result.py
@@ -67,6 +67,7 @@ class LLMResult:
@classmethod
def from_dict(cls, data: dict[str, Any] | None) -> "LLMResult":
data = data or {}
+ mode = data.get("mode")
return cls(
response=str(data.get("response") or ""),
reasoning=str(data.get("reasoning") or ""),
@@ -77,7 +78,7 @@ class LLMResult:
ResponseItem.from_any(item) for item in data.get("output_items") or []
],
provider_model_key=str(data.get("provider_model_key") or ""),
- mode=str(data.get("mode") or "responses"),
+ mode=str(mode if mode is not None else "responses"),
state=str(data.get("state") or "provider"),
usage=object_to_dict(data.get("usage") or {}),
raw=object_to_dict(data.get("raw") or {}),
diff --git a/helpers/llm_result.py.dox.md b/helpers/llm_result.py.dox.md
index aceaa83d1..09f81c55c 100644
--- a/helpers/llm_result.py.dox.md
+++ b/helpers/llm_result.py.dox.md
@@ -19,6 +19,7 @@
- `LLMResult.metadata()` stores only durable provider state under `RESPONSE_METADATA_KEY`: response IDs, structured output items, provider/mode/state, usage, and capability data. Runtime prompt inputs, raw responses, and duplicated response/reasoning text are not persisted in history.
- `from_response(...)` must preserve provider `response_id`, `previous_response_id`, raw output items, usage, and capability metadata.
+- `from_dict(...)` preserves explicit modes, including the empty non-LLM sentinel; only missing or null modes use the legacy `responses` default.
- `from_chat(...)` must produce an equivalent chat-completions result with `mode="chat_completions"` and `state="off"`, preserving optional function-call output items and provider usage when the chat transport supplies them.
- Function-call output items must preserve `call_id` and optional acknowledged safety checks.
- Argument parsing must tolerate JSON strings, dictionaries, and malformed values without throwing.
diff --git a/tests/test_responses_architecture.py b/tests/test_responses_architecture.py
index 02fa880a7..87ea15264 100644
--- a/tests/test_responses_architecture.py
+++ b/tests/test_responses_architecture.py
@@ -60,6 +60,21 @@ def test_responses_function_call_text_preserves_non_ascii_tool_args():
assert result.function_calls_text() == '{"tool_name": "response", "tool_args": {"text": "привет"}}'
[email protected]("mode", ["", "chat_completions", "responses", "custom"])
+def test_llm_result_round_trip_preserves_explicit_mode(mode):
+ result = LLMResult.non_llm() if not mode else LLMResult(
+ mode=mode, response_id="resp_1", previous_response_id="resp_0",
+ usage={"input_tokens": 10},
+ )
+ assert LLMResult.from_dict(result.to_dict()) == result
+ assert result_from_metadata(result.metadata()).metadata() == result.metadata()
+
+
[email protected]("data", [None, {}, {"mode": None}])
+def test_llm_result_missing_mode_keeps_legacy_default(data):
+ assert LLMResult.from_dict(data).mode == "responses"
+
+
def test_llm_result_persists_only_durable_responses_metadata():
result = LLMResult.from_response(
{