Here’s a simple example of parsing a PDF document from a URL:
import osfrom reducto import Reductoclient = Reducto( api_key=os.environ.get("REDUCTO_API_KEY"), # This is the default and can be omitted # or 'production' | 'au'; defaults to "production". environment="eu",)response = client.parse.run( input="https://pdfobject.com/pdf/sample.pdf",)
The api_key parameter is optional if you’ve set the REDUCTO_API_KEY environment variable.
For asynchronous operations, use AsyncReducto instead of Reducto:
import osimport asynciofrom reducto import AsyncReductoclient = AsyncReducto( api_key=os.environ.get("REDUCTO_API_KEY"), # This is the default and can be omitted # or 'production' | 'au'; defaults to "production". environment="eu",)async def main() -> None: response = await client.parse.run( input="https://pdfobject.com/pdf/sample.pdf", ) print(response)asyncio.run(main())
The async and sync clients have identical APIs - just add await to method calls when using AsyncReducto.
For improved concurrency performance, you can use the aiohttp backend:
1
Install aiohttp support
pip install reductoai[aiohttp]
2
Configure the client
import osimport asynciofrom reducto import DefaultAioHttpClientfrom reducto import AsyncReductoasync def main() -> None: async with AsyncReducto( api_key=os.environ.get("REDUCTO_API_KEY"), # This is the default and can be omitted http_client=DefaultAioHttpClient(), ) as client: response = await client.parse.run( input="https://pdfobject.com/pdf/sample.pdf", ) print(response)asyncio.run(main())
Handle API errors gracefully with exception handling:
import reductofrom reducto import Reductoclient = Reducto()try: client.parse.run( input="https://pdfobject.com/pdf/sample.pdf", )except reducto.APIConnectionError as e: print("The server could not be reached") print(e.__cause__) # an underlying Exception, likely raised within httpx.except reducto.RateLimitError as e: print("A 429 status code was received; we should back off a bit.")except reducto.APIStatusError as e: print("Another non-200-range status code was received") print(e.status_code) print(e.response)