# create_spark_sql_agent

> **Function** in `langchain_community`

📖 [View in docs](https://reference.langchain.com/python/langchain-community/agent_toolkits/spark_sql/base/create_spark_sql_agent)

Construct a Spark SQL agent from an LLM and tools.

!!! warning
    This agent can execute arbitrary SQL against your Spark environment.

    By default, the agent is allowed to generate SQL strings and run them via the
    underlying connection. This is powerful, but it also means the agent can
    generate expensive or dangerous queries (e.g., long-running queries, large
    scans/joins, or locking queries depending on your environment and permissions).

    ``create_spark_sql_agent`` returns a ``langchain_classic`` ``AgentExecutor``.
    ``AgentExecutor`` is an agent abstraction that has long been considered legacy
    and is not actively supported as the recommended foundation for new production
    applications.

    For production-grade agent development, prefer building with Deep Agents:
    https://github.com/langchain-ai/deepagents

    If you use this in production, coordinate with your security/DB teams and apply
    server-side controls:

    - Use least-privilege roles (ideally read-only, schema-limited).
    - Enforce statement timeouts / max execution time and other resource limits at
      the role or session level.
    - Apply query guardrails (e.g., restrict accessible schemas/tables, limit
      concurrency, and monitor/alert on slow queries).

    Client-side timeouts do not always guarantee that a running statement is
    cancelled on the server.

## Signature

```python
create_spark_sql_agent(
    llm: BaseLanguageModel,
    toolkit: SparkSQLToolkit,
    callback_manager: Optional[BaseCallbackManager] = None,
    callbacks: Callbacks = None,
    prefix: str = SQL_PREFIX,
    suffix: str = SQL_SUFFIX,
    format_instructions: Optional[str] = None,
    input_variables: Optional[List[str]] = None,
    top_k: int = 10,
    max_iterations: Optional[int] = 15,
    max_execution_time: Optional[float] = None,
    early_stopping_method: str = 'force',
    verbose: bool = False,
    agent_executor_kwargs: Optional[Dict[str, Any]] = None,
    **kwargs: Any = {},
) -> AgentExecutor
```

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `llm` | `BaseLanguageModel` | Yes | The language model to use. |
| `toolkit` | `SparkSQLToolkit` | Yes | The Spark SQL toolkit. |
| `callback_manager` | `Optional[BaseCallbackManager]` | No | Optional. The callback manager. Default is None. (default: `None`) |
| `callbacks` | `Callbacks` | No | Optional. The callbacks. Default is None. (default: `None`) |
| `prefix` | `str` | No | Optional. The prefix for the prompt. Default is SQL_PREFIX. (default: `SQL_PREFIX`) |
| `suffix` | `str` | No | Optional. The suffix for the prompt. Default is SQL_SUFFIX. (default: `SQL_SUFFIX`) |
| `format_instructions` | `Optional[str]` | No | Optional. The format instructions for the prompt. Default is None. (default: `None`) |
| `input_variables` | `Optional[List[str]]` | No | Optional. The input variables for the prompt. Default is None. (default: `None`) |
| `top_k` | `int` | No | Optional. The top k for the prompt. Default is 10. (default: `10`) |
| `max_iterations` | `Optional[int]` | No | Optional. The maximum iterations to run. Default is 15. (default: `15`) |
| `max_execution_time` | `Optional[float]` | No | Optional. The maximum execution time. Default is None. (default: `None`) |
| `early_stopping_method` | `str` | No | Optional. The early stopping method. Default is "force". (default: `'force'`) |
| `verbose` | `bool` | No | Optional. Whether to print verbose output. Default is False. (default: `False`) |
| `agent_executor_kwargs` | `Optional[Dict[str, Any]]` | No | Optional. The agent executor kwargs. Default is None. (default: `None`) |
| `kwargs` | `Any` | No | Any. Additional keyword arguments. (default: `{}`) |

## Returns

`AgentExecutor`

The agent executor.

---

[View source on GitHub](https://github.com/langchain-ai/langchain-community/blob/4b280287bd55b99b44db2dd849f02d66c89534d5/libs/community/langchain_community/agent_toolkits/spark_sql/base.py#L17)