Skip to content

Warn when extract() receives a string naming an existing file - #597

Closed
inamdarmihir wants to merge 1 commit into
google:mainfrom
inamdarmihir:fix/249-warn-on-file-path-input
Closed

inamdarmihir wants to merge 1 commit into
google:mainfrom
inamdarmihir:fix/249-warn-on-file-path-input

Conversation

@inamdarmihir

@inamdarmihir inamdarmihir commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Description

extract() treats any string as literal text. When a file path is passed as text_or_documents, the path string itself becomes the document sent to the model, which then tends to map it onto the schema (e.g. a venue extraction returning a directory name) or hallucinate content, and can leak local paths and usernames into results (#249).

This makes extract() emit a UserWarning when the string is a single-line path of an existing file, telling the user to read the file first. Behavior is otherwise unchanged: the string is still processed as literal text, so no existing caller breaks.

The check is deliberately narrow so ordinary text never triggers it or hits the filesystem unnecessarily: it only runs for strings of 1 to 4096 characters with no newline, and uses os.path.isfile (so directories and nonexistent paths don't warn). The warning uses stacklevel=3 so it points at the caller of lx.extract, not the wrapper. The docstring for text_or_documents now states that non-URL strings are never treated as paths.

This is an independent change from #489, which was closed without merging.

Fixes #249

Other

How Has This Been Tested?

Added FilePathInputWarningTest in tests/init_test.py: absolute and relative existing paths warn (message contains the path); plain text, a nonexistent path, a directory, an empty string, multi-line text starting with a real path, a string with a null byte, and a Document list do not warn; strings over the length limit skip the isfile call; the warning is attributed to the caller's file.

$ pytest tests
995 passed, 21 skipped, 3 failed

The 3 failures are in tests/create_provider_plugin_test.py (offline setuptools install) and fail identically on an unmodified main.

$ isort langextract tests && pyink langextract tests --config pyproject.toml

Checklist:

  • I have read and acknowledged Google's Open Source
    Code of conduct.
  • I have read the
    Contributing
    page, and I either signed the Google
    Individual CLA
    or am covered by my company's
    Corporate CLA.
  • I have discussed my proposed solution with code owners in the linked
    issue(s) and we have agreed upon the general approach.
  • I have made any needed documentation changes, or noted in the linked
    issue(s) that documentation elsewhere needs updating.
  • I have added tests, or I have ensured existing tests cover the changes
  • I have followed Google's Python Style Guide and ran pylint over the affected code.

@github-actions github-actions Bot added the size/S Pull request with 50-150 lines changed label Oct 29, 2026
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

⚠️ Branch Update Required

Your branch is 1 commits behind main. Please update your branch to ensure CI checks run with the latest code:

git fetch origin main
git merge origin/main
git push

Note: Enable "Allow edits by maintainers" to allow automatic updates.

@aksg87

aksg87 commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Thanks for taking a look at #249, @inamdarmihir. After reviewing the tradeoffs, we want to avoid adding filesystem stat checks on short text inputs in extract() and keep string inputs strictly in-memory literal text as documented, so we are going to pass on this change and close the PR. Appreciate your other contributions in this cycle.

@aksg87 aksg87 closed this Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/S Pull request with 50-150 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

potential exposure of local path and identity of the user

2 participants