@@ -48,6 +48,25 @@ def save_df_to_csv(df, base_name):
48
48
df_clean .to_csv (file_path , index = False )
49
49
return str (file_path ), list (df_clean .columns )
50
50
51
+ def save_json_to_file (data , base_name ):
52
+ file_path = OUTPUTS_DIR / f"{ base_name } .json"
53
+ if file_path .exists ():
54
+ unique_id = uuid .uuid4 ().hex [:8 ]
55
+ file_path = OUTPUTS_DIR / f"{ base_name } _{ unique_id } .json"
56
+ with open (file_path , "w" ) as f :
57
+ json .dump (data , f , indent = 2 )
58
+ # Schema: for dict, top-level keys; for list, type of first element or 'list'; else type
59
+ if isinstance (data , dict ):
60
+ schema = list (data .keys ())
61
+ preview = {k : data [k ] for k in list (data )[:PREVIEW_ROWS ]}
62
+ elif isinstance (data , list ):
63
+ schema = [type (data [0 ]).__name__ ] if data else ["list" ]
64
+ preview = data [:PREVIEW_ROWS ]
65
+ else :
66
+ schema = [type (data ).__name__ ]
67
+ preview = data
68
+ return str (file_path ), schema , preview
69
+
51
70
class FinancialType (str , Enum ):
52
71
income_stmt = "income_stmt"
53
72
quarterly_income_stmt = "quarterly_income_stmt"
@@ -134,8 +153,13 @@ def get_stock_info_sync(ticker):
134
153
logger .error (f"Company ticker { ticker } not found." )
135
154
return json .dumps ({"error" : f"Company ticker { ticker } not found." })
136
155
info = company .info
156
+ file_path , schema , preview = save_json_to_file (info , f"{ ticker } _stock_info" )
137
157
logger .info (f"Returning stock info for { ticker } " )
138
- return json .dumps (info )
158
+ return json .dumps ({
159
+ "file_path" : file_path ,
160
+ "schema" : schema ,
161
+ "preview" : preview
162
+ })
139
163
140
164
@yfinance_server .tool (
141
165
name = "get_stock_info" ,
@@ -173,13 +197,18 @@ def get_yahoo_finance_news_sync(ticker):
173
197
description = news_item .get ("content" , {}).get ("description" , "" )
174
198
url = news_item .get ("content" , {}).get ("canonicalUrl" , {}).get ("url" , "" )
175
199
news_list .append (
176
- f"Title: { title } \n Summary: { summary } \n Description: { description } \n URL: { url } "
200
+ { "title" : title , "summary" : summary , "description" : description , "url" : url }
177
201
)
178
202
if not news_list :
179
203
logger .warning (f"No news found for company with ticker { ticker } ." )
180
204
return json .dumps ({"error" : f"No news found for company that searched with { ticker } ticker." })
205
+ file_path , schema , preview = save_json_to_file (news_list , f"{ ticker } _news" )
181
206
logger .info (f"Returning news for { ticker } " )
182
- return json .dumps ({"news" : news_list })
207
+ return json .dumps ({
208
+ "file_path" : file_path ,
209
+ "schema" : schema ,
210
+ "preview" : preview
211
+ })
183
212
184
213
@yfinance_server .tool (
185
214
name = "get_yahoo_finance_news" ,
@@ -335,8 +364,14 @@ def get_option_expiration_dates_sync(ticker):
335
364
if company .isin is None :
336
365
logger .error (f"Company ticker { ticker } not found." )
337
366
return json .dumps ({"error" : f"Company ticker { ticker } not found." })
367
+ dates = list (company .options )
368
+ file_path , schema , preview = save_json_to_file (dates , f"{ ticker } _option_expiration_dates" )
338
369
logger .info (f"Returning option expiration dates for { ticker } " )
339
- return json .dumps (list (company .options ))
370
+ return json .dumps ({
371
+ "file_path" : file_path ,
372
+ "schema" : schema ,
373
+ "preview" : preview
374
+ })
340
375
341
376
@yfinance_server .tool (
342
377
name = "get_option_expiration_dates" ,
0 commit comments