Skip to content

Conversation

@20ns
Copy link

@20ns 20ns commented Jul 22, 2025

Summary

This PR resolves issue #28848 by adding bind_tools() support to RunnableSequence, enabling the combination of structured output and tool binding.

Problem

Users couldn't combine with_structured_output() and bind_tools() because:

  • with_structured_output() returns a RunnableSequence
  • RunnableSequence lacked a bind_tools() method
  • This prevented essential AI application patterns that need both structured output (JSON responses) and tool calling (external functions)

Solution

Added an intelligent bind_tools() method to RunnableSequence that:

  • Detects structured output patterns (Model < /dev/null | Parser)
  • Delegates tool binding to the underlying ChatModel
  • Preserves the existing sequence structure
  • Provides helpful error messages for unsupported cases

Code Example

# This now works (was failing before):
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
structured_llm = llm.with_structured_output(ResponseModel)  
llm_with_tools = structured_llm.bind_tools([a_tool])  # ✅ Success!

Technical Details

  • File: libs/core/langchain_core/runnables/base.py
  • Method: Added bind_tools() to RunnableSequence class
  • Strategy: Detect 2-step sequences, bind tools to first step if it supports bind_tools()
  • Compatibility: Fully backward compatible, no breaking changes

Testing

Impact

This enables critical AI application patterns like:

  • ChatGPT-style apps with rich UI responses (structured output)
  • Apps that also need external function calls (tools)
  • Previously impossible workflow combinations now work seamlessly

Fixes #28848

… 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]>
@20ns 20ns requested a review from eyurtsev as a code owner July 22, 2025 16:51
@vercel
Copy link

vercel bot commented Jul 22, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchain ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 22, 2025 5:34pm

@codspeed-hq
Copy link

codspeed-hq bot commented Jul 22, 2025

CodSpeed WallTime Performance Report

Merging #32175 will not alter performance

Comparing 20ns:master (d24af2b) with master (10996a2)

⚠️ 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

- 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.
@codspeed-hq
Copy link

codspeed-hq bot commented Jul 22, 2025

CodSpeed Instrumentation Performance Report

Merging #32175 will not alter performance

Comparing 20ns:master (d24af2b) with master (10996a2)

Summary

✅ 14 untouched benchmarks

bansalkanav and others added 5 commits July 22, 2025 18:06
…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.
@mdrxy
Copy link
Collaborator

mdrxy commented Jul 22, 2025

Could you re-submit this without updating any lockfiles are creating new test files? You will need to pull the latest changes from the repo.

@mdrxy mdrxy closed this Jul 22, 2025
@20ns
Copy link
Author

20ns commented Jul 22, 2025

Could you re-submit this without updating any lockfiles are creating new test files? You will need to pull the latest changes from the repo.

Done

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.

openai: bind_tools not callable after with_structured_output

3 participants