langchain-ai/langchain

openai: `bind_tools` not callable after `with_structured_output`

Open

#28.848 aperta il 20 dic 2024

Vedi su GitHub
 (15 commenti) (4 reazioni) (1 assegnatario)Python (22.617 fork)batch import
externalhelp wantedinvestigateopenai

Metriche repository

Star
 (136.758 star)
Metriche merge PR
 (Merge medio 10g 2h) (288 PR mergiate in 30 g)

Descrizione

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

from langchain_openai import ChatOpenAI
from pydantic import BaseModel, Field
from langchain.tools import StructuredTool

class ResponseModel(BaseModel):
  a_value:str = Field(description="This doesn't matter much")

def a_func(val: int):
    return True

a_tool = StructuredTool.from_function(
            func=a_func,
            name="A func",
            description="A function you will need",
        )

llm = ChatOpenAI(model="gpt-4o-mini",temperature=0)
structured_llm = llm.with_structured_output(ResponseModel)
llm_with_tools = structured_llm.bind_tools([a_tool]) <----- not available

Error Message and Stack Trace (if applicable)

'RunnableSequence' object has no attribute 'bind_tools'

Description

I am attempting to retrieved structured output in a json format (to pass via an api to a frontend), and I also require calling out to tools. I cannot figure out how to combine the two, or there is an issue with code to do so.

System Info

System Information

OS: Darwin OS Version: Darwin Kernel Version 24.1.0: Thu Oct 10 21:02:27 PDT 2024; root:xnu-11215.41.3~2/RELEASE_X86_64 Python Version: 3.13.1 (main, Dec 3 2024, 17:59:52) [Clang 16.0.0 (clang-1600.0.26.4)]

Package Information

langchain_core: 0.3.28 langchain: 0.3.13 langchain_community: 0.3.13 langsmith: 0.2.4 langchain_experimental: 0.3.4 langchain_openai: 0.2.14 langchain_text_splitters: 0.3.4

Optional packages not installed

langserve

Other Dependencies

aiohttp: 3.10.10 async-timeout: Installed. No version info available. dataclasses-json: 0.6.7 httpx: 0.27.2 httpx-sse: 0.4.0 jsonpatch: 1.33 langsmith-pyo3: Installed. No version info available. numpy: 1.26.4 openai: 1.58.1 orjson: 3.10.9 packaging: 24.1 pydantic: 2.9.2 pydantic-settings: 2.6.0 PyYAML: 6.0.2 requests: 2.32.3 requests-toolbelt: 1.0.0 SQLAlchemy: 2.0.36 tenacity: 9.0.0 tiktoken: 0.8.0 typing-extensions: 4.12.2

Guida contributor