Files
langchaingo/FIXES_SUMMARY.md
Travis Cline 9d4df6f942 agents: improve robustness of MRKL and OpenAI Functions agents (#1362)
* agents: improve robustness of MRKL and OpenAI Functions agents

Enhance agent implementations with more flexible output parsing and better tool handling:

- Add case-insensitive "Final Answer" detection with multiple variations
- Improve regex patterns for action/input extraction in MRKL agent
- Refactor OpenAI Functions agent to properly handle parallel tool calls
- Enhance tool response grouping and formatting in conversation history
- Fix whitespace handling in agent outputs

These changes make both agents more resilient to variations in model outputs.

* agents: fix linting issues

- Remove unused mrkl_improved.go file
- Fix gofmt formatting in mrkl.go and openai_functions_agent.go
- Clean up whitespace inconsistencies
2025-08-16 19:43:35 +02:00

3.5 KiB

High Priority Bug Fixes Summary

Overview

This branch contains fixes for three high-priority issues affecting the langchaingo agents system.

Fixed Issues

1. Agent Executor Max Iterations Bug (#1225)

Problem: Agents using models like llama2/llama3 would not finish before reaching max iterations, even when they had the answer.

Root Cause: The MRKL agent's parseOutput function was too strict in looking for exactly "Final Answer:" which some models don't consistently generate.

Fix: Enhanced the parseOutput function in agents/mrkl.go to:

  • Accept case-insensitive variations of "final answer"
  • Recognize alternative phrases like "the answer is:" and "answer:"
  • Support flexible spacing and punctuation
  • Maintain backward compatibility with the original format

Files Modified:

  • agents/mrkl.go - Enhanced parseOutput function
  • agents/executor_fix_test.go - Added comprehensive tests

2. OpenAI Functions Agent Multiple Tools Error (#1192)

Problem: The OpenAI Functions Agent would only process the first tool call when multiple tools were invoked, causing errors.

Root Cause: The ParseOutput function only handled choice.ToolCalls[0] instead of iterating through all tool calls.

Fix: Updated the OpenAI Functions Agent to:

  • Process all tool calls in a response, not just the first one
  • Properly group parallel tool calls in the scratchpad
  • Handle multiple tool responses correctly

Files Modified:

  • agents/openai_functions_agent.go - Fixed ParseOutput and constructScratchPad

3. Ollama Agents and Tools Issues (#1045)

Problem: Ollama models would fail when used with agents due to inconsistent output formatting and lack of native function calling support.

Root Cause: Ollama doesn't have native function/tool calling like OpenAI, and models generate responses in various formats.

Fix:

  • Leveraged the improved MRKL parser from fix #1
  • Created comprehensive documentation and best practices
  • Added guidance for prompt engineering with Ollama models

Files Added:

  • agents/ollama_agent_guide.md - Complete usage guide with examples

Testing

Run the test suite with:

chmod +x test_all_fixes.sh
./test_all_fixes.sh

Or run individual tests:

# Test agent executor improvements
go test -v ./agents -run TestImprovedFinalAnswerDetection

# Test OpenAI functions agent
go test -v ./agents -run TestOpenAIFunctionsAgent

# Test full agent suite
go test -race ./agents/...

Impact

These fixes significantly improve the reliability of agents when using:

  • Open-source models via Ollama (llama2, llama3, mistral, etc.)
  • OpenAI models with multiple function calls
  • Any LLM that might have slight variations in output formatting

Backward Compatibility

All fixes maintain full backward compatibility:

  • Original "Final Answer:" format still works
  • Single tool calls work as before
  • Existing tests continue to pass

Recommendations

  1. For Ollama users: Use the guide in ollama_agent_guide.md for best results
  2. For OpenAI users: Multiple tool calls now work seamlessly
  3. General: Consider using lower temperature (0.2-0.3) for more consistent agent behavior

Next Steps

  1. Create individual PRs for each fix
  2. Add integration tests with actual LLM providers
  3. Update documentation with these improvements
  4. Consider adding more agent examples

Code Quality

  • All tests pass
  • Race condition free (go test -race)
  • Maintains backward compatibility
  • Follows Go best practices
  • Well-documented with inline comments