Skip to content
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

prebuilt: add a test for using create_react_agent w/ Pydantic schema and ChatPromptTemplate prompt #3867

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ianchi
Copy link
Contributor

@ianchi ianchi commented Mar 16, 2025

Hi @vbarda, this test is to showcase the problems I was refering to in the other PR. The prebuilt react agent still does not play well with pydantic.

It can be improved with a combination of langchain-ai/langchain#30297 and part of the "hack" in #3559 , or probably some better variant of that.

The problems still present:

  • cannot use a chat prompt template
  • injected state in the tool is inconsistent

Not an error, but "pollutes" the outer app state:

  • forcing to have RemainingStes in the state. This is an implementation detail of the agent, and could be part of a private state. It is not really necessary to be declared in the "public" overall state. As it is a managed variable it exists regardless, it could be just requested by the internal node that needs it, by a proper type annotation. The StateGraph already can solve that, no need for the user to do it.

Copy link

vercel bot commented Mar 16, 2025

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

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langgraph-docs-preview ⬜️ Ignored (Inspect) Visit Preview Mar 16, 2025 7:35pm

@vbarda vbarda changed the title error in creat_react_agent and pydantic prebuilt: add a test for using create_react_agent w/ Pydantic schema and ChatPromptTemplate prompt Mar 16, 2025
@vbarda
Copy link
Collaborator

vbarda commented Mar 16, 2025

@ianchi thanks for adding the test -- I updated it a bit to fix some errors in it.

as you pointed out, pydantic inputs are not supported in langchain-core ChatPromptTemplate. we can continue discussion for that in the other PR you created

also, you can simply supply a function as a prompt. would that be sufficient for your use case?

def prompt(state: State):
    return [SystemMessage(content=f"You are an {state.role}.")] + state.messages

injected state in the tool is inconsistent

what do you mean by this? it works as expected for me (when i use prompt function)

as for "forcing to have RemainingStes in the state." - this is actually required for this part of the code to work, so i think for now we'll keep it as is. note that since these are managed values, they will not show up in the output state

@ianchi
Copy link
Contributor Author

ianchi commented Mar 17, 2025

Hi @vbarda ,

For now I switched back to using TypedDict instead of pydantic in everything langchain/langgraph related, and keep pydantic for all the rest of the application.

It would be really nice to have better support for it, not only in the react agent, but in general.
But I understand from the comments in the PRs, that it is simply not the native way.

Thanks for all the great work!

@vbarda
Copy link
Collaborator

vbarda commented Mar 17, 2025

Hi @vbarda ,

For now I switched back to using TypedDict instead of pydantic in everything langchain/langgraph related, and keep pydantic for all the rest of the application.

It would be really nice to have better support for it, not only in the react agent, but in general. But I understand from the comments in the PRs, that it is simply not the native way.

Thanks for all the great work!

That works too! I would say that LangGraph (the framework) has full support for both Pydantic and TypedDict state schemas. But I agree that it's suboptimal that ChatPromptTemplate doesn't handle Pydantic models out of box (which is not an issue with LangGraph, but still annoying -- will look into how to support this properly)

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.

2 participants