Repository navigation
fix(dspy): accept LangChain tools with JSON schema args #10621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
fd9aac9
4468587
dc14339
8cad034
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ | |
|
|
||
| def convert_langchain_tool(tool: "BaseTool") -> Tool: | ||
| """Build a DSPy tool from a LangChain tool. | ||
|
|
||
| This function converts a LangChain tool (either created with @tool decorator | ||
| or by subclassing BaseTool) into a DSPy Tool. | ||
|
|
||
|
|
@@ -29,13 +29,15 @@ async def func(**kwargs): | |
| # Get args_schema from the tool | ||
| # https://python.langchain.com/api_reference/core/tools/langchain_core.tools.base.BaseTool.html#langchain_core.tools.base.BaseTool.args_schema | ||
| args_schema = tool.args_schema | ||
| args, _, arg_desc = convert_input_schema_to_tool_args(args_schema.model_json_schema()) | ||
|
|
||
| # The args_schema of Langchain tool is a pydantic model, so we can get the type hints from the model fields | ||
| arg_types = { | ||
| key: field.annotation if field.annotation is not None else Any | ||
| for key, field in args_schema.model_fields.items() | ||
| } | ||
| if isinstance(args_schema, dict): | ||
| args, arg_types, arg_desc = convert_input_schema_to_tool_args(args_schema) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The dictionary path does not preserve the schema's Artifacts
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed both requiredness mismatches independently. dc14339 preserves a detached copy of the original dict schema for native advertisement and whole-argument validation. required-with-default stays required; optional-without-default stays optional; missing required and forbidden additional properties reject before ainvoke. Existing function/Pydantic schema inference stays unchanged. Regression asserts exact advertised schema and successful invocation only for valid arguments. Full corrective diff reviewed before push; remote acceptance still pending. |
||
| else: | ||
| args, _, arg_desc = convert_input_schema_to_tool_args(args_schema.model_json_schema()) | ||
| # Preserve model annotations for nested Pydantic arguments. | ||
| arg_types = { | ||
| key: field.annotation if field.annotation is not None else Any | ||
| for key, field in args_schema.model_fields.items() | ||
| } | ||
|
|
||
| return Tool( | ||
| func=func, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.