> ## Documentation Index
> Fetch the complete documentation index at: https://developers.datagrid.com/llms.txt
> Use this file to discover all available pages before exploring further.

# AI Search

> AI-powered search that retrieves relevant knowledge from your teamspace, builds a merged context,
and generates a natural language answer with numbered source citations.
The response includes the generated answer, the sources cited (with links and metadata),
and the full search tree results for reference.
Use this when you want an AI-generated summary answer grounded in your team's data.




## OpenAPI

````yaml post /search/ai
openapi: 3.0.3
info:
  version: 0.1.1
  title: Datagrid API
  description: Datagrid API
servers:
  - url: https://api.datagrid.com/v1
security:
  - BearerAuth: []
paths:
  /search/ai:
    post:
      tags:
        - Search
      summary: AI Search
      description: >
        AI-powered search that retrieves relevant knowledge from your teamspace,
        builds a merged context,

        and generates a natural language answer with numbered source citations.

        The response includes the generated answer, the sources cited (with
        links and metadata),

        and the full search tree results for reference.

        Use this when you want an AI-generated summary answer grounded in your
        team's data.
      operationId: searchAi
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SearchAiRequestBody'
      responses:
        '200':
          description: >-
            An AI-generated answer with source citations and the underlying
            search tree results.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SearchAiResult'
        '402':
          description: >-
            Credit limit exceeded. The organization does not have enough credits
            to perform this search. Check your billing status and add credits to
            your account.
        '429':
          description: >-
            Rate limit exceeded. The request has been throttled because the rate
            limit for this endpoint has been reached. Check the `Retry-After`
            response header and retry after the specified number of seconds.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RateLimitError'
      x-codeSamples:
        - lang: JavaScript
          source: >-
            import Datagrid from 'datagrid-ai';


            const client = new Datagrid({
              apiKey: process.env['DATAGRID_API_KEY'], // This is the default and can be omitted
            });


            const searchAIResult = await client.search.searchAI({ query: 'query'
            });


            console.log(searchAIResult.answer);
        - lang: Python
          source: |-
            import os
            from datagrid_ai import Datagrid

            client = Datagrid(
                api_key=os.environ.get("DATAGRID_API_KEY"),  # This is the default and can be omitted
            )
            search_ai_result = client.search.search_ai(
                query="query",
            )
            print(search_ai_result.answer)
components:
  schemas:
    SearchAiRequestBody:
      type: object
      description: Request body for AI-powered search.
      required:
        - query
      properties:
        query:
          type: string
          description: >-
            The natural language search query. The AI will search your indexed
            knowledge and generate an answer.
        record_types:
          type: array
          nullable: true
          items:
            $ref: '#/components/schemas/SearchRecordType'
          description: >-
            Optional filter to restrict search to specific record types (rows,
            tables, files, pages, cells).
        limit:
          type: integer
          nullable: true
          description: >-
            Maximum number of search results to consider when generating the
            answer.
          default: 10
    SearchAiResult:
      type: object
      description: >-
        AI-generated answer with source citations and the underlying search
        results.
      required:
        - object
        - answer
        - sources
        - search_results
        - credits
      properties:
        object:
          type: string
          enum:
            - search_ai_result
          description: The object type, always `search_ai_result`.
        answer:
          type: string
          description: >-
            AI-generated natural language answer based on the search results.
            Contains numbered citations like [1], [2] that reference entries in
            the `sources` array.
        sources:
          type: array
          items:
            $ref: '#/components/schemas/AiSource'
          description: >-
            Sources cited in the answer, ordered by citation index. Each source
            links back to the original data in your teamspace.
        search_results:
          $ref: '#/components/schemas/SearchTreeResult'
          description: >-
            The full search tree results used to generate the answer. Useful for
            displaying raw results alongside the AI summary.
        credits:
          nullable: true
          description: >-
            Credit consumption for this search. When present, `consumed`
            reflects the actual variable cost of the retrieval work performed
            for this request. `null` when the billing write fails or the request
            is aborted before billing completes.
          allOf:
            - $ref: '#/components/schemas/OperationCredits'
    RateLimitError:
      type: object
      description: >-
        Returned when the rate limit is exceeded. Rate limits are enforced per
        teamspace, endpoint path, and HTTP method over a 60-second sliding
        window. Each endpoint may have its own limit — check the
        X-RateLimit-Limit response header for the effective value.
      required:
        - error
        - message
        - retryable
        - status_code
      properties:
        status_code:
          type: integer
          description: The HTTP status code (429).
        statusCode:
          type: integer
          deprecated: true
          description: Deprecated. Use status_code instead.
        error:
          type: string
          enum:
            - rate_limit_exceeded
          description: The error code identifying this as a rate limit error.
        message:
          type: string
          description: A human-readable error message.
        mitigation:
          type: string
          description: Suggested action to resolve the error.
        retryable:
          type: boolean
          description: Whether the request can be retried after a delay.
        details:
          type: object
          properties:
            reason:
              type: string
              description: A detailed explanation of why the rate limit was exceeded.
    SearchRecordType:
      type: string
      description: The record type in the vector database.
      enum:
        - rows
        - tables
        - files
        - pages
        - cells
    AiSource:
      type: object
      description: A source cited in an AI-generated search answer.
      required:
        - index
        - node_id
        - type
        - title
      properties:
        index:
          type: integer
          description: >-
            1-based citation index corresponding to [N] references in the answer
            text.
        node_id:
          type: string
          description: Unique identifier of the source node in the search tree.
        type:
          type: string
          enum:
            - file
            - record
            - table
          description: >-
            The type of source: `file` (document/attachment), `record` (data
            row), or `table` (dataset).
        title:
          type: string
          description: Human-readable title of the source.
        dataset_name:
          type: string
          description: Name of the dataset this source belongs to, if applicable.
        table_name:
          type: string
          description: Name of the table this source belongs to, if applicable.
        emoji:
          type: string
          description: Emoji icon for the source type (e.g., 📄 for files, 📊 for tables).
        url:
          type: string
          description: URL to view the source in the Datagrid web app.
        thumbnail_uri:
          type: string
          description: URI for a thumbnail preview of the source, if available.
        page_numbers:
          type: array
          items:
            type: integer
          description: Page numbers within a document where the relevant content was found.
    SearchTreeResult:
      type: object
      description: >-
        Hierarchical search results grouped by source (datasets, files, pages)
        with navigation items.
      required:
        - object
        - query
        - data
        - nav_items
        - has_more
        - credits
      properties:
        object:
          type: string
          enum:
            - search_tree
          description: The object type, always `search_tree`.
        query:
          type: string
          description: The original search query.
        data:
          type: array
          items:
            type: object
          description: >-
            Tree nodes representing search results grouped by source. Each node
            contains the matched content, metadata, score, and child nodes for
            hierarchical display.
        nav_items:
          type: array
          items:
            type: object
          description: >-
            Navigation items providing quick links to the datasets, tables, and
            files that matched the query. Each item includes a name, emoji, URL,
            and type.
        has_more:
          type: boolean
          description: >-
            Whether more results are available beyond this page. Use the `next`
            cursor to fetch additional results.
        next:
          type: string
          nullable: true
          description: >-
            Pagination cursor (numeric offset as string). Pass this value as the
            `next` query parameter to `/search/tree` to fetch the next page.
        credits:
          nullable: true
          description: >-
            Credit consumption for this search. When present, `consumed`
            reflects the actual variable cost of the retrieval work performed
            for this request. `null` when the billing write fails or the request
            is aborted before billing completes.
          allOf:
            - $ref: '#/components/schemas/OperationCredits'
    OperationCredits:
      type: object
      required:
        - consumed
      properties:
        consumed:
          type: number
          description: The number of credits consumed by the operation.
  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer

````