Skip to content

Conversation

20ns
Copy link

@20ns 20ns commented Aug 31, 2025

Description

This PR fixes issue #32671 where RunnableConfig was not being passed to tools when using AgentExecutor. Tools were receiving None instead of the proper configuration object, preventing them from accessing important context like session IDs, tags, and metadata.

Problem

When using AgentExecutor with tools that accept a config parameter, the tools would always receive None instead of the RunnableConfig passed to the executor. This prevented configuration-aware tool execution and limited the ability to maintain context across tool calls.

Before:

@tool
def my_tool(input: str, config: RunnableConfig = None) -> str:
    print(f"Config: {config}")  # Always printed "Config: None"
    return "result"

# config was not passed through to tools
agent_executor.invoke({"input": "test"}, config={"tags": ["session-1"]})

After:

@tool  
def my_tool(input: str, config: RunnableConfig = None) -> str:
    print(f"Config: {config}")  # Now properly receives config!
    return "result"

# config is now properly passed to tools
agent_executor.invoke({"input": "test"}, config={"tags": ["session-1"]})

Changes

1. Enhanced Chain Base Class (chains/base.py)

  • Modified invoke() and ainvoke() methods to detect config parameter support using inspect.signature
  • Updated abstract method signatures for _call() and _acall() to include optional config parameter
  • Added proper documentation for the config parameter

2. Updated AgentExecutor (agents/agent.py)

  • Added config parameter to all methods in the execution call chain:
    • _call() and _acall()
    • _take_next_step() and _atake_next_step()
    • _iter_next_step() and _aiter_next_step()
    • _perform_agent_action() and _aperform_agent_action()
  • Modified both sync and async tool execution to pass config to tool.run() and tool.arun()

Testing

  • ✅ Comprehensive validation of config propagation through entire call chain
  • ✅ Verified both sync and async execution paths work correctly
  • ✅ Confirmed backward compatibility - no breaking changes
  • ✅ Validated Chain inheritance patterns remain intact
  • ✅ Linting and formatting checks pass
  • ✅ Added test cases to verify config is properly received by tools

Impact

  • Enables configuration-aware tool execution - tools can now access session context, tags, and metadata
  • Maintains full backward compatibility - existing code continues to work unchanged
  • No performance impact - config is only passed when tools support it
  • Follows LangChain patterns - consistent with other config propagation in the codebase

Example Use Cases

This fix enables important use cases like:

  • Session-aware tools that can maintain context across conversations
  • Tagged execution for monitoring and analytics
  • Metadata propagation for debugging and tracing
  • Configuration-driven tool behavior based on runtime settings

Checklist

  • Breaking Changes: Verified no public API changes
  • Type Hints: All functions have complete type annotations
  • Tests: New functionality is fully tested
  • Security: No dangerous patterns introduced
  • Documentation: Proper docstrings for modified methods
  • Code Quality: Linting and formatting checks pass
  • Commit Message: Follows Conventional Commits format

Fixes #32671

20ns and others added 15 commits July 22, 2025 17:50
… chains

This resolves issue langchain-ai#28848 where calling bind_tools() on a RunnableSequence
created by with_structured_output() would fail with AttributeError.

The fix enables the combination of structured output and tool binding,
which is essential for modern AI applications that need both:
- Structured JSON output formatting
- External function calling capabilities

**Changes:**
- Added bind_tools() method to RunnableSequence class
- Method intelligently detects structured output patterns
- Delegates tool binding to the underlying ChatModel
- Preserves existing sequence structure and behavior
- Added comprehensive unit tests

**Technical Details:**
- Detects 2-step sequences (Model  < /dev/null |  Parser) from with_structured_output()
- Binds tools to the first step if it supports bind_tools()
- Returns new RunnableSequence with updated model + same parser
- Falls back gracefully with helpful error messages

**Impact:**
This enables previously impossible workflows like ChatGPT-style apps
that need both structured UI responses and tool calling capabilities.

Fixes langchain-ai#28848

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Remove quoted type annotations
- Fix line length violations
- Remove trailing whitespace
- Use double quotes consistently
- Improve error message formatting for better readability

The S110 warnings about try-except-pass are intentional - we want
silent fallback behavior before raising the final helpful error.
…ain-ai#32169)

## **Description:** 
This PR updates the internal documentation link for the RAG tutorials to
reflect the updated path. Previously, the link pointed to the root
`/docs/tutorials/`, which was generic. It now correctly routes to the
RAG-specific tutorial page for the following text-embedding models.

1. DatabricksEmbeddings
2. IBM watsonx.ai
3. OpenAIEmbeddings
4. NomicEmbeddings
5. CohereEmbeddings
6. MistralAIEmbeddings
7. FireworksEmbeddings
8. TogetherEmbeddings
9. LindormAIEmbeddings
10. ModelScopeEmbeddings
11. ClovaXEmbeddings
12. NetmindEmbeddings
13. SambaNovaCloudEmbeddings
14. SambaStudioEmbeddings
15. ZhipuAIEmbeddings

## **Issue:** N/A
## **Dependencies:** None
## **Twitter handle:** N/A
- Replace broad Exception catching with specific exceptions (AttributeError, TypeError, ValueError)
- Add proper type annotations to test functions and variables
- Add type: ignore comments for dynamic method assignment in tests
- Fix line length violations and formatting issues
- Ensure all MyPy checks pass

All lint checks now pass successfully. The S110 warnings are resolved
by using more specific exception handling instead of bare try-except-pass.
- Remove test_bind_tools_fix.py
- Remove test_real_example.py
- Remove test_sequence_bind_tools.py

These test files were created during development but should not be in the root directory.
The actual fix for issue langchain-ai#28848 (RunnableSequence.bind_tools) is already implemented in core.
pulling from the updated branch
- Add fallback mechanism in _create_chat_result to handle cases where
  OpenAI client's model_dump() returns choices as None even when the
  original response object contains valid choices data
- This resolves TypeError: 'Received response with null value for choices'
  when using vLLM with LangChain-OpenAI integration
- Add comprehensive test suite to validate the fix and edge cases
- Maintain backward compatibility for cases where choices are truly unavailable
- Fix addresses GitHub issue langchain-ai#32252

The issue occurred because some OpenAI-compatible APIs like vLLM return
valid response objects, but the OpenAI client library's model_dump() method
sometimes fails to properly serialize the choices field, returning None
instead of the actual choices array. This fix attempts to access the choices
directly from the response object when model_dump() fails.
- Add fallback mechanism in _create_chat_result to handle cases where
  OpenAI client's model_dump() returns choices as None even when the
  original response object contains valid choices data
- This resolves TypeError: 'Received response with null value for choices'
  when using vLLM with LangChain-OpenAI integration
- Add comprehensive test suite to validate the fix and edge cases
- Maintain backward compatibility for cases where choices are truly unavailable
- Fix addresses GitHub issue langchain-ai#32252

The issue occurred because some OpenAI-compatible APIs like vLLM return
valid response objects, but the OpenAI client library's model_dump() method
sometimes fails to properly serialize the choices field, returning None
instead of the actual choices array. This fix attempts to access the choices
directly from the response object when model_dump() fails.
fix(openai): resolve vLLM compatibility issue with ChatOpenAI (langchain-ai#32252)

More details can be read on this thread.
Fixes langchain-ai#32671 by modifying AgentExecutor to properly propagate RunnableConfig
through the entire execution chain to tools.

Changes:
- Enhanced Chain.invoke() and Chain.ainvoke() to detect and pass config parameter
- Updated AgentExecutor._call() and _acall() to accept config parameter
- Modified all intermediate methods to propagate config: _take_next_step,
  _atake_next_step, _iter_next_step, _aiter_next_step, _perform_agent_action,
  _aperform_agent_action
- Added config parameter to tool.run() and tool.arun() calls in both sync and async paths
- Added comprehensive test case to verify config propagation works correctly

The fix ensures tools receive the RunnableConfig parameter instead of None,
enabling proper configuration-aware tool execution in AgentExecutor workflows.
- Fixed line length issue in Chain.ainvoke()
- Updated abstract method signatures to include config parameter
- Added proper documentation for config parameter in docstrings
@20ns 20ns requested a review from eyurtsev as a code owner August 31, 2025 23:37
Copy link

vercel bot commented Aug 31, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
langchain Ignored Ignored Preview Aug 31, 2025 11:48pm

Copy link

codspeed-hq bot commented Aug 31, 2025

CodSpeed WallTime Performance Report

Merging #32773 will not alter performance

Comparing 20ns:fix/issue-32671-config-not-passed-to-tools (666f2da) with master (6b5fdfb)

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

Summary

✅ 13 untouched benchmarks

@20ns 20ns changed the title fix(agents): Pass config to tools in AgentExecutor agents: pass config to tools in AgentExecutor Aug 31, 2025
Copy link

codspeed-hq bot commented Aug 31, 2025

CodSpeed Instrumentation Performance Report

Merging #32773 will not alter performance

Comparing 20ns:fix/issue-32671-config-not-passed-to-tools (666f2da) with master (6b5fdfb)

Summary

✅ 14 untouched benchmarks

@20ns 20ns closed this Aug 31, 2025
@20ns 20ns reopened this Aug 31, 2025
@mdrxy mdrxy changed the title agents: pass config to tools in AgentExecutor agents: pass config to tools in AgentExecutor Sep 3, 2025
@mdrxy mdrxy added integration Related to a provider partner package integration core Related to the package `langchain-core` langchain Related to the package `langchain` labels Sep 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Related to the package `langchain-core` integration Related to a provider partner package integration langchain Related to the package `langchain`
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Config not passed to tools when using AgentExecutor
3 participants