fix(elements): render file elements with null mime instead of crashing#2939
Open
mihidumh wants to merge 1 commit into
Open
fix(elements): render file elements with null mime instead of crashing#2939mihidumh wants to merge 1 commit into
mihidumh wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Opening or resuming a thread that contains a persisted file element with a
nullmime crashes the entire thread view withCannot 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 withmime = NULL.Fixes #2938
Changes
Frontend (the crash fix)
Attachment.tsx—mimeis now optional and the dereference is guarded:mime.startsWith('image/')→!!mime?.startsWith('image/').Elements/File.tsx— dropped the unsafeelement.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)) whenfiletype.guess()returnsNone, so text-based files get a sensible mime instead of being persisted asNULL.Tests
Verification
pytest tests/test_element.py— 45 passed (incl. 2 new)tsc --noEmitcleanThe 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.mimeinAttachment.tsxand remove the non-null assertion inFile.tsx.Element.send(), fall back tomimetypes.guess_type()whenfiletype.guess()returnsNone(covers.md,.csv,.txt, source files).Written for commit 43514ab. Summary will update on new commits.
Review in cubic