Skip to content

perf: guard any_to_str() with isinstance check in AgentRearrange#1492

Open
GopalGB wants to merge 1 commit intokyegomez:masterfrom
GopalGB:perf/guard-any-to-str-1462
Open

perf: guard any_to_str() with isinstance check in AgentRearrange#1492
GopalGB wants to merge 1 commit intokyegomez:masterfrom
GopalGB:perf/guard-any-to-str-1462

Conversation

@GopalGB
Copy link
Copy Markdown

@GopalGB GopalGB commented Mar 21, 2026

Summary

Skip redundant any_to_str() call in _run_sequential_workflow when agent.run() already returns a string (the common path).

Changes

  • Added isinstance(current_task, str) guard before any_to_str() call at line 577
  • Near-zero cost check that avoids function call overhead on every agent step

Before

current_task = agent.run(task=self.conversation.get_str(), img=img)
current_task = any_to_str(current_task)  # Always called, even when already str

After

current_task = agent.run(task=self.conversation.get_str(), img=img)
if not isinstance(current_task, str):
    current_task = any_to_str(current_task)  # Only when needed

Test plan

  • Existing tests pass unchanged (behavior is identical)
  • any_to_str() still called for non-string returns (dicts, lists, objects)
  • String returns skip the redundant conversion

Fixes #1462


📚 Documentation preview 📚: https://swarms--1492.org.readthedocs.build/en/1492/

Skip redundant any_to_str() function call when agent.run() already
returns a string (the common case). The isinstance check is near-zero
cost and avoids unnecessary type inspection overhead on every agent step.

Fixes kyegomez#1462
@github-actions
Copy link
Copy Markdown

Hello there, thank you for opening an PR ! 🙏🏻 The team was notified and they will get back to you asap.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[perf] Drop redundant any_to_str() calls in AgentRearrange

1 participant