🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
fix: handle empty xAI streams without crashing (#4015)
Junghwan7583e0d62333
diff --git a/camel/models/xai_model.py b/camel/models/xai_model.pyindex b5b2d68ce..b0c853d1e 100644--- a/camel/models/xai_model.py+++ b/camel/models/xai_model.py@@ -523,6 +523,7 @@ class XAIModel(BaseModelBackend):"""chunk_id = ""model = str(self.model_type)+ response = Nonetool_calls_emitted = Falsefor response, chunk in stream_iter:chunk_id = chunk_id or (response.id if response.id else "")@@ -558,7 +559,7 @@ class XAIModel(BaseModelBackend):)# After stream ends, store state from the accumulated response- if response:+ if response is not None:encrypted = response.encrypted_contentreasoning = response.reasoning_contentif encrypted:diff --git a/test/models/test_xai_model.py b/test/models/test_xai_model.pynew file mode 100644index 000000000..828f5d0cb--- /dev/null+++ b/test/models/test_xai_model.py@@ -0,0 +1,28 @@+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========++from types import MethodType++from camel.models.xai_model import XAIModel+++def test_stream_to_chunks_handles_empty_stream():+ model = XAIModel.__new__(XAIModel)+ model.model_type = "grok-3"+ model._last_encrypted_content = None+ model._last_reasoning_content = None+ model._save_response_chain = lambda *_args, **_kwargs: None+ model._make_chunk = MethodType(lambda self, **kwargs: kwargs, model)++ assert list(XAIModel._stream_to_chunks(model, iter([]), 0)) == []