Inferensys

Prompt

Code Comment API Documentation Extraction Prompt Template

A practical prompt playbook for using Code Comment API Documentation Extraction Prompt Template in production AI workflows.
Developer doing prompt engineering on laptop, prompt variations visible on screen, casual coding session.
PROMPT PLAYBOOK

When to Use This Prompt

Defines the ideal job-to-be-done, user profile, and operational boundaries for the code comment extraction prompt.

This prompt is designed for developer tools engineers and platform teams who need to automate the conversion of source code comments into a structured, machine-readable API documentation catalog. The primary job-to-be-done is transforming raw source files containing JSDoc, Python docstrings, JavaDoc, or inline annotations into a consistent JSON array of endpoints, parameters, return types, and usage examples. This output is intended for direct ingestion by documentation generators, API portals, or internal service catalogs, eliminating the manual drift between code and docs. The ideal user is an engineer building an internal developer portal, a CI/CD pipeline maintainer, or a technical writer managing a docs-as-code workflow who needs a reliable, typed extraction layer that can handle the messy reality of incomplete or inconsistent comment formats.

You should use this prompt when you have a repository of source code text and require a structured extraction that normalizes disparate comment styles into a single schema. The prompt assumes the input is raw source code text, not a pre-parsed Abstract Syntax Tree (AST) or a compiled comment tree. This makes it suitable for lightweight pipeline stages where you are processing code as text before or after a parser runs. It is particularly effective for handling legacy codebases where comments are non-standard, or for aggregating documentation across polyglot microservice environments. However, do not use this prompt for generating prose tutorials, architectural decision records, or natural-language summaries of entire codebases. It is a structured extraction tool, not a narrative generator. For high-risk or security-sensitive APIs, you must implement a human review step to validate the extracted records before they are published, as the model may misinterpret authorization scopes or deprecated flags from comments alone.

Before integrating this prompt, ensure your pipeline can handle the required inputs: the raw source code text and a defined output schema. The prompt works best when you provide a clear [OUTPUT_SCHEMA] that matches your downstream ingestion contract, such as a specific JSON structure for your API catalog. If your source code contains no comments, or only auto-generated boilerplate, the extraction will produce sparse or empty records, which is expected behavior. The next step is to wire this prompt into an application harness that validates the JSON output, retries on malformed responses, and logs extraction confidence for auditability. Avoid using this prompt as a one-off utility; its value compounds when it is part of a repeatable CI/CD pipeline that keeps documentation in sync with every commit.

PRACTICAL GUARDRAILS

Use Case Fit

Where this prompt works and where it does not. Use these cards to decide if the Code Comment API Documentation Extraction Prompt Template is the right tool for your pipeline.

01

Good Fit: Structured Comment Formats

Use when: source code contains JSDoc, Python docstrings, JavaDoc, or other structured annotation formats with @param, @returns, and @example tags. Guardrail: the prompt template includes format-specific parsing instructions that map common annotation conventions to a unified output schema.

02

Bad Fit: Implicit or Missing Documentation

Avoid when: the codebase has no comments, sparse inline notes, or documentation that lives entirely in external wikis. Guardrail: the prompt requires explicit comment blocks as input; for undocumented code, pair with a code analysis prompt that infers behavior from implementation before attempting extraction.

03

Required Inputs

What you need: source code files or extracted comment blocks, a target output schema (endpoint, method, parameters, return type, examples), and optional format hints (JSDoc, docstring, etc.). Guardrail: pre-process code to isolate comment blocks before sending to the model to reduce token waste and improve extraction accuracy.

04

Operational Risk: Inconsistent Comment Quality

What to watch: developers write comments with varying completeness, outdated information, or conflicting styles across files. Guardrail: add a confidence field to the output schema for each extracted record and route low-confidence extractions to a human review queue before publishing to API docs.

05

Operational Risk: Hallucinated Endpoints

What to watch: the model may invent parameters, endpoints, or examples not present in the source comments, especially when comments are sparse. Guardrail: implement a post-extraction validator that checks every extracted field against the original comment span and flags unsupported claims for removal or review.

06

Scale Consideration: Large Codebases

What to watch: processing thousands of files sequentially can hit rate limits, cost thresholds, and context window limits. Guardrail: batch files by module, use a map-reduce pattern with per-file extraction followed by cross-file deduplication, and cache results to avoid re-processing unchanged files.

PROMPT PLAYBOOK

Copy-Ready Prompt Template

A reusable prompt for extracting structured API documentation records from source code comments, ready to copy and adapt with your own placeholders.

This prompt template is designed to extract structured API endpoint records from source code comments in formats like JSDoc, Python docstrings, or inline annotations. It expects a raw code block as input and produces a typed JSON array of endpoint objects, each containing method, path, parameters, return types, and examples. The template uses square-bracket placeholders that you must replace before sending to the model—these control the input source, output schema, extraction constraints, and risk tolerance.

text
You are an API documentation extraction engine. Your job is to read source code comments and produce a structured JSON array of API endpoint records.

## INPUT
[CODE_BLOCK]

## OUTPUT SCHEMA
Return a JSON array of endpoint objects. Each object must follow this schema exactly:
{
  "endpoint_id": "string (unique slug derived from method+path)",
  "method": "GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS",
  "path": "string (the route pattern, e.g. /users/{id})",
  "summary": "string (one-line description of what the endpoint does)",
  "description": "string (longer explanation, may include markdown)",
  "parameters": [
    {
      "name": "string",
      "location": "path|query|header|body|cookie",
      "type": "string (JSON Schema type or language type)",
      "required": true|false,
      "description": "string",
      "default": "string or null",
      "example": "string or null"
    }
  ],
  "request_body": {
    "content_type": "string (e.g. application/json)",
    "schema": "string (type description or JSON Schema snippet)",
    "required": true|false,
    "example": "string or null"
  } | null,
  "responses": [
    {
      "status_code": 200,
      "description": "string",
      "schema": "string (type description or JSON Schema snippet)",
      "example": "string or null"
    }
  ],
  "auth_required": true|false,
  "deprecated": true|false,
  "tags": ["string"],
  "source_comment_type": "jsdoc|docstring|inline|block|unknown",
  "extraction_confidence": "high|medium|low",
  "extraction_notes": ["string (any ambiguities, missing fields, or assumptions made)"]
}

## CONSTRAINTS
[CONSTRAINTS]

## EXAMPLES
[EXAMPLES]

## INSTRUCTIONS
1. Parse all comments in the provided code block, regardless of comment style.
2. Identify every documented API endpoint. If a comment block describes an endpoint but is missing the method or path annotation, infer it from surrounding code context if possible and flag it in extraction_notes.
3. For each parameter, extract the name, type, location, required flag, description, default value, and example if present. If a parameter is mentioned in prose but not formally annotated, include it with extraction_confidence set to "low" and note the inference.
4. For request bodies, extract the content type, schema description, required flag, and example. If the body schema is defined in a separate type or interface, include a reference note rather than expanding it inline.
5. For responses, extract every documented status code with its description, schema, and example. If only a success response is documented, include it and note that other status codes are undocumented.
6. Detect deprecation notices, version tags, and authentication requirements from comments and annotations.
7. Set extraction_confidence to "high" only when all expected fields are explicitly present. Use "medium" when some fields required inference. Use "low" when the comment block is sparse or ambiguous.
8. Never fabricate endpoints, parameters, or examples that are not present in the source. If a field is missing, omit it or set it to null as appropriate.
9. If the code block contains no API documentation comments, return an empty array.
10. If [RISK_LEVEL] is "high", include a human_review_required boolean for any endpoint where extraction_confidence is "low" or where auth_required is true but the auth mechanism is unclear.

Return ONLY the JSON array. No markdown fences, no commentary.

To adapt this template, replace [CODE_BLOCK] with the raw source code you want to extract from. Replace [CONSTRAINTS] with any additional rules—for example, "ignore private endpoints marked with @internal" or "only extract endpoints tagged @api-public." Replace [EXAMPLES] with one or two few-shot examples showing a code comment and its expected JSON output to anchor the model's behavior. If your use case is high-risk (generating customer-facing docs, compliance-sensitive APIs), set [RISK_LEVEL] to "high" to enable the human review flag. Always validate the output JSON against your schema before ingestion—missing fields, type mismatches, or hallucinated endpoints are the most common failure modes. For production pipelines, pair this prompt with a validation step that checks required fields, enum values, and path format consistency before the extracted records enter your API catalog.

IMPLEMENTATION TABLE

Prompt Variables

Required inputs for the Code Comment API Documentation Extraction prompt. Validate these before sending the prompt to avoid silent failures or hallucinated endpoints.

PlaceholderPurposeExampleValidation Notes

[SOURCE_CODE]

Raw source file content containing comments to extract from

A complete .ts file with JSDoc blocks above exported functions

Must be non-empty string. Check for binary content or minified code. If file exceeds context window, chunk by function boundary.

[COMMENT_FORMAT]

Expected comment style to parse

JSDoc

Must match one of: JSDoc, docstring, inline, mixed. If null, prompt attempts auto-detection but extraction quality degrades.

[LANGUAGE]

Programming language of the source code

TypeScript

Must be a recognized language identifier. Used to disambiguate comment syntax. If null, auto-detection is attempted.

[EXTRACTION_SCHEMA]

JSON Schema defining the output structure for each endpoint

{"type":"object","properties":{"path":{"type":"string"},"method":{"enum":["GET","POST","PUT","DELETE"]}}}

Must be valid JSON Schema. Validate with a schema validator before injection. Missing required fields in schema cause silent output omissions.

[INCLUDE_EXAMPLES]

Whether to extract code examples from comments

Must be boolean. When false, the 'examples' field in output should be empty array, not null. Check output contract compliance.

[INCLUDE_DEPRECATED]

Whether to include endpoints marked as deprecated

Must be boolean. When false, deprecated endpoints must be excluded entirely. Verify output contains no deprecated markers when set to false.

[CONFIDENCE_THRESHOLD]

Minimum confidence score for including an extracted endpoint

0.7

Must be float between 0.0 and 1.0. Endpoints below threshold should be omitted or flagged for review. Check output for confidence field presence.

[NULL_HANDLING]

Strategy for missing optional fields

omit

Must be one of: omit, null, placeholder. Controls whether missing optional parameters appear as absent keys, explicit null values, or '[MISSING]' placeholders.

PROMPT PLAYBOOK

Implementation Harness Notes

How to wire the code comment extraction prompt into a reliable application pipeline.

This prompt is designed to be embedded in a developer tool or CI/CD pipeline that scans source files, extracts structured API documentation from comments, and writes the results to a documentation database or OpenAPI spec. The harness must handle file I/O, chunking, model invocation, output validation, and error recovery. The prompt itself is a single step in a larger workflow that includes file discovery, comment block isolation, and post-processing of the extracted records.

Implement a validation layer that checks every extracted record against a JSON Schema before accepting it. Required fields include endpoint, method, parameters, and return_type. Reject records where endpoint is missing or method is not a recognized HTTP verb. For optional fields like examples and deprecated, apply default values ([] and false respectively) when absent. Log validation failures with the source file path and the raw model output for debugging. If more than 10% of records from a single file fail validation, halt processing for that file and flag it for human review—this often indicates a malformed comment block or a model drift issue.

Use a retry loop with a maximum of two retries for validation failures. On retry, include the validation error message and the original comment block in the prompt context so the model can correct its output. If the retry also fails, write the raw output to a dead-letter queue and continue processing other files. For model selection, prefer a model with strong JSON mode and code understanding capabilities. Set temperature to 0.0 for deterministic extraction. If processing large codebases, batch files into groups of 50-100 and run extraction in parallel, but rate-limit API calls to avoid throttling. Store extracted records in a versioned documentation store keyed by file_path + endpoint so subsequent runs can diff changes and detect deprecated or removed endpoints.

IMPLEMENTATION TABLE

Expected Output Contract

Defines the strict JSON array schema for extracted API documentation records. Use this contract to build a downstream validator before ingesting model output into a developer portal or API catalog.

Field or ElementType or FormatRequiredValidation Rule

[OUTPUT_ARRAY]

Array of objects

Root must be a JSON array. Reject if object or scalar. Min items: 0, Max items: 500.

endpoint

String

Must match pattern ^/[a-zA-Z0-9_-/{}]+$. Reject if empty string or missing leading slash.

method

Enum string

Must be one of: GET, POST, PUT, PATCH, DELETE. Case-sensitive. Reject on mismatch.

summary

String

Min length: 10 characters. Max length: 300 characters. Reject if only whitespace or missing.

parameters

Array of objects

If present, each object must have 'name' (string), 'type' (string), 'required' (boolean), 'description' (string). Null allowed.

response_type

String

If present, must be a valid JSON type string (object, array, string, number, boolean, null) or a custom model name. Null allowed.

example

String or null

If present, must be a non-empty string. Null explicitly allowed when no example is found in source comments.

source_annotation

String

Must be one of: JSDoc, docstring, inline, block_comment. Reject on unrecognized annotation type.

PRACTICAL GUARDRAILS

Common Failure Modes

What breaks first when extracting structured API documentation from code comments and how to guard against it.

01

Incomplete or Missing Comment Blocks

What to watch: The model hallucinates parameters, return types, or descriptions for undocumented functions, or silently skips them. Guardrail: Instruct the prompt to output an explicit extraction_status field (complete, partial, missing) and require null for any field not found in the source comment.

02

Inconsistent Comment Style Drift

What to watch: A codebase mixes JSDoc, Google-style, Sphinx-style, and informal inline comments. The model applies one style's parsing rules to another, misaligning @param with :param or Args:. Guardrail: Include a [COMMENT_STYLE_HINT] variable or auto-detect preamble. Add few-shot examples showing correct extraction from each style present in the target repo.

03

Type Annotation Mismatch

What to watch: The model trusts the comment's stated type over the actual code signature (e.g., comment says String but code is int), or vice versa, producing an inaccurate spec. Guardrail: Add a [CODE_SIGNATURE] context block alongside the comment. Instruct the model to flag mismatches in a type_conflict field rather than silently resolving them.

04

Example Code Contamination

What to watch: The model treats example code inside a JSDoc @example block as part of the parameter list or endpoint description, polluting the extracted fields. Guardrail: Explicitly instruct the model to isolate @example content into a dedicated examples array in the output schema and exclude it from parameter or description parsing.

05

Over-Abstraction of Endpoint Paths

What to watch: The model normalizes or rewrites endpoint paths (e.g., /users/{id} becomes /users/:id or /users/123), breaking downstream tooling that expects the exact comment string. Guardrail: Require the raw path string from the comment in an extracted_path field, with a separate normalized_path field if normalization is desired. Validate with a regex check post-extraction.

06

Silent Skipping of Overloaded Functions

What to watch: A function has multiple signatures or comment blocks (e.g., TypeScript overloads). The model extracts only the first or merges them into one incoherent record. Guardrail: Instruct the model to output an array of endpoint records per function when multiple signatures are detected. Add a post-processing check that counts extracted records against the number of documented signatures.

IMPLEMENTATION TABLE

Evaluation Rubric

Criteria for testing the quality of extracted API documentation before integrating the output into a developer portal or SDK generation pipeline.

CriterionPass StandardFailure SignalTest Method

Schema Compliance

Output is valid JSON matching the [OUTPUT_SCHEMA] exactly; no extra or missing keys.

JSON parse error, unexpected keys, or required fields missing.

Automated JSON Schema validator run against the raw model output.

Endpoint Completeness

All documented public endpoints in [INPUT] are extracted; no endpoint is skipped.

Count of extracted endpoints is less than the count of documented endpoints in the source.

Manual spot-check or automated count comparison if source metadata is available.

Parameter Accuracy

All parameters for each endpoint are extracted with correct name, type, required flag, and description.

Parameter name is hallucinated, type is incorrect, or required flag contradicts the source comment.

Diff extracted parameters against a manually curated golden set for a sample file.

Return Type Correctness

The return type and description match the source annotation, including generics and promises.

Return type is simplified to 'object' when a complex type is specified, or void/null is misrepresented.

Type string comparison against the source code's type annotations.

Example Validity

Extracted examples are syntactically valid for the target language and match the endpoint's purpose.

Example contains syntax errors, uses undefined variables, or demonstrates an unrelated operation.

Execute extracted code examples in a sandboxed runtime or validate with a language parser.

Null Handling Discipline

Missing optional fields are represented as null or omitted per schema; empty strings are not used for missing data.

Required fields are null, or missing optional fields are represented by empty strings.

Schema validation with strict null checking and type comparison.

Description Quality

Descriptions are extracted verbatim from the source comment; no paraphrasing or summarization.

Description is truncated, rewritten, or contains information not present in the source comment.

String similarity check (e.g., Levenshtein distance) against the source comment text.

Confidence Annotation

A confidence score between 0.0 and 1.0 is provided for each extracted endpoint reflecting comment clarity.

Confidence is always 1.0, or confidence is missing for endpoints with ambiguous or incomplete comments.

Check that confidence is a float in range [0.0, 1.0] and correlates with comment completeness heuristics.

ADAPTATION OPTIONS

Adapt This Prompt

How to adapt

Start with the base prompt and a single language's comment format (e.g., JSDoc). Remove strict schema enforcement and confidence scoring. Use a simple markdown table as the output format instead of JSON. Focus on extracting only the most common tags: @param, @returns, @description.

code
Extract API documentation from the following [LANGUAGE] source code comments.

For each function or method with documentation comments, output:
- Function name
- Description
- Parameters (name, type, description)
- Return type and description

Source code:
[CODE_SNIPPET]

Watch for

  • Incomplete parameter extraction when comments use non-standard tags
  • Missing return type inference when @returns is absent but type is obvious from code
  • Over-extraction of non-API functions (private helpers, constructors)
  • No handling of overloaded function signatures
Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.