Skip to content

fix: emit role chunk from anthropic message start in chat stream - #3575

Merged
akshaydeo merged 1 commit into
devfrom
05-18-fix_emit_role_chunk_from_anthropic_message_start_in_chat_stream
May 18, 2026
Merged

fix: emit role chunk from anthropic message start in chat stream#3575
akshaydeo merged 1 commit into
devfrom
05-18-fix_emit_role_chunk_from_anthropic_message_start_in_chat_stream

Conversation

@TejasGhatte

@TejasGhatte TejasGhatte commented May 18, 2026

Copy link
Copy Markdown
Collaborator

Summary

When processing Anthropic streaming events, the message_start event was previously discarded entirely. This meant the assistant role information included in the initial message event was never forwarded to the caller, resulting in incomplete stream chunks.

Changes

  • When a message_start event contains a non-empty role on the message, a chat.completion.chunk stream response is now returned with the role populated in the delta, rather than returning nil and silently dropping it.

Type of change

  • Bug fix
  • Feature
  • Refactor
  • Documentation
  • Chore/CI

Affected areas

  • Core (Go)
  • Transports (HTTP)
  • Providers/Integrations
  • Plugins
  • UI (React)
  • Docs

How to test

Send a streaming chat completion request through the Anthropic provider and verify that the first chunk in the stream includes the assistant role in the delta.

go test ./core/providers/anthropic/...

Confirm the first streamed chunk contains:

{
  "object": "chat.completion.chunk",
  "choices": [
    {
      "index": 0,
      "delta": {
        "role": "assistant"
      }
    }
  ]
}

Screenshots/Recordings

N/A

Breaking changes

  • Yes
  • No

Related issues

N/A

Security considerations

No security implications.

Checklist

  • I read docs/contributing/README.md and followed the guidelines
  • I added/updated tests where appropriate
  • I updated documentation where needed
  • I verified builds succeed (Go and UI)
  • I verified the CI pipeline passes locally if applicable

@coderabbitai

coderabbitai Bot commented May 18, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 8493d56e-2efd-4d8b-b5fd-90eaad8c78dc

📥 Commits

Reviewing files that changed from the base of the PR and between 89a99f0 and 4f3a121.

📒 Files selected for processing (1)
  • core/providers/anthropic/chat.go

📝 Walkthrough

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Improved handling of streaming chat completion responses to properly process message initialization events and correctly set message role information in the stream.

Walkthrough

This change extends ToBifrostChatCompletionStream to handle the Anthropic MessageStart stream event. When received with a non-empty Role, it emits a stream chunk with the role set in the response delta, enabling early role transmission in the completion stream.

Changes

Anthropic MessageStart Event Handler

Layer / File(s) Summary
MessageStart event type handler
core/providers/anthropic/chat.go
Added a switch case for AnthropicStreamEventTypeMessageStart that emits a chat.completion.chunk response with the role delta when the message contains a non-empty role, otherwise falls through to existing default behavior.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Suggested reviewers

  • akshaydeo
  • danpiths

Poem

🐰 A rabbit hops through streams so fine,
With roles now sent right on time,
When messages start their journey through,
The role flows swift—both fast and true! 🌟

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: emitting role chunk from Anthropic message start in chat stream, directly matching the file modification.
Description check ✅ Passed The description includes all required template sections with substantive content: summary, changes, type of change, affected areas, testing instructions, and breaking changes. However, several checklist items remain unchecked (contributing guidelines, tests, documentation, and build verification).
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch 05-18-fix_emit_role_chunk_from_anthropic_message_start_in_chat_stream

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.12.2)

level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies"


Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Collaborator Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@TejasGhatte
TejasGhatte marked this pull request as ready for review May 18, 2026 13:17
@coderabbitai
coderabbitai Bot requested review from akshaydeo and danpiths May 18, 2026 13:17
@greptile-apps

greptile-apps Bot commented May 18, 2026

Copy link
Copy Markdown
Contributor

Confidence Score: 4/5

The change is a small, well-scoped addition to an existing switch statement; the new code path is logically consistent with how other event types build role/delta chunks in the same function.

The implementation correctly extracts the role before returning a role-only chunk, and the caller already handles nil vs non-nil responses, so the new branch does not introduce any observable runtime risk. The only gap is the absence of a unit test, which leaves the new branch entirely uncovered.

core/providers/anthropic/chat.go — the new branch in ToBifrostChatCompletionStream has no test coverage.

Important Files Changed

Filename Overview
core/providers/anthropic/chat.go Adds role emission on message_start streaming event; logic is correct and consistent with other chunk builders, but no unit test was added for the new branch.

Reviews (1): Last reviewed commit: "fix: emit role chunk from anthropic mess..." | Re-trigger Greptile

Comment thread core/providers/anthropic/chat.go

akshaydeo commented May 18, 2026

Copy link
Copy Markdown
Contributor

Merge activity

  • May 18, 1:42 PM UTC: A user started a stack merge that includes this pull request via Graphite.
  • May 18, 1:43 PM UTC: @akshaydeo merged this pull request with Graphite.

@akshaydeo
akshaydeo merged commit db2552e into dev May 18, 2026
15 checks passed
@akshaydeo
akshaydeo deleted the 05-18-fix_emit_role_chunk_from_anthropic_message_start_in_chat_stream branch May 18, 2026 13:43
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


tejas ghatte seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

akshaydeo pushed a commit that referenced this pull request May 20, 2026
## Summary

When processing Anthropic streaming events, the `message_start` event was previously discarded entirely. This meant the assistant role information included in the initial message event was never forwarded to the caller, resulting in incomplete stream chunks.

## Changes

- When a `message_start` event contains a non-empty role on the message, a `chat.completion.chunk` stream response is now returned with the role populated in the delta, rather than returning `nil` and silently dropping it.

## Type of change

- [x] Bug fix
- [ ] Feature
- [ ] Refactor
- [ ] Documentation
- [ ] Chore/CI

## Affected areas

- [x] Core (Go)
- [ ] Transports (HTTP)
- [x] Providers/Integrations
- [ ] Plugins
- [ ] UI (React)
- [ ] Docs

## How to test

Send a streaming chat completion request through the Anthropic provider and verify that the first chunk in the stream includes the assistant role in the delta.

```sh
go test ./core/providers/anthropic/...
```

Confirm the first streamed chunk contains:
```json
{
  "object": "chat.completion.chunk",
  "choices": [
    {
      "index": 0,
      "delta": {
        "role": "assistant"
      }
    }
  ]
}
```

## Screenshots/Recordings

N/A

## Breaking changes

- [ ] Yes
- [x] No

## Related issues

N/A

## Security considerations

No security implications.

## Checklist

- [ ] I read `docs/contributing/README.md` and followed the guidelines
- [ ] I added/updated tests where appropriate
- [ ] I updated documentation where needed
- [ ] I verified builds succeed (Go and UI)
- [ ] I verified the CI pipeline passes locally if applicable
@akshaydeo akshaydeo mentioned this pull request May 20, 2026
akhsaul pushed a commit to akhsaul/bifrost that referenced this pull request Aug 27, 2026
…imhq#3575)

## Summary

When processing Anthropic streaming events, the `message_start` event was previously discarded entirely. This meant the assistant role information included in the initial message event was never forwarded to the caller, resulting in incomplete stream chunks.

## Changes

- When a `message_start` event contains a non-empty role on the message, a `chat.completion.chunk` stream response is now returned with the role populated in the delta, rather than returning `nil` and silently dropping it.

## Type of change

- [x] Bug fix
- [ ] Feature
- [ ] Refactor
- [ ] Documentation
- [ ] Chore/CI

## Affected areas

- [x] Core (Go)
- [ ] Transports (HTTP)
- [x] Providers/Integrations
- [ ] Plugins
- [ ] UI (React)
- [ ] Docs

## How to test

Send a streaming chat completion request through the Anthropic provider and verify that the first chunk in the stream includes the assistant role in the delta.

```sh
go test ./core/providers/anthropic/...
```

Confirm the first streamed chunk contains:
```json
{
  "object": "chat.completion.chunk",
  "choices": [
    {
      "index": 0,
      "delta": {
        "role": "assistant"
      }
    }
  ]
}
```

## Screenshots/Recordings

N/A

## Breaking changes

- [ ] Yes
- [x] No

## Related issues

N/A

## Security considerations

No security implications.

## Checklist

- [ ] I read `docs/contributing/README.md` and followed the guidelines
- [ ] I added/updated tests where appropriate
- [ ] I updated documentation where needed
- [ ] I verified builds succeed (Go and UI)
- [ ] I verified the CI pipeline passes locally if applicable
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.

3 participants