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

# Edit Document

> Apply AI-powered edits to a document using natural language instructions

## Edit a Document

Apply AI-powered edits to your document using natural language instructions. The API uses advanced language models to understand your intent and make precise edits to the document.

### Path Parameters

<ParamField path="doc_id" type="string" required>
  The unique identifier of the document to edit (UUID from upload response)
</ParamField>

### Request Body

<ParamField body="editUid" type="string" required>
  Client-provided UUID for idempotency and tracking. Generate a new UUID for
  each edit request.
</ParamField>

<ParamField body="editInstructions" type="string" required>
  Natural language instructions describing the edit to apply (e.g., "Change the
  title to 'Q4 Report' and update all dates to 2024")
</ParamField>

<ParamField body="lookupText" type="string">
  Optional text excerpt to help locate the section to edit in longer documents.
  Use this to pinpoint the exact location for the edit.
</ParamField>

<ParamField body="useLargeModel" type="boolean">
  Use a larger, more capable model for complex edits. Default is false.
</ParamField>

### Response

<ResponseField name="editUid" type="string" required>
  The UUID of the edit operation (same as request)
</ResponseField>

<ResponseField name="editApplied" type="boolean" required>
  Whether the edit was successfully applied
</ResponseField>

<ResponseField name="timeToEdit" type="number" required>
  Time taken to process the edit in seconds
</ResponseField>

<ResponseField name="documentName" type="string" required>
  Name of the edited document
</ResponseField>

<ResponseField name="documentId" type="string" required>
  UUID of the edited document
</ResponseField>

### Code Examples

<CodeGroup>
  ```javascript JavaScript (Fetch) theme={null}
  import { v4 as uuidv4 } from "uuid";

  const docId = "a1b2c3d4-e5f6-7890-abcd-ef1234567890"; // from upload response

  const response = await fetch(
    `https://api.agentoffice.dev/v1/documents/${docId}/edits`,
    {
      method: "POST",
      headers: {
        Authorization: "Bearer YOUR_API_KEY",
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        editUid: uuidv4(),
        editInstructions:
          'Change the title to "Q4 Financial Report" and update the date to December 2024',
        lookupText: "Financial Report",
        useLargeModel: false,
      }),
    }
  );

  const result = await response.json();
  console.log("Edit applied:", result.editApplied);
  console.log("Time taken:", result.timeToEdit, "seconds");
  ```

  ```python Python (Requests) theme={null}
  import requests
  import uuid

  doc_id = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890'  # from upload response
  url = f'https://api.agentoffice.dev/v1/documents/{doc_id}/edits'

  headers = {
      'Authorization': 'Bearer YOUR_API_KEY',
      'Content-Type': 'application/json'
  }

  payload = {
      'editUid': str(uuid.uuid4()),
      'editInstructions': 'Change the title to "Q4 Financial Report" and update the date to December 2024',
      'lookupText': 'Financial Report',
      'useLargeModel': False,
  }

  response = requests.post(url, headers=headers, json=payload)
  result = response.json()
  print(f"Edit applied: {result['editApplied']}")
  print(f"Time taken: {result['timeToEdit']} seconds")
  ```

  ```python Python SDK theme={null}
  from agent_office import AgentOffice
  from uuid import uuid4
  import os

  client = AgentOffice(api_key=os.getenv('AGENT_OFFICE_API_KEY'))

  doc_id = "a1b2c3d4-e5f6-7890-abcd-ef1234567890"  # from upload response

  result = client.edit(
      doc_id=doc_id,
      edit_uid=str(uuid4()),
      edit_instructions='Change the title to "Q4 Financial Report" and update the date to December 2024',
      lookup_text="Financial Report",
      use_large_model=False,
  )

  print(f"Edit applied: {result.edit_applied}")
  print(f"Time taken: {result.time_to_edit} seconds")
  ```

  ```bash cURL theme={null}
  curl -X POST 'https://api.agentoffice.dev/v1/documents/a1b2c3d4-e5f6-7890-abcd-ef1234567890/edits' \
    -H 'Authorization: Bearer YOUR_API_KEY' \
    -H 'Content-Type: application/json' \
    -d '{
      "editUid": "550e8400-e29b-41d4-a716-446655440000",
      "editInstructions": "Change the title to \"Q4 Financial Report\" and update the date to December 2024",
      "lookupText": "Financial Report",
      "useLargeModel": false
    }'
  ```
</CodeGroup>

### Example Response

```json theme={null}
{
  "editUid": "550e8400-e29b-41d4-a716-446655440000",
  "editApplied": true,
  "timeToEdit": 3.45,
  "documentName": "report.docx",
  "documentId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
}
```

## Edit Instructions Best Practices

<Tip>
  **Be specific and clear**: The more specific your instructions, the better the
  results. - ✅ Good: "Change the company name from 'Acme Corp' to 'Global
  Industries Inc.' in the header" - ❌ Vague: "Update the company info"
</Tip>

### Examples of Edit Instructions

<AccordionGroup>
  <Accordion title="Text Replacement">
    Replace all occurrences of "2023" with "2024"
  </Accordion>

  {" "}

  <Accordion title="Content Addition">
    Add a new section titled "Risk Analysis" after the Executive Summary with
    placeholder text
  </Accordion>

  {" "}

  <Accordion title="Formatting Changes">
    Make all section headings bold and increase font size to 16pt
  </Accordion>

  <Accordion title="Complex Edits">
    In the financial table, update Q4 revenue to \$2.5M and recalculate the total.
    Also add a footnote explaining the increase.
  </Accordion>
</AccordionGroup>

## Using lookupText for Long Documents

For documents longer than a few pages, use `lookupText` to help the AI locate the correct section:

```javascript theme={null}
{
  editUid: uuidv4(),
  editInstructions: 'Change the budget amount to $150,000',
  lookupText: 'Marketing Budget Q4' // This helps locate the right section
}
```

## Sequential Edits

All edits to the same document are queued and processed sequentially. Each edit waits for the previous one to complete:

```javascript theme={null}
// Edit 1
await fetch(`https://api.agentoffice.dev/v1/documents/${docId}/edits`, {
  method: "POST",
  headers: {
    Authorization: "Bearer YOUR_API_KEY",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    editUid: uuidv4(),
    editInstructions: "Update the title",
  }),
});

// Edit 2 - will wait for Edit 1 to complete
await fetch(`https://api.agentoffice.dev/v1/documents/${docId}/edits`, {
  method: "POST",
  headers: {
    Authorization: "Bearer YOUR_API_KEY",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    editUid: uuidv4(),
    editInstructions: "Add a conclusion section",
  }),
});
```

## Error Responses

<ResponseExample>
  ```json 404 Not Found theme={null}
  {
    "detail": "Document not found or has expired"
  }
  ```

  ```json 408 Timeout theme={null}
  {
    "detail": "Edit operation timed out after 5 minutes"
  }
  ```

  ```json 500 Edit Failed theme={null}
  {
    "detail": "Edit operation failed: Unable to locate the specified text"
  }
  ```
</ResponseExample>

<Note>
  The document's TTL (time to live) is automatically refreshed after each
  successful edit.
</Note>
