Obtain an API key
Install instructor
instructor
with Writer support by running:Set the `WRITER_API_KEY` environment variable
WRITER_API_KEY
environment variable with your Writer API key or pass it as an argument to the Writer constructor.instructor
with Writer:
name
and age
. It then uses the instructor.from_writer
function to create a client
object that uses the Writer API to extract structured data from a text.
WRITER_JSON
) and Tools mode (WRITER_TOOLS
). Both use the Writer chat completion endpoint to generate structured outputs. The key difference lies in how the model returns the data — either as a raw JSON object or via a structured tool call.
response_format
parametertools
parametertool_calls
field, where it populates the defined tool and its arguments based on the inputtool_calls
field and validates them against the original Pydantic modelWRITER_TOOLS
by default.
Feature | Instructor | Writer SDK |
---|---|---|
Retry mechanism | Built-in retry logic with error handling and prompt editing | Manual implementation required |
Schema handling | Auto-converts a wide range of Python types into JSON Schema via Pydantic | Manual schema conversion required |
Validation & reliability | Enhanced outcomes through Pydantic validation + retries | Same results possible with manual implementation |
Prompt optimization | Built-in prompt minimization and optimization | Manual prompt engineering required |
Model access | Limited to supported models (currently V4 + Tool Calls) | Full access to latest models (V4, V5) and all output modes |
Update speed | Requires updates/contributions for new features | Immediate support for latest Palmyra features |
Create a new project
Add dependencies
Set up your environment variables
.env
file in your project root and add your Writer API key:Create `main.py` file and add imports
main.py
file and add the following imports:asyncio
: This is used to run the application on multiple files concurrently.csv
: This is used to write the extracted data to a CSV file.json
: This is used to write the extracted data to a JSON file.os
: This is used to read the files.instructor
: The instructor
library is used for structured output.writerai
: This is the Writer Python SDK, which is used to interact with the Writer API.typing
and pydantic
: These modules are used to define the types for fields in the UserExtract
class defined in the next step.dotenv
: The dotenv
module is used to load the .env
file that contains your Writer API key.Setting up Writer client
UserExtract
class to represent the data you want to extract:
first_name
and last_name
fields are validated to ensure they start with an uppercase letter and contain only letters. In this example, the email
field is a simple string field, though you could also use a Pydantic field to validate the email format.
Create a function to handle file processing
Create a function to read the files
Extract the file content
Create a function to repair the data
Implementing CSV generation
Create an `example_data` directory
example_data
directory and add some test files:main.py
file to point to the new files.Run the application