Prompting AI for Core Functionality
Now comes the exciting part — getting AI to write the core functionality of your stock fetcher. The quality of AI-generated code depends heavily on how clearly you describe what you need. Vague prompts produce vague code.
Writing Effective Prompts
Think of prompting like giving instructions to a skilled but literal-minded colleague. They'll do exactly what you ask, so be specific about:
- Inputs: What data does the function receive?
- Outputs: What should it return, and in what format?
- Error cases: What could go wrong, and how should it respond?
- Constraints: Are there security or performance requirements?
A weak prompt like "write code to get stock prices" leaves too much to interpretation. The AI might hardcode your API key, skip error handling, or return data in an inconvenient format.
A Strong Prompt Example
Here's a prompt that specifies exactly what you need:
Write a Python function called get_stock_price that:
- Takes a stock ticker symbol as a string
- Uses the Alpha Vantage API to fetch the current price
- Returns a dictionary with 'symbol', 'price', and 'timestamp'
- Raises a custom exception StockNotFoundError if the symbol is invalid
- Uses environment variable ALPHA_VANTAGE_API_KEY for the API key
- Include comments explaining each step
- Include example usage at the bottom
This prompt tells the AI about inputs, outputs, error handling, security (environment variables), and documentation (comments and examples).
Reviewing Before Running
When AI returns code, resist the urge to run it immediately. First, scan through and verify:
- Does it load the API key from an environment variable, not a hardcoded string?
- Does it handle HTTP errors like timeouts or server failures?
- Does it parse the API response correctly?
- Are the comments clear enough that you understand each section?
If something looks wrong or unclear, ask follow-up questions. "Why did you use requests.get instead of requests.Session?" or "What happens if the API returns an empty response?"
Iterating on the Result
Your first prompt rarely produces perfect code. That's normal. Treat AI collaboration as a conversation:
- Get initial code
- Review and identify issues
- Ask for specific improvements
- Repeat until satisfied
You might say: "The code looks good, but add retry logic for network failures" or "Change the return format to include the currency symbol."
This iterative process is how professional developers work with AI tools — not expecting perfection, but refining toward it.