ai
ai copied to clipboard
SocketError: other side closed
Description
I'm facing a problem where if my agent streams for long (we have agent that runs for 100+ steps and write big files), I get error socket connection closed error. It always happens during streaming a big write to a file using a tool.
This seems to happen across providers (anthropic, openai).
AI SDK Version
latest verison of anthropic provider and ai sdk.
Code of Conduct
- [x] I agree to follow this project's Code of Conduct
@gr2m any pointers for me to create a reproducible example?
The only thing I can do to reproduce the problem consistently is to ask it to write 50 page long research.txt file in a loop. That will run into this.
(formatted for readability)
start: {
"level": 50,
"time": 1761315667228,
"pid": 14,
"hostname": "srv-d2j1d7mmcj7s73eh3kmg-5c9bb5c585-cb5rn",
"name": "@runable/api",
"err": {
"type": "TypeError",
"message": "terminated: other side closed",
"stack": "TypeError: terminated ... error stack trace ..."
},
"msg": "Got error in agent stream"
}
start: {
"level": 50,
"time": 1761315667228,
"pid": 14,
"hostname": "srv-d2j1d7mmcj7s73eh3kmg-5c9bb5c585-cb5rn",
"name": "@runable/api",
"err": {
"type": "Error",
"message": "",
"stack": "Error ... error stack trace ..."
},
"msg": "Got error in agent stream"
}
So I think the reason why it kills the stream and throw error beyond max-retry is because we don't handle the unidici error separately because those error structures contain circular dependencies causing json serialization to fail and not re-try. @gr2m
@lgrammel this is a very frequent issue on openai side. Any time, I use codex model to write a long file. It times out especially in non streaming mode eg - generateText
It's hard for us to investigate until we have a way to replicate the problem ourselves, unfortunately