dataGemini

API Response Parsing Prompt (Gemini)

API integrations break most often when the response structure changes or contains unexpected null values. This prompt generates both the type definitions and a robust mapper with validation, so unexpected API changes produce a clear error at the parsing boundary rather than a cryptic downstream failure. The zod/pydantic validation section is particularly valuable for catching schema drift early. This variant is formatted for Gemini: Optimised for Gemini 1.5 Pro and Gemini Ultra. Uses Google AI markdown formatting conventions.

Prompt Template
# Gemini AI Prompt

You are a helpful AI assistant powered by Google Gemini.

## Instructions
You are a senior software engineer specialising in API integrations.

Write code to parse and map the following API response.

API response sample:
```json
{{api_response}}
```

Target domain model:
{{target_model}}

Language: {{language}}
Framework/libraries available: {{libraries}}

Provide:
1. **Type definitions** — interfaces or classes for the API response and domain model
2. **Parser/mapper function** — transforms the API response to the domain model
3. **Error handling** — handles missing fields, null values, and unexpected types
4. **Validation** — verifies the response has required fields before mapping
5. **Unit tests** — tests for successful mapping and error cases

## Output Format
Provide a well-structured response using Markdown headers and code blocks where appropriate.

Variables

{{api_response}}A sample API response JSON (one or more objects)
{{target_model}}The internal domain model you want to map to, as a type definition or plain description
{{language}}Programming language: TypeScript, Python, Go, Java, etc.
{{libraries}}Available parsing/validation libraries, e.g., "zod", "pydantic", "joi", or "standard library only"

Example

Input
api_response:
{"data": {"id": "usr_123", "attributes": {"email": "[email protected]", "created_at": "2024-01-15T10:00:00Z"}}}
target_model: User { id: string, email: string, createdAt: Date }
language: TypeScript
libraries: zod
Output
import { z } from 'zod';

const ApiUserSchema = z.object({
  data: z.object({
    id: z.string(),
    attributes: z.object({
      email: z.string().email(),
      created_at: z.string().datetime(),
    }),
  }),
});

function mapApiUser(raw: unknown): User {
  const parsed = ApiUserSchema.parse(raw);
  return {
    id: parsed.data.id,
    email: parsed.data.attributes.email,
    createdAt: new Date(parsed.data.attributes.created_at),
  };
}

Related Tools

FAQ

What if the API does not have consistent types across environments?
Use optional fields (z.optional() in zod, Optional in Python) for fields that may be absent, and coerce types where needed. Log a warning when optional fields are missing so you can track how often the inconsistency occurs.
How do I handle API pagination in the mapper?
Add the pagination metadata (next_cursor, total_count) to the API response type and return them alongside the mapped data. The caller can then implement the pagination loop using the cursor.
Can I generate an OpenAPI schema from an API response sample?
Yes. Change the target_model to "OpenAPI 3.0 schema" and the AI will infer the schema definition from the sample. This is useful for documenting undocumented third-party APIs.

Related Prompts