|
| 1 | +"""Custom hook to provide helpful error messages for agent file upload issues.""" |
| 2 | + |
| 3 | +from typing import Optional, Tuple, Union |
| 4 | +import httpx |
| 5 | +from glean.api_client._hooks.types import AfterErrorContext, AfterErrorHook |
| 6 | + |
| 7 | + |
| 8 | +class AgentFileUploadErrorHook(AfterErrorHook): |
| 9 | + """ |
| 10 | + Hook that detects when users incorrectly pass file objects to agents.run() |
| 11 | + and provides clear guidance on the correct two-step upload workflow. |
| 12 | +
|
| 13 | + This hook intercepts 400 errors from agent run operations that contain |
| 14 | + "permission" in the error message, which typically indicates a file was |
| 15 | + passed incorrectly instead of a file ID. |
| 16 | + """ |
| 17 | + |
| 18 | + def after_error( |
| 19 | + self, |
| 20 | + hook_ctx: AfterErrorContext, |
| 21 | + response: Optional[httpx.Response], |
| 22 | + error: Optional[Exception], |
| 23 | + ) -> Union[Tuple[Optional[httpx.Response], Optional[Exception]], Exception]: |
| 24 | + """ |
| 25 | + Intercept agent run errors and enhance them with helpful file upload guidance. |
| 26 | +
|
| 27 | + Args: |
| 28 | + hook_ctx: Context about the operation being performed |
| 29 | + response: The HTTP response (if available) |
| 30 | + error: The exception that was raised |
| 31 | +
|
| 32 | + Returns: |
| 33 | + Either a tuple of (response, error) to continue normal error handling, |
| 34 | + or a new Exception to replace the original error. |
| 35 | + """ |
| 36 | + # Only intercept 400 errors from agent run operations |
| 37 | + if ( |
| 38 | + response is not None |
| 39 | + and response.status_code == 400 |
| 40 | + and hook_ctx.operation_id in ["createAndWaitRun", "createAndStreamRun"] |
| 41 | + ): |
| 42 | + error_message = str(error) if error else "" |
| 43 | + |
| 44 | + # Check if this looks like a file upload error |
| 45 | + # (API returns "permission" error when file objects are passed incorrectly) |
| 46 | + if "permission" in error_message.lower(): |
| 47 | + # Create enhanced error message with clear instructions |
| 48 | + enhanced_message = ( |
| 49 | + "Agent file upload error: When using agents with file inputs, " |
| 50 | + "you must follow a two-step process:\n" |
| 51 | + "\n" |
| 52 | + "1. First, upload files using client.chat.upload_files():\n" |
| 53 | + " from glean.api_client import models\n" |
| 54 | + " \n" |
| 55 | + " # Upload the file\n" |
| 56 | + " upload_result = client.chat.upload_files(\n" |
| 57 | + " files=[\n" |
| 58 | + " models.File(\n" |
| 59 | + " file_name='data.csv',\n" |
| 60 | + " content=file_content # bytes or file-like object\n" |
| 61 | + " )\n" |
| 62 | + " ]\n" |
| 63 | + " )\n" |
| 64 | + "\n" |
| 65 | + "2. Then, pass the returned file IDs (not file objects) in the input field:\n" |
| 66 | + " result = client.agents.run(\n" |
| 67 | + " agent_id='<agent-id>',\n" |
| 68 | + " input={\n" |
| 69 | + " 'my_file': upload_result.files[0].id # Use the file ID string\n" |
| 70 | + " }\n" |
| 71 | + " )\n" |
| 72 | + "\n" |
| 73 | + "For a complete example, see: examples/agent_with_file_upload.py\n" |
| 74 | + f"\nOriginal error: {error_message}" |
| 75 | + ) |
| 76 | + |
| 77 | + # Return new exception with enhanced message |
| 78 | + return Exception(enhanced_message) |
| 79 | + |
| 80 | + # Pass through all other errors unchanged |
| 81 | + return response, error |
0 commit comments