Skip to content

Support composite arg type parsing in dspy.Tool #8095

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

Merged
merged 3 commits into from
Apr 24, 2025

Conversation

chenmoneygithub
Copy link
Collaborator

Currently our arg parsing cannot handle composite type like list[MyPydanticModel], so we make this PR to resolve the issue.

The solution is creating a pydantic wrapper with one single dummy field value of the arg type, and utilize pydantic model_validate() to convert the LLM-generated args (json format) into the right type.

@@ -159,3 +159,17 @@ def foo(x=100):
tool = Tool(foo)
assert tool.args["x"]["default"] == 100
assert not hasattr(tool.args["x"], "type")


def test_tool_call_parses_args():
Copy link
Collaborator

@TomeHirata TomeHirata Apr 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Can we also add a unit test for the nested type like list[DummyModel]?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call!

}
}

result = tool(**args)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: What are the types we officially support as tool-callable for ReAct? Python primitives + Pydantic?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, everything supported by typing + pydantic

@okhat
Copy link
Collaborator

okhat commented Apr 23, 2025

LGTM, thank you @chenmoneygithub !! Thanks for the review @TomeHirata !

Copy link
Collaborator

@TomeHirata TomeHirata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@okhat okhat merged commit c498de5 into stanfordnlp:main Apr 24, 2025
3 checks passed
@chenmoneygithub chenmoneygithub deleted the tool-calling-parsing branch April 25, 2025 21:48
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.

3 participants