Type Hinting for Query Terms

Overview

When querying for certain information on a page, the desired response type of a particular query term may be ambiguous, with multiple acceptable types depending on the expected usage or processing of the AgentQL result. This may be desirable to both control for return types of values which are present on the page, but also indicate to AgentQL what the desired output should be if the requested data isn't located on the page.

AgentQL Context

Through AgentQL Context, you can pass additional details to hint the desired response type for a term, which results in a more stable query response in the correct format.

Example

Imagine a given page which lists various businesses, along with the business name, star rating, and address:

[Business A]
3.5 stars
123 Example Ln.

[Business B]
No rating
456 Documentation St.

[Business C]
5 stars
789 Great Business Ct.

If you run a general AgentQL query such as the following:

{
    businesses[] {
        business_name
        star_rating
        address
    }
}

The star_rating field for Business B, on any given run, could return null, No rating, or 0.0, all of which AgentQL could interpret as correct. Similarly, for Business A, AgentQL could consider a rating of 3.5 or 3.5 Stars as correct. An example of how you might hint to AgentQL the desired output format and null handling case would be the following:

{
    businesses[] {
        business_name
        star_rating (as a float, or null if not present)
        address
    }
}

By providing this context, it indicates that the desired format for processing results here would be to provide the star_rating value as a float, if present on the page, and null otherwise, leaving no room for ambiguity on what the acceptable response should look like.