AI / Core OpenAI Codex Application Fundamentals Interview Questions
How do you use the OpenAI API for code generation, review, and debugging tasks?
Code-related tasks are among the most common and well-supported use cases in the OpenAI API. The following patterns apply across code generation, review, and debugging.
from openai import OpenAI client = OpenAI() # 1. Code generation with constraints: generated = client.responses.create( model="gpt-5.5", instructions="""You are a senior Python engineer. Always: - Add type hints - Write docstrings (Google style) - Include basic error handling - Add a usage example in if __name__ == "__main__" """, input="Write a function to parse a CSV file and return a list of dicts.", reasoning={"effort": "medium"}, ) # 2. Code review with structured output: from pydantic import BaseModel class Review(BaseModel): summary: str bugs: list[str] security_issues: list[str] performance_issues: list[str] suggested_improvements: list[str] severity: str # "clean" | "low" | "medium" | "high" | "critical" review = client.responses.create( model="gpt-5.5", input=f"Review this code:\n\n```python\n{code_to_review}\n```", text={"format": {"type": "json_schema", "json_schema": {"name": "review", "schema": Review.model_json_schema(), "strict": True}}}, ) result = Review.model_validate_json(review.output_text) # 3. Debugging: debug_response = client.responses.create( model="gpt-5.5", input=f"""Debug this code. I get this error:\n\n{error_traceback}\n\nCode:\n{code}\n\nExplain the root cause and provide a fix.""", reasoning={"effort": "high"}, ) # 4. Unit test generation: tests = client.responses.create( model="gpt-5.5", instructions="Generate comprehensive pytest tests. Include edge cases, error cases, and typical usage.", input=f"Generate tests for:\n\n{function_code}", tools=[{"type": "code_interpreter"}], # run tests to verify they pass )
Best practices for code tasks: be explicit about constraints in the system prompt (type hints, test framework, error handling style). Use structured outputs for code reviews to get machine-readable results. Use reasoning: effort: high for debugging complex issues. Use code_interpreter to actually run and verify generated code.
More Related questions...