anomalyco / anomalyco/opencode
POST /session/{id}/message crashes with UnknownError when "agent" is included in the body
@rekram1-node is already working on this.
Since Aug 7, 2026.
- Dominant language
- TypeScript
- Stars
- 209k
- Forks
- 27.5k
- PR merge metrics
- PR metrics pending
Description
Description
POST /session/{id}/message (operationId: session.prompt) crashes with a generic UnknownError whenever the request body includes an agent field — reproduced with three different payload shapes (agent alone, agent+format, agent+format+model), all producing the identical stack trace within ~100ms of session creation, before any provider call is attempted.
Separately, omitting agent from the message body does not fall back to the session's configured agent — it silently uses the default build agent instead, even though the session was created with a custom agent. So there is currently no way to run a message against a non-default agent through this endpoint at all: specifying it crashes, and omitting it silently uses the wrong agent.
This also blocks using format: json_schema structured output together with a custom agent, since format lives on the same endpoint.
Steps to reproduce
export OPENCODE_CONFIG="/path/to/opencode.json" # must define the custom agent + provider
opencode serve --port 4096 --print-logs --log-level INFO &
# 1. Create a session scoped to a custom agent + provider (this works correctly)
curl -s -X POST "http://127.0.0.1:4096/session?directory=/path/to/project" \
-H "Content-Type: application/json" \
-d '{"agent":"my-custom-agent","model":{"providerID":"my-provider","id":"my-model"}}'
# -> returns a session with agent=my-custom-agent, model.providerID=my-provider (confirmed correct in server logs)
# 2. Send a message WITHOUT "agent" in the body (replace <id> with the session id from step 1)
curl -s -X POST "http://127.0.0.1:4096/session/<id>/message" \
-H "Content-Type: application/json" \
-d '{"parts":[{"type":"text","text":"hello"}]}'
# -> succeeds (200), but server logs show `agent=build`, not `agent=my-custom-agent` -- the
# session's configured agent is silently ignored.
# 3. Send a message WITH "agent" in the body, to try to force the correct agent
curl -s -X POST "http://127.0.0.1:4096/session/<id>/message" \
-H "Content-Type: application/json" \
-d '{"parts":[{"type":"text","text":"hello"}],"agent":"my-custom-agent"}'
# -> 500, body: {"name":"UnknownError","data":{"message":"Unexpected server error. Check server logs for details.","ref":"err_..."}}
Server log for the step-3 crash
error="UnknownError: UnknownError" cause="UnknownError: UnknownError
at <anonymous> (B:/~BUN/root/chunk-0acbd0yq.js:1085:2069)
at SessionPrompt.createUserMessage (B:/~BUN/root/chunk-0acbd0yq.js:1085:9954)
at SessionPrompt.createUserMessage (definition) (B:/~BUN/root/chunk-0acbd0yq.js:1085:1827)
at SessionPrompt.prompt (B:/~BUN/root/chunk-d8mqra64.js:4:13220)
at SessionPrompt.prompt (definition) (B:/~BUN/root/chunk-0acbd0yq.js:1085:9838)
at SessionHttpApi.prompt (B:/~BUN/root/chunk-d8mqra64.js:2:84408)
at SessionHttpApi.prompt (definition) (B:/~BUN/root/chunk-d8mqra64.js:4:13133)
The crash happens with format entirely absent, so it is specifically triggered by the agent field, not by format/structured output.
I also tried the newer v2 API (POST /api/session/{id}/agent, operationId: v2.session.switchAgent) as a possible workaround to set the agent before prompting, but v2's prompt endpoint (POST /api/session/{id}/prompt, PromptInput body schema: text, files, agents) has no format field at all, so it can't carry structured output either way.
Environment
- OpenCode version: 1.18.14 (latest on npm as of this report)
- Provider: custom OpenAI-compatible (
@ai-sdk/openai-compatible), self-hostedsglangserver - OS: Windows 11 (also expected to reproduce on Linux/macOS -- nothing OS-specific in the repro)
- Invocation: raw HTTP against
opencode serve, not the CLI'srun/--agentflag (which works correctly since it doesn't go through this exact code path)
Impact
There is no working way to combine a custom agent with format: json_schema structured output via the HTTP API in this version. This blocks using engine-enforced structured output for any project that relies on custom agent definitions rather than the default build/plan agents.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.