import io from typing import Any, Dict import pandas as pd import requests from smolagents import tool @tool def parse_csv(csv_url: str) -> Dict[str, Any]: """ Parse a CSV file and return its content as structured data. Args: csv_url: URL of the CSV file to parse Returns: Dictionary containing parsed CSV data """ try: # Download the CSV response = requests.get(csv_url) response.raise_for_status() # Parse the CSV df = pd.read_csv(io.StringIO(response.text)) # Convert to dictionary format columns = df.columns.tolist() data = df.to_dict(orient="records") # Return basic statistics and preview return { "columns": columns, "row_count": len(data), "preview": data[:5] if len(data) > 5 else data, "column_dtypes": {col: str(df[col].dtype) for col in columns}, } except Exception as e: return {"error": str(e)}