Skip to content

feat(studio): multi-file unstructured seed upload with better backend extraction - #4468

Merged
shimmyshimmer merged 13 commits into
unslothai:mainfrom
wasimysaid:fix/reciper-enchantments
Mar 20, 2026
Merged

shimmyshimmer merged 13 commits into
unslothai:mainfrom
wasimysaid:fix/reciper-enchantments

Conversation

@wasimysaid

@wasimysaid wasimysaid commented Mar 19, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Replace single-file unstructured seed input with drag-and-drop multi-file upload (PDF, DOCX, TXT, MD)
  • Move text extraction from client to backend and convert (PDF→markdown) and (DOCX→markdown)
  • Round-robin preview so all source files are represented in the 10-row preview
  • Persist uploaded file metadata across dialog close/reopen and page reload
  • Store uploads in ~/.unsloth/studio/assets/datasets/unstructured-uploads/
  • Input validation, path traversal protection, and React state fixes

Test plan

  • Upload multiple PDF/DOCX/TXT files via drag-and-drop and file picker
  • Verify preview loads automatically after uploads finish
  • Close and reopen dialog, file list and preview should persist
  • Refresh page, file list should restore from saved config
  • Delete individual files and verify cleanup
  • Run full recipe with unstructured seed block

Ensures every uploaded file is represented in the preview table
by cycling through sources instead of just taking the first N rows.
- Disable pymupdf4llm OCR with write_images=False, show_progress=False
- Replace onAllUploaded callback with useEffect that detects uploading→done
  transition (avoids stale closure reading empty file IDs)
- Fix importer to preserve file IDs from saved recipes instead of clearing
  (clearing only happens at share time via sanitizeSeedForShare)
Validate block_id/file_id with alphanumeric regex to prevent path
traversal, use exact stem match for file deletion, add error handling
for metadata writes and empty files, fix React stale closures and
object mutations in upload loop, and correct validation logic for
unstructured seed resolved_paths.
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant enhancement to the data designer by enabling multi-file unstructured seed uploads. It shifts text extraction to the backend for improved performance and security, and enhances the user experience with a more robust and persistent upload process. Input validation and state management improvements ensure a more reliable feature.

Highlights

  • Multi-File Upload: Replaced the single-file unstructured seed input with a drag-and-drop multi-file upload feature, supporting PDF, DOCX, TXT, and MD files.
  • Backend Text Extraction: Moved text extraction from the client to the backend, including conversion from PDF to Markdown and DOCX to Markdown.
  • Preview Enhancement: Implemented a round-robin preview to ensure all source files are represented in the 10-row preview.
  • Persistence: Ensured uploaded file metadata persists across dialog close/reopen and page reload.
  • Storage: Files are now stored in ~/.unsloth/studio/assets/datasets/unstructured-uploads/.
  • Validation and Fixes: Added input validation, path traversal protection, and React state fixes.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a70cf751f3

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

…fect

Promote legacy source.path into resolved_paths for old unstructured
recipes, clear source.paths in share sanitizer to prevent leaking local
filesystem paths, and gate file sync effect to dialog open transition
so users can actually delete all uploaded files.
@wasimysaid
wasimysaid force-pushed the fix/reciper-enchantments branch from 06eb527 to e5deb74 Compare March 19, 2026 17:22

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e5deb74283

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 35395d2df6

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@wasimysaid
wasimysaid force-pushed the fix/reciper-enchantments branch from 35395d2 to ba4ff1d Compare March 19, 2026 20:03

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ba4ff1d771

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@wasimysaid
wasimysaid force-pushed the fix/reciper-enchantments branch from ba4ff1d to 2a2ed37 Compare March 19, 2026 20:18

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2a2ed37b82

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread studio/backend/models/data_recipe.py Resolved

@danielhanchen danielhanchen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Review Summary

The fitView changes look good. The multi-file unstructured upload is a solid feature direction, but there are several correctness and backward compatibility issues that should be addressed before merging.

Blocking Issues

  1. Legacy path backward compatibility break -- UnstructuredSeedSource now requires paths: list[str] with no fallback from the legacy path field. Any previously persisted recipe with {"seed_type": "unstructured", "path": "..."} will fail validation. A model_validator(mode="before") shim that normalizes path into paths=[path] would fix this server-side.

  2. Legacy single-file PDF/DOCX inspect flow is broken -- The legacy inspect-upload path for seed_source_type="unstructured" routes through _read_preview_rows_from_unstructured_file -> build_unstructured_preview_rows -> load_unstructured_text_file, which now only accepts .txt/.md. Any caller still using the legacy single-file base64 upload with .pdf or .docx files will get Unsupported unstructured seed file type.

  3. CSV in-place rewrite is not atomic -- full_df.to_csv(path, ...) writes directly to the original upload file. If the process is interrupted mid-write, the file is left in a corrupt state. The parquet materialization code correctly uses a tmp-then-rename pattern (line 155-157 of chunking.py); the CSV rewrite should do the same.

  4. React state updater anti-patterns in UnstructuredDropZone -- Two issues: (a) handleFiles mutates an outer overLimit variable inside a setState updater and reads it synchronously after -- React 18 does not guarantee updaters run synchronously before the next line. (b) handleRemove fires a network DELETE request inside a setState updater, which violates React's rules (updaters should be pure; strict mode may call them twice, causing double-deletes).

  5. Stale config spread in unstructured onUpdate -- The unstructured branch uses onUpdate({ ...config, ... }) while hf and local branches only pass the delta. The ...config spread can overwrite interim user edits with stale values captured at callback creation time.

Non-Blocking Issues

  • tmp.rename(cached) at chunking.py:194 should be tmp.replace(cached) for consistency with the single-file path (line 157) and for atomicity.
  • Empty file_ids=[] passes pydantic validation and causes an unhandled ValueError -> HTTP 500; should reject with 400.
  • On extraction failure, extracted_path is not cleaned up if write_text() partially writes before raising.
  • resolved_paths ?? [config.hf_path] in builders-seed.ts:74 does not handle empty array (empty array is truthy, so ?? does not trigger the fallback).

Comment thread studio/backend/models/data_recipe.py Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a020955819

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@danielhanchen danielhanchen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

All 10 previously flagged issues have been addressed. Nice work on the fixes.

What was fixed

  1. Legacy path backward compat -- _normalize_legacy_path model_validator added
  2. Legacy PDF/DOCX inspect -- restricted to .txt/.md with clear error message
  3. CSV atomic rewrite -- now uses tmp-then-replace pattern
  4. React overLimit race -- uses filesRef.current instead of async setState
  5. DELETE inside setState -- side effects moved outside the updater, with deletedIdsRef guard
  6. ...config spread -- removed from unstructured onUpdate
  7. tmp.rename() -- changed to .replace() for consistency
  8. Empty file_ids=[] -- validated in both model validator and route handler
  9. extracted_path cleanup -- moved assignment before try, cleanup in except
  10. resolved_paths empty array -- proper length check fallback

Additional improvements

  • _get_block_total_size now only counts raw upload files (excludes derived .extracted.txt and .meta.json)
  • MAX_TOTAL_SIZE reduced from 500MB to 100MB
  • handleUnstructuredFilesChange wrapper syncs config on file changes
  • Upload endpoint accepts existing_file_ids for quota tracking
  • f.unlink(missing_ok=True) in delete endpoint

Minor non-blocking suggestions for future consideration

  • handleUnstructuredFilesChange uses queueMicrotask inside a setState updater to defer onUpdate. This works correctly in production but fires twice in React 18 Strict Mode (dev). A useEffect watching unstructuredFiles would be the more idiomatic approach.
  • The pydantic model validator already rejects file_ids=[] with a 422, making the route-level HTTPException(400) check unreachable dead code.
  • _LEGACY_UNSTRUCTURED_EXTS is defined inside the function body -- could be moved to module scope for consistency.

@shimmyshimmer
shimmyshimmer merged commit dd283b0 into unslothai:main Mar 20, 2026
shibizhao pushed a commit to shibizhao/unsloth-npu that referenced this pull request Apr 7, 2026
… extraction (unslothai#4468)

* fix(recipe-studio): prevent fitView from zooming to wrong location on recipe load

* feat: add pymupdf/python-docx deps and unstructured uploads storage root

* feat: add POST /seed/upload-unstructured-file endpoint

* feat: add multi-file chunking with source_file column

* feat: update frontend types and API layer for multi-file upload

* feat: round-robin preview rows across source files

Ensures every uploaded file is represented in the preview table
by cycling through sources instead of just taking the first N rows.

* fix: disable OCR, fix auto-load timing, fix persistence on reload

- Disable pymupdf4llm OCR with write_images=False, show_progress=False
- Replace onAllUploaded callback with useEffect that detects uploading→done
  transition (avoids stale closure reading empty file IDs)
- Fix importer to preserve file IDs from saved recipes instead of clearing
  (clearing only happens at share time via sanitizeSeedForShare)

* fix: harden unstructured upload with input validation and state fixes

Validate block_id/file_id with alphanumeric regex to prevent path
traversal, use exact stem match for file deletion, add error handling
for metadata writes and empty files, fix React stale closures and
object mutations in upload loop, and correct validation logic for
unstructured seed resolved_paths.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix: address PR review - legacy path import, share sanitizer, sync effect

Promote legacy source.path into resolved_paths for old unstructured
recipes, clear source.paths in share sanitizer to prevent leaking local
filesystem paths, and gate file sync effect to dialog open transition
so users can actually delete all uploaded files.

* fix: CSV column fix (BOM + whitespace + unnamed index re-save) for unslothai#4470

* fix: harden unstructured upload flow and polish dialog UX

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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