Skip to content

Backport: feat(cerebras): add chat language model provider#15428

Merged
github-actions[bot] merged 2 commits into
release-v6.0from
backport-pr-15420-to-release-v6.0
May 19, 2026
Merged

Backport: feat(cerebras): add chat language model provider#15428
github-actions[bot] merged 2 commits into
release-v6.0from
backport-pr-15420-to-release-v6.0

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

This is an automated backport of #15420 to the release-v6.0 branch. FYI @aayush-kapoor

## Background

#15349

this is a follow up to #15416. 

cerebras models could return a valid structured JSON answer, like
{"result":"2026"}, while also marking the response as `tool_calls` and
repeating a tool call in the same response. The AI SDK interpreted that
as “keep calling tools,” so the loop continued until stopWhen was hit.
Because the final step still had finish reason `tool-calls`,
`result.output` was never parsed and threw `NoOutputGeneratedError`

## Summary

- Added a CerebrasChatLanguageModel wrapper around the OpenAI-compatible
chat model
- For structured JSON output calls only, when cerebras returns non-empty
text with raw finish reason `tool_calls`, we treat it as the final
structured answer
- In that narrow case, we normalize the finish reason to `stop` and drop
the stray repeated tool-call part
- Real tool-call turns without text are still preserved and executed
normally

what would have been a bug fix was treated as an opportunity to handle
cerebras quirks specifically

## Manual Verification

verified by running the repro: 

```ts
import { NoOutputGeneratedError, Output, isStepCount, tool, ToolLoopAgent } from "ai";
import z from "zod";
import { cerebras } from "@ai-sdk/cerebras";
import { run } from '../../lib/run';

run(async () => {
  const agent = new ToolLoopAgent({
    instructions:
      "Use the required structured output. For simple arithmetic you may answer directly.",
    model: cerebras("zai-glm-4.7"),
    tools: {
      nonUsefulTool: tool({
        description: "A non-useful tool that returns a magic number",
        inputSchema: z.object({}),
        execute: () => {
          return 2026;
        },
      }),
    },
    output: Output.object({
      schema: z.object({ result: z.string() }),
    }),
    stopWhen: isStepCount(5),
  });
  const result = await agent.generate({
    prompt: "what is the magic number?",
  });

  console.log(
    'steps:',
    JSON.stringify(
      result.steps.map(step => ({
        finishReason: step.finishReason,
        text: step.text,
        toolCalls: step.toolCalls.map(toolCall => ({
          toolName: toolCall.toolName,
          input: toolCall.input,
        })),
      })),
      null,
      2,
    ),
  );

  try {
    console.log(result.output);
  } catch (cause) {
    if (NoOutputGeneratedError.isInstance(cause)) {
      console.error(
        "Structured output missing (last step did not yield valid JSON).",
        "finishReason:",
        result.finishReason,
        "lastText:",
        JSON.stringify(result.text),
      );
      throw cause;
    }
    throw cause;
  }
});
```

## Checklist

- [x] All commits are signed (PRs with unsigned commits cannot be
merged)
- [x] Tests have been added / updated (for bug fixes / features)
- [ ] Documentation has been added / updated (for bug fixes / features)
- [x] A _patch_ changeset for relevant packages has been added (for bug
fixes / features - run `pnpm changeset` in the project root)
- [x] I have reviewed this pull request (self-review) 

## Related Issues

fixes #15349

---------

Co-authored-by: Gregor Martynus <39992+gr2m@users.noreply.github.com>
Comment thread packages/cerebras/src/cerebras-chat-language-model.ts
@github-actions github-actions Bot merged commit e99134d into release-v6.0 May 19, 2026
17 checks passed
@github-actions github-actions Bot deleted the backport-pr-15420-to-release-v6.0 branch May 19, 2026 04:34
@github-actions
Copy link
Copy Markdown
Contributor Author

🚀 Published in:

Package Version
@ai-sdk/cerebras 2.0.53 github npm

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