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

# Generate Sql

> Generate SQL from a natural-language question. Returns the SQL string plus the schemas it references. Pipe the SQL into `execute_query` to fetch rows; then feed those rows into `generate_plotly_chart` to render a figure. Use this as the first step in any 'show me X' or 'what is Y' data-analysis flow.



## OpenAPI

````yaml /openapi/em-talk2data-v2.yaml post /talk2data/mcp/generate-sql
openapi: 3.1.0
info:
  title: Talk to Data Service
  version: 3.2.0-dev1
servers: []
security: []
paths:
  /talk2data/mcp/generate-sql:
    post:
      tags:
        - mcp
      summary: Generate Sql
      description: >-
        Generate SQL from a natural-language question. Returns the SQL string
        plus the schemas it references. Pipe the SQL into `execute_query` to
        fetch rows; then feed those rows into `generate_plotly_chart` to render
        a figure. Use this as the first step in any 'show me X' or 'what is Y'
        data-analysis flow.
      operationId: mcp_generate_sql
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/GenerateSQLRequest'
        required: true
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/GenerateSQLResponse'
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HTTPValidationError'
        '502':
          description: Text2SQL agent unreachable or pipeline failed
        '503':
          description: Text2SQL agent URL not configured
      security:
        - HTTPBearer: []
components:
  schemas:
    GenerateSQLRequest:
      properties:
        question:
          type: string
          minLength: 1
          title: Question
          description: Natural language question to convert to SQL
        database:
          type: string
          minLength: 1
          title: Database
          description: Resource URI of the data connection (e.g. data:org:project:name)
        schemas:
          anyOf:
            - items:
                $ref: '#/components/schemas/DatasourceSelectedSchema'
              type: array
            - type: 'null'
          title: Schemas
          description: >-
            List of schemas to query. If not provided, defaults to 'public'
            schema.
      type: object
      required:
        - question
        - database
      title: GenerateSQLRequest
      description: Request body for synchronous SQL generation.
    GenerateSQLResponse:
      properties:
        sql:
          type: string
          title: Sql
        explanation:
          type: string
          title: Explanation
        assumptions:
          items:
            type: string
          type: array
          title: Assumptions
          default: []
      type: object
      required:
        - sql
        - explanation
      title: GenerateSQLResponse
      description: Synchronous SQL generation result.
    HTTPValidationError:
      properties:
        detail:
          items:
            $ref: '#/components/schemas/ValidationError'
          type: array
          title: Detail
      type: object
      title: HTTPValidationError
    DatasourceSelectedSchema:
      properties:
        schema_name:
          type: string
          title: Schema Name
        schema_fqn:
          anyOf:
            - type: string
            - type: 'null'
          title: Schema Fqn
        selected_tables:
          anyOf:
            - items:
                $ref: '#/components/schemas/DatasourceSelectedTable'
              type: array
            - type: 'null'
          title: Selected Tables
      type: object
      required:
        - schema_name
      title: DatasourceSelectedSchema
      description: A schema the user scoped, with optional table list.
    ValidationError:
      properties:
        loc:
          items:
            anyOf:
              - type: string
              - type: integer
          type: array
          title: Location
        msg:
          type: string
          title: Message
        type:
          type: string
          title: Error Type
      type: object
      required:
        - loc
        - msg
        - type
      title: ValidationError
    DatasourceSelectedTable:
      properties:
        table_name:
          type: string
          title: Table Name
        table_fqn:
          anyOf:
            - type: string
            - type: 'null'
          title: Table Fqn
      type: object
      required:
        - table_name
      title: DatasourceSelectedTable
      description: A table the user scoped within a schema.
  securitySchemes:
    HTTPBearer:
      type: http
      scheme: bearer

````