@@ -153,7 +153,17 @@ async def read_document(
153
153
response = client .docs .v1 .content .get (request )
154
154
155
155
if not response .success ():
156
- print (f"Failed to read document: { response .msg } " )
156
+ error_msg = response .msg
157
+ print (f"Failed to read document: { error_msg } " )
158
+
159
+ # Provide more specific error messages for common error codes
160
+ if "not found" in error_msg .lower () or "not exist" in error_msg .lower ():
161
+ raise ValueError (f"Document not found. Please check if the document token is valid: { doc_token } " )
162
+ elif "permission" in error_msg .lower () or "access" in error_msg .lower ():
163
+ raise ValueError (f"Permission denied. You don't have access to this document: { doc_token } " )
164
+ else :
165
+ raise ValueError (f"Error accessing document: { error_msg } " )
166
+
157
167
return None
158
168
159
169
# Clean and format the content
@@ -162,7 +172,8 @@ async def read_document(
162
172
163
173
except Exception as e :
164
174
print (f"Error reading document: { str (e )} " )
165
- return None
175
+ # Re-raise the error to be handled by the calling function
176
+ raise
166
177
167
178
async def summarize_document (
168
179
doc_token : str ,
@@ -177,48 +188,64 @@ async def summarize_document(
177
188
agent_config = get_agent_config ("document" )
178
189
language = agent_config .get ("language" , "en" )
179
190
180
- # Read the document
181
- content = await read_document (doc_token )
182
- if not content :
183
- return None
184
-
185
- # Create and run the summarization agent
186
- agent = create_document_agent (model , language )
187
-
188
191
try :
189
- result = await Runner .run (
190
- agent ,
191
- content ,
192
- run_config = RunConfig (
193
- model_provider = provider ,
194
- model = model ,
195
- model_settings = model_settings
196
- )
197
- )
192
+ # Read the document
193
+ content = await read_document (doc_token )
194
+ if not content :
195
+ raise ValueError (f"Failed to read document with token: { doc_token } " )
196
+
197
+ # Create and run the summarization agent
198
+ agent = create_document_agent (model , language )
198
199
199
- # Validate the output
200
- summary = result .final_output_as (DocumentSummary )
201
- if not summary .questions :
202
- print ("Warning: No questions were generated. Using fallback questions." )
203
- # Generate some basic fallback questions based on the content
204
- title = summary .content
205
- fallback_questions = [
206
- f"What are the key challenges in implementing { title } ?" ,
207
- f"How might recent technological advances impact the future of { title } ?" ,
208
- f"What are the potential limitations or drawbacks of current approaches to { title } ?" ,
209
- f"How could we improve the efficiency and effectiveness of { title } ?" ,
210
- f"What are the ethical considerations surrounding the deployment of { title } ?"
211
- ]
212
- summary .questions = fallback_questions
200
+ try :
201
+ result = await Runner .run (
202
+ agent ,
203
+ content ,
204
+ run_config = RunConfig (
205
+ model_provider = provider ,
206
+ model = model ,
207
+ model_settings = model_settings
208
+ )
209
+ )
210
+
211
+ # Validate the output
212
+ summary = result .final_output_as (DocumentSummary )
213
+ if not summary .questions :
214
+ print ("Warning: No questions were generated. Using fallback questions." )
215
+ # Generate some basic fallback questions based on the content
216
+ title = summary .content
217
+ fallback_questions = [
218
+ f"What are the key challenges in implementing { title } ?" ,
219
+ f"How might recent technological advances impact the future of { title } ?" ,
220
+ f"What are the potential limitations or drawbacks of current approaches to { title } ?" ,
221
+ f"How could we improve the efficiency and effectiveness of { title } ?" ,
222
+ f"What are the ethical considerations surrounding the deployment of { title } ?"
223
+ ]
224
+ summary .questions = fallback_questions
225
+
226
+ return summary
227
+
228
+ except Exception as e :
229
+ print (f"Error during document summarization: { str (e )} " )
230
+ # Only create a fallback summary if we have content
231
+ # Otherwise re-raise the original document access error
232
+ if content :
233
+ title = content .split ('\\ n' )[0 ] if '\\ n' in content else content [:50 ]
234
+ return DocumentSummary (
235
+ content = title ,
236
+ summary = f"Error generating summary: { str (e )[:100 ]} ... Please try again with a different document or model." ,
237
+ questions = []
238
+ )
239
+ else :
240
+ raise
241
+
242
+ except ValueError as e :
243
+ # Re-raise specific ValueError errors (like document not found)
244
+ print (f"Document access error: { str (e )} " )
245
+ raise
213
246
214
- return summary
215
247
except Exception as e :
216
- print (f"Error during document summarization: { str (e )} " )
217
-
218
- # Create a simple fallback summary if JSON parsing fails
219
- title = content .split ('\\ n' )[0 ] if '\\ n' in content else content [:50 ]
220
- return DocumentSummary (
221
- content = title ,
222
- summary = f"Error generating summary: { str (e )[:100 ]} ... Please try again with a different document or model." ,
223
- questions = []
224
- )
248
+ # Convert other exceptions to ValueError with a clear message
249
+ error_msg = f"Error processing document: { str (e )} "
250
+ print (error_msg )
251
+ raise ValueError (error_msg )
0 commit comments