API Response Parsing Prompt (LLaMA / Ollama)
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 LLaMA / Ollama: Optimised for LLaMA 3, Mistral, and Ollama local models. Uses [INST] / <<SYS>> instruction format.
[INST] <<SYS>>
You are a helpful, accurate, and detailed AI assistant. Follow the instructions carefully.
<</SYS>>
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 [/INST]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
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: zodimport { 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
API integrations break most often when the response structure changes or contains unexpect...
API Response Parsing Prompt (ChatGPT)API integrations break most often when the response structure changes or contains unexpect...
API Response Parsing Prompt (Claude)API integrations break most often when the response structure changes or contains unexpect...
API Response Parsing Prompt (Gemini)API integrations break most often when the response structure changes or contains unexpect...
JSON Transformation PromptJSON transformations are error-prone when null fields or unexpected types appear. This pro...
REST API Design PromptGood API design is about consistency and predictability. This prompt enforces REST convent...