[FIX][Conversation outputting metadata information for no reason]

pull/1155/head
Kye Gomez 3 days ago
parent b79ded8aa5
commit 5e35bda4b3

@ -1,4 +1,5 @@
from swarms import Agent
import json
# Initialize the agent
agent = Agent(
@ -8,10 +9,13 @@ agent = Agent(
dynamic_temperature_enabled=True,
max_loops=1,
dynamic_context_window=True,
streaming_on=True,
streaming_on=False,
top_p=None,
output_type="dict",
)
out = agent.run(
task="What are the top five best energy stocks across nuclear, solar, gas, and other energy sources?",
n=1,
)
print(json.dumps(out, indent=4))

@ -1,6 +1,5 @@
import concurrent.futures
import datetime
import inspect
import json
import os
import traceback
@ -95,6 +94,7 @@ class Conversation:
export_method: str = "json",
dynamic_context_window: bool = True,
caching: bool = True,
output_metadata: bool = False,
):
# Initialize all attributes first
@ -118,6 +118,7 @@ class Conversation:
self.export_method = export_method
self.dynamic_context_window = dynamic_context_window
self.caching = caching
self.output_metadata = output_metadata
if self.name is None:
self.name = id
@ -534,7 +535,7 @@ class Conversation:
"""
return self.return_history_as_string()
def to_dict(self) -> Dict[str, Any]:
def to_dict(self) -> Dict[Any, Any]:
"""
Converts all attributes of the class into a dictionary, including all __init__ parameters
and conversation history. Automatically extracts parameters from __init__ signature.
@ -544,43 +545,7 @@ class Conversation:
- metadata: All initialization parameters and their current values
- conversation_history: The list of conversation messages
"""
# Get all parameters from __init__ signature
init_signature = inspect.signature(self.__class__.__init__)
init_params = [
param
for param in init_signature.parameters
if param not in ["self", "args", "kwargs"]
]
# Build metadata dictionary from init parameters
metadata = {}
for param in init_params:
# Get the current value of the parameter from instance
value = getattr(self, param, None)
# Special handling for certain types
if value is not None:
if isinstance(
value, (str, int, float, bool, list, dict)
):
metadata[param] = value
elif hasattr(value, "to_dict"):
metadata[param] = value.to_dict()
else:
try:
# Try to convert to string if not directly serializable
metadata[param] = str(value)
except Exception:
# Skip if we can't serialize
continue
# Add created_at if it exists
if hasattr(self, "created_at"):
metadata["created_at"] = self.created_at
return {
"metadata": metadata,
"conversation_history": self.conversation_history,
}
return self.conversation_history
def save_as_json(self, force: bool = True):
"""Save the conversation history and metadata to a JSON file.

@ -452,7 +452,9 @@ def test_add_agents_batch_with_custom_parameters(
)
def test_add_agents_batch_empty_list(real_agents, aop_instance, mock_fastmcp):
def test_add_agents_batch_empty_list(
real_agents, aop_instance, mock_fastmcp
):
"""Test adding empty agents list raises ValueError."""
with patch.object(aop_instance, "_register_agent_discovery_tool"):
with pytest.raises(

Loading…
Cancel
Save