Skip to content

fix: LLM backend async api_base and enhance agent observation robustness#96

Open
uday-codes69 wants to merge 4 commits intomesa:mainfrom
uday-codes69:fix-llm-obs-and-async-backend
Open

fix: LLM backend async api_base and enhance agent observation robustness#96
uday-codes69 wants to merge 4 commits intomesa:mainfrom
uday-codes69:fix-llm-obs-and-async-backend

Conversation

@uday-codes69
Copy link
Contributor

Overview

This PR addresses several critical issues in the mesa-llm library to improve backend flexibility and the accuracy of agent-based modeling simulations.


🛠️ Changes Implemented

1. Fixed ModuleLLM Async API Base Bug

Problem:
The agenerate (async) method was ignoring the api_base parameter, preventing local providers such as Ollama from working correctly in async simulations.

Fix:
Updated agenerate to correctly pass api_base to the litellm.acompletion call.


2. Enhanced Observation Logic for LLMAgent

ContinuousSpace Support

Improved neighbor detection for agents using ContinuousSpace, ensuring compatibility with both:

  • model.grid
  • model.space

Vision Radius for Cell-Grids

Implemented proper multi-radius search for:

  • OrthogonalMooreGrid
  • OrthogonalVonNeumannGrid

Previously, these only supported distance-1 neighbors.
Now, agents correctly respect their configured vision parameter.


3. Improved Message Formatting

Updated ModuleLLM.get_messages to natively handle pre-formatted message dictionaries.

This allows users to:

  • Pass full conversation histories
  • Use structured prompts
  • Provide complex multi-turn interactions

4. Expanded Test Coverage

Added new test cases to:

  • tests/test_module_llm.py
  • tests/test_llm_agent.py

These tests verify:

  • Async backend support
  • Proper handling of api_base
  • ContinuousSpace observation logic
  • Multi-radius vision behavior

- Fixed hardcoded vision radius in LLMAgent._build_observation
- Fixed missing neighbor detection logic for ContinuousSpace in LLMAgent
- Updated ModuleLLM return type hints to match litellm's ModelResponse
- Added InternalServerError and ServiceUnavailableError to retryable exceptions
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 26, 2026

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4fa26c4d-5303-4234-ad61-c8475a57a520

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

- inbuilt_tools: fix TypeError in teleport_to_location for ContinuousSpace
  move_agent() is an instance method; passing agent.model.space as the
  first argument caused it to be bound twice. Fixed to agent.model.space.move_agent(agent, pos).

- inbuilt_tools: fix variable shadowing in speak_to return string
  The list comprehension '[agent.unique_id for agent in listener_agents]'
  shadowed the outer 'agent' function parameter, returning the last
  listener's id instead of the sender's. Renamed loop var to 'la'.

- st_memory: fix potential IndexError and unbound variable in process_step
  Accessing self.short_term_memory[-1] without first checking emptiness
  raises IndexError when post_step is called without a prior pre_step.
  Also guarded the display call with 'new_entry is not None' to prevent
  an UnboundLocalError when the elif branch is not taken.

- rewoo: add missing ttl and obs parameters to plan() and aplan()
  ReWOOReasoning.plan() was missing the 'ttl' parameter required by the
  Reasoning ABC, and aplan() was missing both 'obs' and 'ttl', silently
  breaking callers that pass these by keyword or position.

- __init__: remove duplicate entries in __all__
  'step_agents_parallel' and 'step_agents_parallel_sync' were each listed
  twice in __all__, causing redundancy and potential confusion for tooling
  that relies on the exported names list.

All 178 tests pass.
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.

2 participants