Skip to content

fix(elements): render file elements with null mime instead of crashing#2939

Open
mihidumh wants to merge 1 commit into
Chainlit:mainfrom
mihidumh:fix/null-mime-file-element
Open

fix(elements): render file elements with null mime instead of crashing#2939
mihidumh wants to merge 1 commit into
Chainlit:mainfrom
mihidumh:fix/null-mime-file-element

Conversation

@mihidumh
Copy link
Copy Markdown
Contributor

@mihidumh mihidumh commented May 27, 2026

Summary

Opening or resuming a thread that contains a persisted file element with a null mime crashes the entire thread view with Cannot read properties of null (reading 'startsWith'). Because the error is thrown during render, the whole conversation becomes unviewable.

This is a regression from #2783 ("add image preview") and is easy to hit with any text-based file (.md, .csv, .txt, source code, …): filetype.guess() detects by magic bytes only, so those files are persisted with mime = NULL.

Fixes #2938

Changes

Frontend (the crash fix)

  • Attachment.tsxmime is now optional and the dereference is guarded: mime.startsWith('image/')!!mime?.startsWith('image/').
  • Elements/File.tsx — dropped the unsafe element.mime! non-null assertion that masked the runtime null.

Backend (defense-in-depth)

  • Element.send() — added a filename-based mime fallback (mimetypes.guess_type(path or name)) when filetype.guess() returns None, so text-based files get a sensible mime instead of being persisted as NULL.

Tests

  • Added two tests covering the mime fallback for content-based and path-based text files.

Verification

  • Backend: pytest tests/test_element.py — 45 passed (incl. 2 new)
  • Frontend: tsc --noEmit clean
  • Lint/format: ruff, eslint, prettier all clean on changed files

The frontend guard alone repairs already-broken threads (no data backfill required); the backend fallback prevents new text-file elements from being stored with a null mime.

🤖 Generated with Claude Code


Summary by cubic

Fixes a crash in the thread view when rendering file elements with a null mime. Adds a safe frontend check and a backend mime fallback so existing threads open and new text files get a correct mime.

  • Bug Fixes
    • Frontend: Guard mime in Attachment.tsx and remove the non-null assertion in File.tsx.
    • Backend: In Element.send(), fall back to mimetypes.guess_type() when filetype.guess() returns None (covers .md, .csv, .txt, source files).
    • Tests: Added two tests for the filename-based mime fallback (content and path cases).

Written for commit 43514ab. Summary will update on new commits.

Review in cubic

A persisted file element with a null mime crashed the whole thread view
with "Cannot read properties of null (reading 'startsWith')". The shared
Attachment component dereferenced mime unguarded (regression from Chainlit#2783),
and File.tsx passed element.mime via a non-null assertion despite mime
being optional at runtime.

Guard the mime access in Attachment, drop the unsafe assertion in
File.tsx, and add a filename-based mime fallback in Element.send() so
text-based files (.md, .csv, ...) that filetype.guess() can't detect get
a sensible mime instead of being persisted as NULL.

Fixes Chainlit#2938

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. backend Pertains to the Python backend. bug Something isn't working frontend Pertains to the frontend. unit-tests Has unit tests. labels May 27, 2026
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 4 files

Re-trigger cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Pertains to the Python backend. bug Something isn't working frontend Pertains to the frontend. size:S This PR changes 10-29 lines, ignoring generated files. unit-tests Has unit tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Thread fails to render: Cannot read properties of null (reading 'startsWith') for file elements with null mime

1 participant