From 222655c0b1cf7974bf50b854ed5ef5611d0689d2 Mon Sep 17 00:00:00 2001 From: Ridwannurudeen Date: Fri, 6 Feb 2026 12:49:47 +0100 Subject: [PATCH] fix: standardize tool function docstrings to Google Python style Closes #14 LangChain's `_parse_python_function_docstring` expects docstrings to follow the Google Python style guide in order to correctly extract function descriptions and parameter metadata for OpenAI tool schemas. The existing docstrings in `functions.py` had several formatting issues that caused `convert_to_openai_tool` to produce malformed tool definitions: 1. Missing 4-space indentation under Args/Returns sections (get_financial_statements, get_key_financial_ratios, get_analyst_recommendations, get_dividend_data, get_company_news, get_technical_indicators, get_company_profile) 2. Inconsistent 2-space indentation instead of 4-space (get_current_stock_price) 3. Note section leaking into tool description (code_interpreter) 4. Detailed Keys block under Returns parsed as description text (get_stock_fundamentals) 5. Missing blank line between Args and Returns sections (google_search_and_scrape) 6. Return type mismatch between docstring (list) and type hint (dict) (google_search_and_scrape) All 11 @tool-decorated functions now follow a consistent format: - Summary on the opening line (no blank line after triple quotes) - 4-space indented Args section with (type) annotations - 4-space indented Returns section - Blank line separating each section - No extraneous sections (Note, Keys) that confuse the parser --- functions.py | 108 ++++++++++++++++++++------------------------------- 1 file changed, 42 insertions(+), 66 deletions(-) diff --git a/functions.py b/functions.py index 794627a..d67c0eb 100644 --- a/functions.py +++ b/functions.py @@ -13,22 +13,17 @@ @tool def code_interpreter(code_markdown: str) -> dict | str: - """ - Execute the provided Python code string on the terminal using exec. - - The string should contain valid, executable and pure Python code in markdown syntax. - Code should also import any required Python packages. + """Execute the provided Python code string on the terminal using exec. Args: code_markdown (str): The Python code with markdown syntax to be executed. - For example: ```python\n\n``` + The string should contain valid, executable and pure Python code + in markdown syntax. Code should also import any required packages. Returns: - dict | str: A dictionary containing variables declared and values returned by function calls, - or an error message if an exception occurred. - - Note: - Use this function with caution, as executing arbitrary code can pose security risks. + dict | str: A dictionary containing variables declared and values + returned by function calls, or an error message if an exception + occurred. """ try: # Extracting code from Markdown code block @@ -64,14 +59,14 @@ def code_interpreter(code_markdown: str) -> dict | str: @tool def google_search_and_scrape(query: str) -> dict: - """ - Performs a Google search for the given query, retrieves the top search result URLs, - and scrapes the text content and table data from those pages in parallel. + """Perform a Google search and scrape text content from top results. Args: query (str): The search query. + Returns: - list: A list of dictionaries containing the URL, text content, and table data for each scraped page. + dict: A list of dictionaries containing the URL, text content, and + table data for each scraped page. """ num_results = 2 url = 'https://www.google.com/search' @@ -101,15 +96,14 @@ def google_search_and_scrape(query: str) -> dict: @tool def get_current_stock_price(symbol: str) -> float: - """ - Get the current stock price for a given symbol. + """Get the current stock price for a given symbol. - Args: - symbol (str): The stock symbol. + Args: + symbol (str): The stock symbol. - Returns: - float: The current stock price, or None if an error occurs. - """ + Returns: + float: The current stock price, or None if an error occurs. + """ try: stock = yf.Ticker(symbol) # Use "regularMarketPrice" for regular market hours, or "currentPrice" for pre/post market @@ -121,27 +115,15 @@ def get_current_stock_price(symbol: str) -> float: @tool def get_stock_fundamentals(symbol: str) -> dict: - """ - Get fundamental data for a given stock symbol using yfinance API. + """Get fundamental data for a given stock symbol using yfinance API. Args: symbol (str): The stock symbol. Returns: - dict: A dictionary containing fundamental data. - Keys: - - 'symbol': The stock symbol. - - 'company_name': The long name of the company. - - 'sector': The sector to which the company belongs. - - 'industry': The industry to which the company belongs. - - 'market_cap': The market capitalization of the company. - - 'pe_ratio': The forward price-to-earnings ratio. - - 'pb_ratio': The price-to-book ratio. - - 'dividend_yield': The dividend yield. - - 'eps': The trailing earnings per share. - - 'beta': The beta value of the stock. - - '52_week_high': The 52-week high price of the stock. - - '52_week_low': The 52-week low price of the stock. + dict: A dictionary containing fundamental data including symbol, + company_name, sector, industry, market_cap, pe_ratio, pb_ratio, + dividend_yield, eps, beta, 52_week_high, and 52_week_low. """ try: stock = yf.Ticker(symbol) @@ -167,14 +149,14 @@ def get_stock_fundamentals(symbol: str) -> dict: @tool def get_financial_statements(symbol: str) -> dict: - """ - Get financial statements for a given stock symbol. + """Get financial statements for a given stock symbol. Args: - symbol (str): The stock symbol. + symbol (str): The stock symbol. Returns: - dict: Dictionary containing financial statements (income statement, balance sheet, cash flow statement). + dict: Dictionary containing financial statements including income + statement, balance sheet, and cash flow statement. """ try: stock = yf.Ticker(symbol) @@ -186,14 +168,13 @@ def get_financial_statements(symbol: str) -> dict: @tool def get_key_financial_ratios(symbol: str) -> dict: - """ - Get key financial ratios for a given stock symbol. + """Get key financial ratios for a given stock symbol. Args: - symbol (str): The stock symbol. + symbol (str): The stock symbol. Returns: - dict: Dictionary containing key financial ratios. + dict: Dictionary containing key financial ratios. """ try: stock = yf.Ticker(symbol) @@ -205,14 +186,13 @@ def get_key_financial_ratios(symbol: str) -> dict: @tool def get_analyst_recommendations(symbol: str) -> pd.DataFrame: - """ - Get analyst recommendations for a given stock symbol. + """Get analyst recommendations for a given stock symbol. Args: - symbol (str): The stock symbol. + symbol (str): The stock symbol. Returns: - pd.DataFrame: DataFrame containing analyst recommendations. + pd.DataFrame: DataFrame containing analyst recommendations. """ try: stock = yf.Ticker(symbol) @@ -224,14 +204,13 @@ def get_analyst_recommendations(symbol: str) -> pd.DataFrame: @tool def get_dividend_data(symbol: str) -> pd.DataFrame: - """ - Get dividend data for a given stock symbol. + """Get dividend data for a given stock symbol. Args: - symbol (str): The stock symbol. + symbol (str): The stock symbol. Returns: - pd.DataFrame: DataFrame containing dividend data. + pd.DataFrame: DataFrame containing dividend data. """ try: stock = yf.Ticker(symbol) @@ -243,14 +222,13 @@ def get_dividend_data(symbol: str) -> pd.DataFrame: @tool def get_company_news(symbol: str) -> pd.DataFrame: - """ - Get company news and press releases for a given stock symbol. + """Get company news and press releases for a given stock symbol. Args: - symbol (str): The stock symbol. + symbol (str): The stock symbol. Returns: - pd.DataFrame: DataFrame containing company news and press releases. + pd.DataFrame: DataFrame containing company news and press releases. """ try: news = yf.Ticker(symbol).news @@ -261,14 +239,13 @@ def get_company_news(symbol: str) -> pd.DataFrame: @tool def get_technical_indicators(symbol: str) -> pd.DataFrame: - """ - Get technical indicators for a given stock symbol. + """Get technical indicators for a given stock symbol. Args: - symbol (str): The stock symbol. + symbol (str): The stock symbol. Returns: - pd.DataFrame: DataFrame containing technical indicators. + pd.DataFrame: DataFrame containing technical indicators. """ try: indicators = yf.Ticker(symbol).history(period="max") @@ -279,14 +256,13 @@ def get_technical_indicators(symbol: str) -> pd.DataFrame: @tool def get_company_profile(symbol: str) -> dict: - """ - Get company profile and overview for a given stock symbol. + """Get company profile and overview for a given stock symbol. Args: - symbol (str): The stock symbol. + symbol (str): The stock symbol. Returns: - dict: Dictionary containing company profile and overview. + dict: Dictionary containing company profile and overview. """ try: profile = yf.Ticker(symbol).info