[Search Playground] Fix the issue related to token count#246589
[Search Playground] Fix the issue related to token count#246589saikatsarkar056 merged 2 commits intoelastic:mainfrom
Conversation
🔍 Preview links for changed docs |
37a3ffd to
2ee726e
Compare
Fix the issue related to token count
d528481 to
ac9fe1f
Compare
@TattdCodeMonkey Those test failures were caused by a previous commit. With the current changes, the conversational chain tests should pass. I’ll wait for the CI/CD pipeline to turn green. |
|
Starting backport for target branches: 9.4 |
💛 Build succeeded, but was flaky
Failed CI StepsTest Failures
Metrics [docs]Async chunks
History
|
💔 All backports failed
Manual backportTo create the backport manually run: Questions ?Please refer to the Backport tool documentation |
|
@saikatsarkar056 we should backport this to all the versions that the ai dep was backported too as well #244675 |
I will create the backport PRs. |
💚 All backports created successfully
Note: Successful backport PRs will be merged automatically after passing CI. Questions ?Please refer to the Backport tool documentation |
💚 All backports created successfully
Note: Successful backport PRs will be merged automatically after passing CI. Questions ?Please refer to the Backport tool documentation |
💚 All backports created successfully
Note: Successful backport PRs will be merged automatically after passing CI. Questions ?Please refer to the Backport tool documentation |
) ## Summary Fixes token count display showing "NaN / 200,000 tokens sent" in the Search Playground after the AI SDK v5 upgrade. The issue was on the client-side in `parseDataStream` (`api.ts`), where `messageAnnotations` was being reset to `undefined` when a `text-start` chunk was received. In AI SDK v5, annotations (including token counts) are sent *before* the `text-start` chunk, so this reset was clearing the already-collected token data. This PR removes that reset to preserve annotations across the stream, ensuring token counts are correctly attached to the final message and displayed in the UI. ### Before <img width="865" height="502" alt="Screenshot 2025-12-16 at 10 39 42 AM" src="https://github.com/user-attachments/assets/331d3c7c-1b3e-455e-b8d7-561e2832054c" /> ### After <img width="1008" height="519" alt="Screenshot 2025-12-16 at 9 56 13 AM" src="https://github.com/user-attachments/assets/ec507093-e382-4320-b610-cc3d53a74c3b" /> ### Checklist Check the PR satisfies following conditions. Reviewers should verify this PR satisfies this list as well. - [ ] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [ ] This was checked for breaking HTTP API changes, and any breaking changes have been approved by the breaking-change committee. The `release_note:breaking` label should be applied in these situations. - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [x] The PR description includes the appropriate Release Notes section, and the correct `release_note:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) - [x] Review the [backport guidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing) and apply applicable `backport:*` labels. ### Identify risks Does this PR introduce any risks? For example, consider risks like hard to test bugs, performance regression, potential of data loss. Describe the risk, its severity, and mitigation for each identified risk. Invite stakeholders and evaluate how to proceed before merging. - [ ] [See some risk examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) - [ ] ... ## Release note Fixes token count display showing "NaN" in Search Playground by preserving message annotations across the AI SDK v5 stream. (cherry picked from commit 6eae531)
) ## Summary Fixes token count display showing "NaN / 200,000 tokens sent" in the Search Playground after the AI SDK v5 upgrade. The issue was on the client-side in `parseDataStream` (`api.ts`), where `messageAnnotations` was being reset to `undefined` when a `text-start` chunk was received. In AI SDK v5, annotations (including token counts) are sent *before* the `text-start` chunk, so this reset was clearing the already-collected token data. This PR removes that reset to preserve annotations across the stream, ensuring token counts are correctly attached to the final message and displayed in the UI. ### Before <img width="865" height="502" alt="Screenshot 2025-12-16 at 10 39 42 AM" src="https://github.com/user-attachments/assets/331d3c7c-1b3e-455e-b8d7-561e2832054c" /> ### After <img width="1008" height="519" alt="Screenshot 2025-12-16 at 9 56 13 AM" src="https://github.com/user-attachments/assets/ec507093-e382-4320-b610-cc3d53a74c3b" /> ### Checklist Check the PR satisfies following conditions. Reviewers should verify this PR satisfies this list as well. - [ ] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [ ] This was checked for breaking HTTP API changes, and any breaking changes have been approved by the breaking-change committee. The `release_note:breaking` label should be applied in these situations. - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [x] The PR description includes the appropriate Release Notes section, and the correct `release_note:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) - [x] Review the [backport guidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing) and apply applicable `backport:*` labels. ### Identify risks Does this PR introduce any risks? For example, consider risks like hard to test bugs, performance regression, potential of data loss. Describe the risk, its severity, and mitigation for each identified risk. Invite stakeholders and evaluate how to proceed before merging. - [ ] [See some risk examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) - [ ] ... ## Release note Fixes token count display showing "NaN" in Search Playground by preserving message annotations across the AI SDK v5 stream. (cherry picked from commit 6eae531)
|
Looks like this PR has backport PRs but they still haven't been merged. Please merge them ASAP to keep the branches relatively in sync. |
…) (#246766) # Backport This will backport the following commits from `main` to `9.2`: - [[Search Playground] Fix the issue related to token count (#246589)](#246589) <!--- Backport version: 10.2.0 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Saikat Sarkar","email":"132922331+saikatsarkar056@users.noreply.github.com"},"sourceCommit":{"committedDate":"2025-12-17T00:27:12Z","message":"[Search Playground] Fix the issue related to token count (#246589)\n\n## Summary\n\nFixes token count display showing \"NaN / 200,000 tokens sent\" in the\nSearch Playground after the AI SDK v5 upgrade.\n\nThe issue was on the client-side in `parseDataStream` (`api.ts`), where\n`messageAnnotations` was being reset to `undefined` when a `text-start`\nchunk was received. In AI SDK v5, annotations (including token counts)\nare sent *before* the `text-start` chunk, so this reset was clearing the\nalready-collected token data. This PR removes that reset to preserve\nannotations across the stream, ensuring token counts are correctly\nattached to the final message and displayed in the UI.\n\n\n### Before\n<img width=\"865\" height=\"502\" alt=\"Screenshot 2025-12-16 at 10 39 42 AM\"\nsrc=\"https://github.com/user-attachments/assets/331d3c7c-1b3e-455e-b8d7-561e2832054c\"\n/>\n\n\n### After\n<img width=\"1008\" height=\"519\" alt=\"Screenshot 2025-12-16 at 9 56 13 AM\"\nsrc=\"https://github.com/user-attachments/assets/ec507093-e382-4320-b610-cc3d53a74c3b\"\n/>\n\n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [ ] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [ ]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [ ] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [ ] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [ ] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [x] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n- [x] Review the [backport\nguidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing)\nand apply applicable `backport:*` labels.\n\n### Identify risks\n\nDoes this PR introduce any risks? For example, consider risks like hard\nto test bugs, performance regression, potential of data loss.\n\nDescribe the risk, its severity, and mitigation for each identified\nrisk. Invite stakeholders and evaluate how to proceed before merging.\n\n- [ ] [See some risk\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)\n- [ ] ...\n\n\n## Release note\nFixes token count display showing \"NaN\" in Search Playground by\npreserving message annotations across the AI SDK v5 stream.","sha":"6eae531e0d82503feff37f1abebeb7d5b0e0907f","branchLabelMapping":{"^v9.3.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:Search","backport:version","v9.3.0","v9.4.0"],"title":"[Search Playground] Fix the issue related to token count","number":246589,"url":"https://github.com/elastic/kibana/pull/246589","mergeCommit":{"message":"[Search Playground] Fix the issue related to token count (#246589)\n\n## Summary\n\nFixes token count display showing \"NaN / 200,000 tokens sent\" in the\nSearch Playground after the AI SDK v5 upgrade.\n\nThe issue was on the client-side in `parseDataStream` (`api.ts`), where\n`messageAnnotations` was being reset to `undefined` when a `text-start`\nchunk was received. In AI SDK v5, annotations (including token counts)\nare sent *before* the `text-start` chunk, so this reset was clearing the\nalready-collected token data. This PR removes that reset to preserve\nannotations across the stream, ensuring token counts are correctly\nattached to the final message and displayed in the UI.\n\n\n### Before\n<img width=\"865\" height=\"502\" alt=\"Screenshot 2025-12-16 at 10 39 42 AM\"\nsrc=\"https://github.com/user-attachments/assets/331d3c7c-1b3e-455e-b8d7-561e2832054c\"\n/>\n\n\n### After\n<img width=\"1008\" height=\"519\" alt=\"Screenshot 2025-12-16 at 9 56 13 AM\"\nsrc=\"https://github.com/user-attachments/assets/ec507093-e382-4320-b610-cc3d53a74c3b\"\n/>\n\n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [ ] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [ ]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [ ] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [ ] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [ ] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [x] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n- [x] Review the [backport\nguidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing)\nand apply applicable `backport:*` labels.\n\n### Identify risks\n\nDoes this PR introduce any risks? For example, consider risks like hard\nto test bugs, performance regression, potential of data loss.\n\nDescribe the risk, its severity, and mitigation for each identified\nrisk. Invite stakeholders and evaluate how to proceed before merging.\n\n- [ ] [See some risk\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)\n- [ ] ...\n\n\n## Release note\nFixes token count display showing \"NaN\" in Search Playground by\npreserving message annotations across the AI SDK v5 stream.","sha":"6eae531e0d82503feff37f1abebeb7d5b0e0907f"}},"sourceBranch":"main","suggestedTargetBranches":["9.4"],"targetPullRequestStates":[{"branch":"main","label":"v9.3.0","branchLabelMappingKey":"^v9.3.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/246589","number":246589,"mergeCommit":{"message":"[Search Playground] Fix the issue related to token count (#246589)\n\n## Summary\n\nFixes token count display showing \"NaN / 200,000 tokens sent\" in the\nSearch Playground after the AI SDK v5 upgrade.\n\nThe issue was on the client-side in `parseDataStream` (`api.ts`), where\n`messageAnnotations` was being reset to `undefined` when a `text-start`\nchunk was received. In AI SDK v5, annotations (including token counts)\nare sent *before* the `text-start` chunk, so this reset was clearing the\nalready-collected token data. This PR removes that reset to preserve\nannotations across the stream, ensuring token counts are correctly\nattached to the final message and displayed in the UI.\n\n\n### Before\n<img width=\"865\" height=\"502\" alt=\"Screenshot 2025-12-16 at 10 39 42 AM\"\nsrc=\"https://github.com/user-attachments/assets/331d3c7c-1b3e-455e-b8d7-561e2832054c\"\n/>\n\n\n### After\n<img width=\"1008\" height=\"519\" alt=\"Screenshot 2025-12-16 at 9 56 13 AM\"\nsrc=\"https://github.com/user-attachments/assets/ec507093-e382-4320-b610-cc3d53a74c3b\"\n/>\n\n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [ ] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [ ]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [ ] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [ ] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [ ] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [x] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n- [x] Review the [backport\nguidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing)\nand apply applicable `backport:*` labels.\n\n### Identify risks\n\nDoes this PR introduce any risks? For example, consider risks like hard\nto test bugs, performance regression, potential of data loss.\n\nDescribe the risk, its severity, and mitigation for each identified\nrisk. Invite stakeholders and evaluate how to proceed before merging.\n\n- [ ] [See some risk\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)\n- [ ] ...\n\n\n## Release note\nFixes token count display showing \"NaN\" in Search Playground by\npreserving message annotations across the AI SDK v5 stream.","sha":"6eae531e0d82503feff37f1abebeb7d5b0e0907f"}},{"branch":"9.4","label":"v9.4.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"url":"https://github.com/elastic/kibana/pull/246764","number":246764,"branch":"8.19","state":"OPEN"},{"url":"https://github.com/elastic/kibana/pull/246765","number":246765,"branch":"9.1","state":"OPEN"}]}] BACKPORT-->
…) (#246765) # Backport This will backport the following commits from `main` to `9.1`: - [[Search Playground] Fix the issue related to token count (#246589)](#246589) <!--- Backport version: 10.2.0 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Saikat Sarkar","email":"132922331+saikatsarkar056@users.noreply.github.com"},"sourceCommit":{"committedDate":"2025-12-17T00:27:12Z","message":"[Search Playground] Fix the issue related to token count (#246589)\n\n## Summary\n\nFixes token count display showing \"NaN / 200,000 tokens sent\" in the\nSearch Playground after the AI SDK v5 upgrade.\n\nThe issue was on the client-side in `parseDataStream` (`api.ts`), where\n`messageAnnotations` was being reset to `undefined` when a `text-start`\nchunk was received. In AI SDK v5, annotations (including token counts)\nare sent *before* the `text-start` chunk, so this reset was clearing the\nalready-collected token data. This PR removes that reset to preserve\nannotations across the stream, ensuring token counts are correctly\nattached to the final message and displayed in the UI.\n\n\n### Before\n<img width=\"865\" height=\"502\" alt=\"Screenshot 2025-12-16 at 10 39 42 AM\"\nsrc=\"https://github.com/user-attachments/assets/331d3c7c-1b3e-455e-b8d7-561e2832054c\"\n/>\n\n\n### After\n<img width=\"1008\" height=\"519\" alt=\"Screenshot 2025-12-16 at 9 56 13 AM\"\nsrc=\"https://github.com/user-attachments/assets/ec507093-e382-4320-b610-cc3d53a74c3b\"\n/>\n\n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [ ] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [ ]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [ ] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [ ] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [ ] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [x] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n- [x] Review the [backport\nguidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing)\nand apply applicable `backport:*` labels.\n\n### Identify risks\n\nDoes this PR introduce any risks? For example, consider risks like hard\nto test bugs, performance regression, potential of data loss.\n\nDescribe the risk, its severity, and mitigation for each identified\nrisk. Invite stakeholders and evaluate how to proceed before merging.\n\n- [ ] [See some risk\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)\n- [ ] ...\n\n\n## Release note\nFixes token count display showing \"NaN\" in Search Playground by\npreserving message annotations across the AI SDK v5 stream.","sha":"6eae531e0d82503feff37f1abebeb7d5b0e0907f","branchLabelMapping":{"^v9.3.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:Search","backport:version","v9.3.0","v9.4.0"],"title":"[Search Playground] Fix the issue related to token count","number":246589,"url":"https://github.com/elastic/kibana/pull/246589","mergeCommit":{"message":"[Search Playground] Fix the issue related to token count (#246589)\n\n## Summary\n\nFixes token count display showing \"NaN / 200,000 tokens sent\" in the\nSearch Playground after the AI SDK v5 upgrade.\n\nThe issue was on the client-side in `parseDataStream` (`api.ts`), where\n`messageAnnotations` was being reset to `undefined` when a `text-start`\nchunk was received. In AI SDK v5, annotations (including token counts)\nare sent *before* the `text-start` chunk, so this reset was clearing the\nalready-collected token data. This PR removes that reset to preserve\nannotations across the stream, ensuring token counts are correctly\nattached to the final message and displayed in the UI.\n\n\n### Before\n<img width=\"865\" height=\"502\" alt=\"Screenshot 2025-12-16 at 10 39 42 AM\"\nsrc=\"https://github.com/user-attachments/assets/331d3c7c-1b3e-455e-b8d7-561e2832054c\"\n/>\n\n\n### After\n<img width=\"1008\" height=\"519\" alt=\"Screenshot 2025-12-16 at 9 56 13 AM\"\nsrc=\"https://github.com/user-attachments/assets/ec507093-e382-4320-b610-cc3d53a74c3b\"\n/>\n\n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [ ] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [ ]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [ ] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [ ] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [ ] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [x] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n- [x] Review the [backport\nguidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing)\nand apply applicable `backport:*` labels.\n\n### Identify risks\n\nDoes this PR introduce any risks? For example, consider risks like hard\nto test bugs, performance regression, potential of data loss.\n\nDescribe the risk, its severity, and mitigation for each identified\nrisk. Invite stakeholders and evaluate how to proceed before merging.\n\n- [ ] [See some risk\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)\n- [ ] ...\n\n\n## Release note\nFixes token count display showing \"NaN\" in Search Playground by\npreserving message annotations across the AI SDK v5 stream.","sha":"6eae531e0d82503feff37f1abebeb7d5b0e0907f"}},"sourceBranch":"main","suggestedTargetBranches":["9.4"],"targetPullRequestStates":[{"branch":"main","label":"v9.3.0","branchLabelMappingKey":"^v9.3.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/246589","number":246589,"mergeCommit":{"message":"[Search Playground] Fix the issue related to token count (#246589)\n\n## Summary\n\nFixes token count display showing \"NaN / 200,000 tokens sent\" in the\nSearch Playground after the AI SDK v5 upgrade.\n\nThe issue was on the client-side in `parseDataStream` (`api.ts`), where\n`messageAnnotations` was being reset to `undefined` when a `text-start`\nchunk was received. In AI SDK v5, annotations (including token counts)\nare sent *before* the `text-start` chunk, so this reset was clearing the\nalready-collected token data. This PR removes that reset to preserve\nannotations across the stream, ensuring token counts are correctly\nattached to the final message and displayed in the UI.\n\n\n### Before\n<img width=\"865\" height=\"502\" alt=\"Screenshot 2025-12-16 at 10 39 42 AM\"\nsrc=\"https://github.com/user-attachments/assets/331d3c7c-1b3e-455e-b8d7-561e2832054c\"\n/>\n\n\n### After\n<img width=\"1008\" height=\"519\" alt=\"Screenshot 2025-12-16 at 9 56 13 AM\"\nsrc=\"https://github.com/user-attachments/assets/ec507093-e382-4320-b610-cc3d53a74c3b\"\n/>\n\n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [ ] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [ ]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [ ] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [ ] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [ ] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [x] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n- [x] Review the [backport\nguidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing)\nand apply applicable `backport:*` labels.\n\n### Identify risks\n\nDoes this PR introduce any risks? For example, consider risks like hard\nto test bugs, performance regression, potential of data loss.\n\nDescribe the risk, its severity, and mitigation for each identified\nrisk. Invite stakeholders and evaluate how to proceed before merging.\n\n- [ ] [See some risk\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)\n- [ ] ...\n\n\n## Release note\nFixes token count display showing \"NaN\" in Search Playground by\npreserving message annotations across the AI SDK v5 stream.","sha":"6eae531e0d82503feff37f1abebeb7d5b0e0907f"}},{"branch":"9.4","label":"v9.4.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"url":"https://github.com/elastic/kibana/pull/246764","number":246764,"branch":"8.19","state":"OPEN"}]}] BACKPORT-->
) (#246764) # Backport This will backport the following commits from `main` to `8.19`: - [[Search Playground] Fix the issue related to token count (#246589)](#246589) <!--- Backport version: 10.2.0 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Saikat Sarkar","email":"132922331+saikatsarkar056@users.noreply.github.com"},"sourceCommit":{"committedDate":"2025-12-17T00:27:12Z","message":"[Search Playground] Fix the issue related to token count (#246589)\n\n## Summary\n\nFixes token count display showing \"NaN / 200,000 tokens sent\" in the\nSearch Playground after the AI SDK v5 upgrade.\n\nThe issue was on the client-side in `parseDataStream` (`api.ts`), where\n`messageAnnotations` was being reset to `undefined` when a `text-start`\nchunk was received. In AI SDK v5, annotations (including token counts)\nare sent *before* the `text-start` chunk, so this reset was clearing the\nalready-collected token data. This PR removes that reset to preserve\nannotations across the stream, ensuring token counts are correctly\nattached to the final message and displayed in the UI.\n\n\n### Before\n<img width=\"865\" height=\"502\" alt=\"Screenshot 2025-12-16 at 10 39 42 AM\"\nsrc=\"https://github.com/user-attachments/assets/331d3c7c-1b3e-455e-b8d7-561e2832054c\"\n/>\n\n\n### After\n<img width=\"1008\" height=\"519\" alt=\"Screenshot 2025-12-16 at 9 56 13 AM\"\nsrc=\"https://github.com/user-attachments/assets/ec507093-e382-4320-b610-cc3d53a74c3b\"\n/>\n\n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [ ] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [ ]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [ ] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [ ] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [ ] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [x] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n- [x] Review the [backport\nguidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing)\nand apply applicable `backport:*` labels.\n\n### Identify risks\n\nDoes this PR introduce any risks? For example, consider risks like hard\nto test bugs, performance regression, potential of data loss.\n\nDescribe the risk, its severity, and mitigation for each identified\nrisk. Invite stakeholders and evaluate how to proceed before merging.\n\n- [ ] [See some risk\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)\n- [ ] ...\n\n\n## Release note\nFixes token count display showing \"NaN\" in Search Playground by\npreserving message annotations across the AI SDK v5 stream.","sha":"6eae531e0d82503feff37f1abebeb7d5b0e0907f","branchLabelMapping":{"^v9.3.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:Search","backport:version","v9.3.0","v9.4.0"],"title":"[Search Playground] Fix the issue related to token count","number":246589,"url":"https://github.com/elastic/kibana/pull/246589","mergeCommit":{"message":"[Search Playground] Fix the issue related to token count (#246589)\n\n## Summary\n\nFixes token count display showing \"NaN / 200,000 tokens sent\" in the\nSearch Playground after the AI SDK v5 upgrade.\n\nThe issue was on the client-side in `parseDataStream` (`api.ts`), where\n`messageAnnotations` was being reset to `undefined` when a `text-start`\nchunk was received. In AI SDK v5, annotations (including token counts)\nare sent *before* the `text-start` chunk, so this reset was clearing the\nalready-collected token data. This PR removes that reset to preserve\nannotations across the stream, ensuring token counts are correctly\nattached to the final message and displayed in the UI.\n\n\n### Before\n<img width=\"865\" height=\"502\" alt=\"Screenshot 2025-12-16 at 10 39 42 AM\"\nsrc=\"https://github.com/user-attachments/assets/331d3c7c-1b3e-455e-b8d7-561e2832054c\"\n/>\n\n\n### After\n<img width=\"1008\" height=\"519\" alt=\"Screenshot 2025-12-16 at 9 56 13 AM\"\nsrc=\"https://github.com/user-attachments/assets/ec507093-e382-4320-b610-cc3d53a74c3b\"\n/>\n\n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [ ] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [ ]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [ ] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [ ] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [ ] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [x] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n- [x] Review the [backport\nguidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing)\nand apply applicable `backport:*` labels.\n\n### Identify risks\n\nDoes this PR introduce any risks? For example, consider risks like hard\nto test bugs, performance regression, potential of data loss.\n\nDescribe the risk, its severity, and mitigation for each identified\nrisk. Invite stakeholders and evaluate how to proceed before merging.\n\n- [ ] [See some risk\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)\n- [ ] ...\n\n\n## Release note\nFixes token count display showing \"NaN\" in Search Playground by\npreserving message annotations across the AI SDK v5 stream.","sha":"6eae531e0d82503feff37f1abebeb7d5b0e0907f"}},"sourceBranch":"main","suggestedTargetBranches":["9.4"],"targetPullRequestStates":[{"branch":"main","label":"v9.3.0","branchLabelMappingKey":"^v9.3.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/246589","number":246589,"mergeCommit":{"message":"[Search Playground] Fix the issue related to token count (#246589)\n\n## Summary\n\nFixes token count display showing \"NaN / 200,000 tokens sent\" in the\nSearch Playground after the AI SDK v5 upgrade.\n\nThe issue was on the client-side in `parseDataStream` (`api.ts`), where\n`messageAnnotations` was being reset to `undefined` when a `text-start`\nchunk was received. In AI SDK v5, annotations (including token counts)\nare sent *before* the `text-start` chunk, so this reset was clearing the\nalready-collected token data. This PR removes that reset to preserve\nannotations across the stream, ensuring token counts are correctly\nattached to the final message and displayed in the UI.\n\n\n### Before\n<img width=\"865\" height=\"502\" alt=\"Screenshot 2025-12-16 at 10 39 42 AM\"\nsrc=\"https://github.com/user-attachments/assets/331d3c7c-1b3e-455e-b8d7-561e2832054c\"\n/>\n\n\n### After\n<img width=\"1008\" height=\"519\" alt=\"Screenshot 2025-12-16 at 9 56 13 AM\"\nsrc=\"https://github.com/user-attachments/assets/ec507093-e382-4320-b610-cc3d53a74c3b\"\n/>\n\n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [ ] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [ ]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [ ] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [ ] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [ ] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [x] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n- [x] Review the [backport\nguidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing)\nand apply applicable `backport:*` labels.\n\n### Identify risks\n\nDoes this PR introduce any risks? For example, consider risks like hard\nto test bugs, performance regression, potential of data loss.\n\nDescribe the risk, its severity, and mitigation for each identified\nrisk. Invite stakeholders and evaluate how to proceed before merging.\n\n- [ ] [See some risk\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)\n- [ ] ...\n\n\n## Release note\nFixes token count display showing \"NaN\" in Search Playground by\npreserving message annotations across the AI SDK v5 stream.","sha":"6eae531e0d82503feff37f1abebeb7d5b0e0907f"}},{"branch":"9.4","label":"v9.4.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT-->
Summary
Fixes token count display showing "NaN / 200,000 tokens sent" in the Search Playground after the AI SDK v5 upgrade.
The issue was on the client-side in
parseDataStream(api.ts), wheremessageAnnotationswas being reset toundefinedwhen atext-startchunk was received. In AI SDK v5, annotations (including token counts) are sent before thetext-startchunk, so this reset was clearing the already-collected token data. This PR removes that reset to preserve annotations across the stream, ensuring token counts are correctly attached to the final message and displayed in the UI.Before
After
Checklist
Check the PR satisfies following conditions.
Reviewers should verify this PR satisfies this list as well.
release_note:breakinglabel should be applied in these situations.release_note:*label is applied per the guidelinesbackport:*labels.Identify risks
Does this PR introduce any risks? For example, consider risks like hard to test bugs, performance regression, potential of data loss.
Describe the risk, its severity, and mitigation for each identified risk. Invite stakeholders and evaluate how to proceed before merging.
Release note
Fixes token count display showing "NaN" in Search Playground by preserving message annotations across the AI SDK v5 stream.