create_agent In "astream" with stream_mode="messages" does not output tokens incrementally.
Checked other resources
- [x] This is a bug, not a usage question. For questions, please use the LangChain Forum (https://forum.langchain.com/).
- [x] I added a clear and detailed title that summarizes the issue.
- [x] I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example).
- [x] I included a self-contained, minimal example that demonstrates the issue INCLUDING all the relevant imports. The code run AS IS to reproduce the issue.
Example Code
import asyncio
from langchain.agents import create_agent
from langchain_openai import ChatOpenAI
modelOpenAI = ChatOpenAI(
model="***",
base_url="***",
api_key="***",
)
agent = create_agent(
model=modelOpenAI,
system_prompt="You are a helpful assistant",
)
async def main():
async for token, metadata in agent.astream(
{"messages": [{"role": "user", "content": "hello who are you?"}]},
stream_mode="messages",
stream_subgraphs=True
):
print(token.content)
if __name__ == '__main__':
asyncio.run(main())
Error Message and Stack Trace (if applicable)
Output:
Hello! I’m your AI assistant, here to help answer your questions, provide information, or assist with tasks. You can think of me as a friendly digital helper. How can I assist you today? 😊
Description
I'm trying to use create_agent in astream with stream_mode="messages", but the output is not generated token by token.
System Info
version:
I think this should be in the langchain repository instead of langgraph.
Hello @nswsnb I ran your code and it worked as expected:
I just replaced print(token.content) by print(token.content + "|", end="") and got output:
|Hello|!| I'm| an| AI| language| model| here| to| assist| you|.| How| can| I| help| today|?||||
I am using same version of langgraph and langchain as you 1.0.3 and 1.0.7
What model are you using ?
Hi @nswsnb, I am closing as I cannot reproduce. Your code is functioning as expected. If you're still having issues please be more descriptive about what's going wrong.