GroqInterface
On this page
llm.GroqInterface — sync & async Groq interface with native token streaming.
Groq exposes an OpenAI-compatible API, and its Python SDK (groq) mirrors the openai SDK's client surface almost exactly — this class is structurally identical to OpenAIInterface, with groq.Groq / groq.AsyncGroq in place of openai.OpenAI / openai.AsyncOpenAI.
Constructor
GroqInterface(
model_name: str = "llama-3.3-70b-versatile",
api_key: Optional[str] = None,
**kwargs,
)
Creates both groq.Groq (sync) and groq.AsyncGroq (async) clients eagerly. Raises ImportError("install groq: pip install groq") if the groq package is missing.
[!NOTE] Default model is hardcoded, not read from
llm_configllm_config.groq_modelexists (also"llama-3.3-70b-versatile") butGroqInterface.__init__'s default value is a separate literal, not a reference toconfig.groq_model. The values happen to match today; see Configuration for the full list of fields like this.
generate(messages=None, prompt=None, max_tokens=2048, temperature=0.3, tools=None, tool_choice=None, **kwargs) -> Union[str, dict]
Identical contract to OpenAIInterface.generate: messages takes priority over prompt; a missing prompt/messages triggers _build_messages's ValueError, but it's caught inside this same method (not propagated) and folded into the return value; returns r.choices[0].message.content on success or {"error": str(e)} on failure; other exceptions are logged via logger.error and swallowed the same way. tools, when given, is converted via llm.tools.to_groq_tools — a plain alias of to_openai_tools, since Groq's function-calling dialect mirrors OpenAI's exactly — and a resulting tool call is parsed with the same parse_openai_tool_calls helper OpenAI uses, returning the same {"content", "tool_calls", "stop_reason"} dict. See Tool Calling.
Example
from zerda.llm import GroqInterface
llm = GroqInterface(api_key="gsk_...")
print(llm.generate(prompt="مرحباً!"))
generate_async(...)
Same contract, using self._async_client.chat.completions.create(...) with await. Same tools/tool_choice handling as generate.
astream(...) -> AsyncIterator[str]
Native streaming via stream=True, yielding chunk.choices[0].delta.content as each arrives — same pattern as OpenAI's. Errors yield f"Error: {e}" as stream content instead of raising. Does not reconstruct tool-call deltas — see Tool Calling § Streaming limitation.
cleanup() / acleanup()
Same pattern as OpenAIInterface: closes sync/async client, logging (not raising) on failure.
Related APIs
BaseLLMInterfaceOpenAIInterface— the class this one mirrors almost line-for-line.- Tool Calling — the unified
tools/tool_choicecontract.