Skip to content

added an optional environment parameter to agent.run_batch#205

Open
amilios wants to merge 4 commits into
mainfrom
run_batch_env
Open

added an optional environment parameter to agent.run_batch#205
amilios wants to merge 4 commits into
mainfrom
run_batch_env

Conversation

@amilios
Copy link
Copy Markdown
Collaborator

@amilios amilios commented Mar 21, 2025

Fixed the following things in this patch:

  • enabled agent.run_batch to take an environment parameter
  • fixed the set() issue so that now tapes can end prematurely and not mess up the indices
  • switched from enforcing TrainableLLM to just ensuring the passed LLM has a batch_generate method

Description by Korbit AI

What change is being made?

Add an optional environment parameter to the run_batch method in the Agent class to allow environment integration during parallel batch processing of tapes.

Why are these changes being made?

This change enhances the flexibility of the run_batch method by enabling it to interact with a provided environment, which can modify tapes through its react method. This is necessary for scenarios where the processing of tapes requires interaction with dynamic external conditions or systems.

Is this description stale? Ask me to generate a new description by commenting /korbit-generate-pr-description

@amilios
Copy link
Copy Markdown
Collaborator Author

amilios commented Mar 21, 2025

I see the linter fails due to a type error for Environment. The issue is that importing Environment causes a circular dependency. I can just omit this type check entirely if this is preferred.

Copy link
Copy Markdown

@korbit-ai korbit-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review by Korbit AI

Korbit automatically attempts to detect when you fix issues in new commits.
Category Issue Fix Detected
Functionality Non-deterministic LLM Selection ▹ view
Functionality Missing Stop Check After Environment React ▹ view
Files scanned
File Path Reviewed
tapeagents/agent.py

Explore our documentation to understand the languages and file types we support and the files we ignore.

Need a new review? Comment /korbit-review on this PR and I'll review your latest changes.

Korbit Guide: Usage and Customization

Interacting with Korbit

  • You can manually ask Korbit to review your PR using the /korbit-review command in a comment at the root of your PR.
  • You can ask Korbit to generate a new PR description using the /korbit-generate-pr-description command in any comment on your PR.
  • Too many Korbit comments? I can resolve all my comment threads if you use the /korbit-resolve command in any comment on your PR.
  • On any given comment that Korbit raises on your pull request, you can have a discussion with Korbit by replying to the comment.
  • Help train Korbit to improve your reviews by giving a 👍 or 👎 on the comments Korbit posts.

Customizing Korbit

  • Check out our docs on how you can make Korbit work best for you and your team.
  • Customize Korbit for your organization through the Korbit Console.

Current Korbit Configuration

General Settings
Setting Value
Review Schedule Automatic excluding drafts
Max Issue Count 10
Automatic PR Descriptions
Issue Categories
Category Enabled
Documentation
Logging
Error Handling
Readability
Design
Performance
Security
Functionality

Feedback and Support

Note

Korbit Pro is free for open source projects 🎉

Looking to add Korbit to your team? Get started with a free 2 week trial here

Comment thread tapeagents/agent.py
if environment is not None and (not hasattr(environment, "react") or not callable(environment.react)):
raise ValueError("Environment must have a callable react method")

original_tapes = list(tapes)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-deterministic LLM Selection category Functionality

Tell me more
What is the issue?

The LLM selection logic is not deterministic when there's no 'default' LLM. Using next(iter()) on a dictionary can return any key-value pair.

Why this matters

This non-deterministic behavior could lead to inconsistent results across different runs, especially when batch processing multiple tapes.

Suggested change ∙ Feature Preview

Replace with explicit LLM selection logic that guarantees consistent behavior:

if 'default' in self.llms:
    llm = self.llms['default']
elif len(self.llms) == 1:
    llm = next(iter(self.llms.values()))
else:
    raise ValueError("No default LLM specified and multiple LLMs present")
Provide feedback to improve future suggestions

Nice Catch Incorrect Not in Scope Not in coding standard Other

💬 Looking for more details? Reply to this comment to chat with Korbit.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is false because I check if the length of the llm array is more than 1, so this is unnecessary. this is deterministic.

Comment thread tapeagents/agent.py
Comment on lines +777 to +778

# Update metadata for all tapes
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing Stop Check After Environment React category Functionality

Tell me more
What is the issue?

The environment reaction is applied after each step generation, but it's unclear if steps after the environment reaction should lead to agent termination.

Why this matters

If the environment's reaction creates a state that should stop the agent, this isn't currently checked, potentially leading to unnecessary continued processing.

Suggested change ∙ Feature Preview

Add a stop check after environment reaction:

# Apply environment reactions if environment is provided
if environment is not None:
    tapes[tape_idx] = environment.react(tapes[tape_idx])
    # Check if agent should stop after environment reaction
    if subagent.should_stop(tapes[tape_idx]):
        pending_tape_indices.remove(tape_idx)
        continue
Provide feedback to improve future suggestions

Nice Catch Incorrect Not in Scope Not in coding standard Other

💬 Looking for more details? Reply to this comment to chat with Korbit.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is really relevant, is it possible for the agent to stop immediately after an environment reaction?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You bring up a good point, we may need to consider cases where the agent should stop immediately after an environment reaction, but doesn't. I would suggest adding this logic as @korbit-ai advised. Although the conditions under which the agent would need to stop after an environment reaction are still ambiguous, I believe adding this validation check increases the robustness of our code. What do you think?

@AlexPiche AlexPiche requested a review from NicolasAG April 3, 2025 18:36
Copy link
Copy Markdown
Collaborator

@NicolasAG NicolasAG left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it is really useful! I never used batch_run() in my agents yet so I can't test if it breaks anything the previous batch_run() function did.

The main weak point I see is that for this to work, the Environments need to be state-LESS. Otherwise executing multiple tapes in parallel may conflict the internal state of the environment.

Potential solution: take as input a list of environments, 1 per tape. If not, have a warning message saying that we will assume the environment is StateLESS.
Don't know if it's worth doing in this PR or in another one. @AlexPiche @rafapi

@rizar
Copy link
Copy Markdown
Contributor

rizar commented May 26, 2025

I think we are moving to using async Python for many-agent orchestration, folks. See e.g. #225 .

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants