Test your knowledge of key API design fundamentals for generative AI, including resource modeling, input validation, versioning, and idempotency. Explore best practices and common scenarios to build robust and scalable AI-powered APIs.
When designing an API endpoint for a text-generating AI, which best represents a suitable resource name according to RESTful conventions?
Explanation: /texts follows the RESTful approach of using plural nouns to represent collections of resources, making it the most appropriate. /generateTextNow is action-based, which is less RESTful. /text_output is singular and doesn't indicate a collection. /TextGenAPI refers to the whole API and not a specific resource.
Why is input validation important when designing an endpoint that accepts user-provided prompts for generative AI?
Explanation: Input validation ensures that only appropriate and safe data is processed by your generative AI, preventing security and quality issues. It does not impact model speed, response size, or replace authentication requirements, all of which are different concerns.
Which approach is commonly used to version a generative AI API in the endpoint design?
Explanation: /v1/texts places the version number as a prefix, which is a widely accepted practice. /texts-v1.0.0 and /texts/versionone use less standard and potentially confusing notation. /latest_texts is ambiguous because 'latest' may change over time and doesn't specify a fixed version.
For a generative AI API, which HTTP method is NOT considered idempotent?
Explanation: POST is not idempotent because multiple identical POST requests may result in multiple entries or generations, causing different results each time. GET, PUT, and DELETE are generally idempotent since repeating them yields the same outcome.
What is a best practice for input validation regarding prompt length in a generative AI endpoint?
Explanation: Rejecting prompts that are too long ensures system stability and predictable processing. Allowing unlimited length can lead to resource exhaustion. Truncating without notification may confuse users. Accepting only empty prompts defeats the endpoint's purpose.
Why should API documentation for generative AI endpoints clearly describe response structures?
Explanation: Clear response documentation helps developers parse and use API results effectively. It does not affect bandwidth or AI generation logic, nor does it serve to hide errors.
If your generative AI endpoint accepts files as input, which content type should you specify to support file uploads?
Explanation: multipart/form-data is the standard for handling file uploads in HTTP requests. text/plain is for plain text only. application/octetstram is a misspelling of application/octet-stream and is not a valid type. application/xml is for XML data, not file uploads.
Which API design shows a hierarchical nesting of resources suitable for user-specific generative results?
Explanation: /users/123/texts uses a clear hierarchy, indicating texts generated by a specific user. /results_text and /user-text-output aren't structured and miss the nesting. /getGeneratedText is action-oriented, not resource-based.
What input data type is best for a prompt field when accepting text for a generative AI endpoint?
Explanation: Prompts are naturally strings of text. Integer and boolean are not suitable for handling text data. Array may be used for multiple prompts, but not for a single prompt field.
What header can be used to help ensure idempotency for POST requests in a generative AI API?
Explanation: The Idempotency-Key header allows servers to recognize repeated requests and prevent duplicate operations. Authorization is for security, Content-Length specifies body size, and Accept-Language handles localization.
What should an API provider do before discontinuing an old version of a generative AI endpoint?
Explanation: Advance notification allows users to migrate to newer API versions. Returning errors or reducing capacity without notice disrupts user experience. Deleting all data is unrelated to versioning and can violate user trust.
If a request includes improperly formatted JSON input, what is the correct API behavior?
Explanation: A 400 Bad Request clearly indicates client-side input issues. A 500 error implies a server malfunction. Ignoring format problems risks unpredictable results, and auto-converting to XML is not standard API behavior.
Why are plural nouns recommended when naming resources in RESTful generative AI APIs (e.g., /outputs vs. /output)?
Explanation: Using plural resource names signals that the endpoint deals with a collection rather than a single item, matching REST guidelines. Plurality does not make endpoints private or dictate HTTP methods, and singular forms are technically allowed but less conventional.
Why should generative AI endpoints implement rate limiting as part of their API design?
Explanation: Rate limiting helps protect APIs from overuse and ensures fair access. It does not inherently speed up operations or grant users unlimited access. It also does not replace authentication mechanisms.
If a generative AI API successfully returns generated text, what status code should be used in the response?
Explanation: A 200 OK status indicates the request was successful and content is returned. 404 Not Found signals a missing resource, 403 Forbidden means access was denied, and 201 Created is typically for creating new resources, not generating or retrieving results.
Why is it important to use a consistent error response format in generative AI API endpoints?
Explanation: Consistent error formats help client apps handle errors systematically. Randomizing or hiding errors impedes troubleshooting, and standardized responses cannot guarantee model success—they just help with communication.