> ## Documentation Index
> Fetch the complete documentation index at: https://developer.box.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Summarize files with Box AI in Python

> Learn how to use Box AI to automatically extract structured data from documents and store it as searchable metadata using the Box Python SDK.

export const SignupCTA = ({children}) => {
  return <div className="flex flex-wrap items-center gap-4 p-5 rounded-lg border border-gray-200 dark:border-gray-700 my-6" style={{
    background: "linear-gradient(135deg, rgba(0, 97, 213, 0.06), rgba(0, 97, 213, 0.02))"
  }}>
      <div className="flex-1 text-sm leading-relaxed text-gray-700 dark:text-gray-300" style={{
    minWidth: "280px"
  }}>
        {children}
      </div>
      <div className="flex flex-col items-center gap-2">
        <a href="https://account.box.com/signup/developer#ty9l3" className="signup-cta-button inline-flex items-center whitespace-nowrap px-5 py-2 text-sm font-semibold text-white no-underline">
          Get started for free
        </a>
        <a href="https://account.box.com/developers/console" className="signup-cta-login text-xs text-gray-500 dark:text-gray-400 no-underline whitespace-nowrap">
          Already have an account? Log in
        </a>
      </div>
    </div>;
};

export const Link = ({href, children, className, ...props}) => {
  const localizedHref = href;
  return <a href={localizedHref} className={className} {...props}>
      {children}
    </a>;
};

<Link href="/ai">Box AI</Link> exposes AI capabilities that enable developers to interrogate documents through a single API call. This powerful feature transforms unstructured
document content into instant insights without the need to build and maintain complex Retrieval Augmented Generation (RAG) pipelines.

This quick start demonstrates how to configure the Box Python SDK and use Box AI to summarize files in Box.

<SignupCTA>
  A free developer account gives you access to the Box AI API. Try document summarization, question answering, and metadata extraction through the API.
</SignupCTA>

<Steps>
  <Step title="Create and configure a Box application">
    The first step for any Box Platform integration is to create and configure a Box
    application.

    1. Go to <Link href="https://app.box.com/developers/console">Box Developer Console</Link>.
    2. For this quick start, create an App with the `Client Credentials Grant`
       application type.
    3. Once the app is created, enable the following scopes:
       * Read all files and folders stored in Box
       * Manage AI

    For more information about creating a new Box application, see <Link href="/guides/getting-started/first-application#create-your-first-application">Create your first application</Link>.
  </Step>

  <Step title="Upload a test file">
    To use Box AI to get insights from documents, you need a file in Box. For this quick start, use this document containing
    <Link href="/static/quickstarts/summarize/files/ai-summary-document.pdf">federal code</Link>.

    1. Download the test document, and then drag and drop it into your Box account.
    2. Get the file ID by opening the file in Box and inspecting the URL.
       The last part of the path is your file ID. For example, the URL might look like this:
       `https://app.box.com/file/2064123286902`

    In this case, the file ID is `2064123286902`.
  </Step>

  <Step title="Configure the environment">
    Now set up your development environment to run this quickstart. This tutorial uses Python and the
    latest Box Python SDK to run the code. Make sure you have Python 3.11 or higher installed on your machine.

    1. Create a new directory for your project and navigate into it.
    2. Create a virtual environment:
       ```bash theme={null}
       python3 -m venv .venv
       source .venv/bin/activate
       ```
    3. Install the Box Python SDK:
       ```bash theme={null}
       pip install "boxsdk~=10"
       ```
    4. Install the `python-dotenv` package to load environment variables from the `.env` file:
       ```bash theme={null}
       pip install python-dotenv
       ```
    5. Create an `.env` file in the root of your project directory and add the following
       environment variables, replacing the placeholder values with your actual Box app credentials
       and the IDs from the previous steps:
       ```bash theme={null}
        BOX_DEVELOPER_TOKEN=your_box_developer_token

        BOX_FILE_ID=your_box_file_id
       ```

    To get your developer token, go to the <Link href="https://app.box.com/developers/console">Box Developer Console</Link>,
    open your app, and navigate to the **Configuration** tab.

    6. Click **Generate Developer Token** to create a new token.

    <Tip>
      For simplicity, this quick start uses a short-lived developer token. In production, you
      should authenticate using your app’s configured method (for example, Client Credentials Grant)
      instead of a developer token.
    </Tip>
  </Step>

  <Step title="Create the summarize.py file">
    Your development environment is now ready to create the Python script to summarize the document using Box AI.

    1. Create a new file named `summarize.py` in the root of your project directory and add the following code:

       ```python theme={null}
       import os

       from dotenv import load_dotenv

       from box_sdk_gen import (
           AiItemAsk,
           AiItemAskTypeField,
           BoxClient,
           BoxDeveloperTokenAuth,
           CreateAiAskMode
       )

       load_dotenv()

       developer_token = os.getenv("BOX_DEVELOPER_TOKEN")
       file_id = os.getenv("BOX_FILE_ID")

       def get_box_client(token: str) -> BoxClient:
           
           if not developer_token:
               raise ValueError("BOX_DEVELOPER_TOKEN is not set in environment variables.")
           
           auth = BoxDeveloperTokenAuth(token=token)
           client = BoxClient(auth=auth)

           return client

       def main():
           client = get_box_client(token=developer_token)

           me = client.users.get_user_me()
           print(f"My user ID is {me.id}")


       if __name__ == "__main__":
           main()
       ```

    This code loads the environment variables from the `.env` file, initializes the Box SDK client,
    and prints the current user's ID to validate that the client is working correctly.

    2. Run the script using the following command in your terminal:

       ```bash theme={null}
       python summarize.py
       ```

       If the Box SDK client is set up correctly, the console displays your user ID. For example:

       ```bash theme={null}
       My user ID is 123456789
       ```
  </Step>

  <Step title="Summarize the file">
    With a working Box SDK client, you can add the code to summarize the document using Box AI.

    1. Between the `get_box_client` function and the `main` function, add the following function:

       ```python theme={null}
       def summarize_file(client: BoxClient, file_id: str) -> str:
           prompt = """
           Summarize the content of the following file in a few sentences.
           """

           summary = client.ai.create_ai_ask(
               CreateAiAskMode.SINGLE_ITEM_QA,
               prompt,
               [
                   AiItemAsk(
                       id=file_id,
                       type=AiItemAskTypeField.FILE
                   )
               ]
           )
           
           return summary.to_dict()['answer']
       ```

       This function uses the Box AI `create_ai_ask` method to interrogate the specified file.
       Your BoxClient and file ID created earlier are sent to the function, which
       returns the summary of the file as a string.

    2. Add the function call to summarize the file in the `main` function. Ensure that the new `main` function contains the following logic:

       ```python theme={null}
       def main():
           client = get_box_client(token=developer_token)

           # Get the current user and print the ID to validate the client is working
           me = client.users.get_user_me()
           print(f"My user ID is {me.id}")

           summary = summarize_file(client=client, file_id=file_id)

           # Print the extracted metadata
           print(f"\n\nSummary: {summary}")
       ```

       The SDK handles the API call to Box AI and returns the summary as an
       <Link href="/reference/resources/ai-response--full">`AiResponseFull`</Link> object. In this quick start,
       the code converts this object to a dictionary and returns the `answer` field that contains the summary.

    3. Print out the summary to the console to verify that the operation was successful by running the following command in your terminal:

       ```bash theme={null}
       python summarize.py
       ```

       If the summarization was successful, the console displays your user ID followed by the summary from the test document.

       ```bash theme={null}
       My user ID is 123456789

       Summary: **Summary of 28 CFR § 2.20 (Paroling Policy Guidelines)**

       - The document sets forth the U.S. Parole Commission’s purpose and framework for national parole policy, emphasizing guidelines to promote consistent, equitable parole decisions while preserving individualized review.
       - It provides a matrix of customary total time-to-release ranges by offense severity categories (1–8) and offender parole prognosis (salient factor scores), noting guidelines apply to inmates with good institutional adjustment and are discretionary.
       - A comprehensive Offense Behavior Severity Index (Chapters 1–12) assigns categories to specific crimes (homicide, assault, theft, drugs, firearms, national defense, etc.) with detailed rules, exceptions, and examples for grading offenses (including monetary thresholds and drug-quantity bands).
       - Chapter Thirteen contains general notes, multiple-offense scoring guidance, definitions of key terms, and the Salient Factor Scoring Manual explaining how to compute parole prognosis points (prior convictions, commitments, age, recent commitment-free period, probation/parole status, and older-offender adjustment).
       - The document also references reparole guidelines and authority to revise guidelines; aggravated crimes and Category Eight cases receive special treatment.
       ```
  </Step>
</Steps>

## Resources

* <Link href="https://github.com/box-community/box-quickstarts/tree/main/box-ai-summarize-quickstart">Final code</Link>
* <Link href="/reference/post-ai-ask">Box AI Ask API Reference</Link>
* <Link href="https://github.com/box/box-python-sdk/tree/main">Box Python SDK</Link>
