Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,24 @@
from unstructured_client.models.operations import CreateJobRequest, DownloadJobOutputRequest
from unstructured_client.models.shared import BodyCreateJob, InputFiles


# Set the variables below this validation function before running this script.
def validate_inputs(api_key, input_dir, output_dir):
# Checks that the settings below have been changed from their placeholder values.
if api_key in ("YOUR_API_KEY_HERE", ""):
raise SystemExit("Set API_KEY to your Unstructured API key before running this script.")
if input_dir in ("/full/path/to/your/input/directory", ""):
raise SystemExit("Set INPUT_DIR to the local directory containing the file (or files) you want to process before running this script.")
if output_dir in ("/full/path/to/your/output/directory", ""):
raise SystemExit("Set OUTPUT_DIR to the local directory where you want the results saved before running this script.")


# API_KEY is included here as a local variable for ease of use in this quickstart.
# This isn't best practice outside of local testing on your own machine. Once
# you've added your real key, don't share this file or check it into any
# repositories.
API_KEY = ""
API_URL = "https://platform-api.transform.unstructured.io"
API_KEY = "YOUR_API_KEY_HERE"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P3: A key pasted with leading or trailing whitespace (common when copying from an editor or .env file) bypasses this guard, since the comparison is verbatim, and is sent unchanged to UnstructuredClient, producing the same late, confusing 401 this guard is meant to prevent. Normalize the key with strip() before the check so whitespace-padded placeholder/empty values are caught too.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At transform/sample-code/extract_quickstart.py, line 14:

<comment>A key pasted with leading or trailing whitespace (common when copying from an editor or `.env` file) bypasses this guard, since the comparison is verbatim, and is sent unchanged to `UnstructuredClient`, producing the same late, confusing 401 this guard is meant to prevent. Normalize the key with `strip()` before the check so whitespace-padded placeholder/empty values are caught too.</comment>

<file context>
@@ -11,7 +11,9 @@
 # you've added your real key, don't share this file or check it into any
 # repositories.
-API_KEY = ""
+API_KEY = "YOUR_API_KEY_HERE"
+if API_KEY in ("YOUR_API_KEY_HERE", ""):
+    raise SystemExit("Set API_KEY to your Unstructured API key before running this script.")
</file context>
Suggested change
API_KEY = "YOUR_API_KEY_HERE"
API_KEY = "YOUR_API_KEY_HERE"
API_KEY = API_KEY.strip()
if API_KEY in ("YOUR_API_KEY_HERE", ""):
raise SystemExit("Set API_KEY to your Unstructured API key before running this script.")

API_URL = "https://platform-api.transform.unstructured.io/api/v1"
# The local directory containing the file (or files) you want to process.
INPUT_DIR = "/full/path/to/your/input/directory"
# The local directory where you want the results saved.
Expand All @@ -23,6 +35,8 @@
# further down in this script does that.
EXTRACTION_PROMPT = "Dates are in MM/DD/YYYY format on the form. Represent them as YYYY-MM-DD. Combine the home address, city, state, and ZIP code fields into a single address string."

validate_inputs(API_KEY, INPUT_DIR, OUTPUT_DIR)

client = UnstructuredClient(
api_key_auth=API_KEY,
server_url=API_URL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,31 @@
from unstructured_client.models.operations import CreateJobRequest, DownloadJobOutputRequest
from unstructured_client.models.shared import BodyCreateJob, InputFiles


# Set the variables below this validation function before running this script.
def validate_inputs(api_key, input_dir, output_dir):
# Checks that the settings below have been changed from their placeholder values.
if api_key in ("YOUR_API_KEY_HERE", ""):
raise SystemExit("Set API_KEY to your Unstructured API key before running this script.")
if input_dir in ("/full/path/to/your/input/directory", ""):
raise SystemExit("Set INPUT_DIR to the local directory containing the file (or files) you want to process before running this script.")
if output_dir in ("/full/path/to/your/output/directory", ""):
raise SystemExit("Set OUTPUT_DIR to the local directory where you want the results saved before running this script.")


# API_KEY is included here as a local variable for ease of use in this quickstart.
# This isn't best practice outside of local testing on your own machine. Once
# you've added your real key, don't share this file or check it into any
# repositories.
API_KEY = ""
API_URL = "https://platform-api.transform.unstructured.io"
API_KEY = "YOUR_API_KEY_HERE"
API_URL = "https://platform-api.transform.unstructured.io/api/v1"
# The local directory containing the file (or files) you want to process.
INPUT_DIR = "/full/path/to/your/input/directory"
# The local directory where you want the results saved.
OUTPUT_DIR = "/full/path/to/your/output/directory"

validate_inputs(API_KEY, INPUT_DIR, OUTPUT_DIR)

client = UnstructuredClient(
api_key_auth=API_KEY,
server_url=API_URL
Expand Down
Loading