Skip to main content
Loading navigation
Back to prompt library

Implement Code Analysis Tool

Linked challenge: AI Code Audit & Optimization Agent

Prompt source

implementation18 lines4 sectionsNo variables1 code block
Implement a Python function, `analyze_python_code(code: str) -> dict`, that simulates a code analysis tool. This function should take a Python code string and return a dictionary of potential issues (e.g., syntax errors, common linting problems, simple security patterns). This will be registered as a `tool_function` with the OpenAI Assistants API. Ensure it can be called by your agents.

```python
import openai

# Placeholder for your code analysis tool function
def analyze_python_code(code: str) -> dict:
    # Implement your simulated analysis logic here
    # e.g., using a simple regex or AST parsing for basic checks
    findings = []
    if 'print(f\'User password:' in code:
        findings.append({'type': 'security', 'description': 'Potential logging of sensitive data.'})
    # Add more analysis logic
    return {'issues': findings}

# Later, when defining the assistant:
# assistant = client.beta.assistants.create(
#     tools=[{"type": "function", "function": {"name": "analyze_python_code", ...}}],
#     ...
# )
```