Skip to main content

afsar logo

afsar

A simple, student-friendly procedural Python library providing a uniform interface to LLMs. afsar prioritizes installed local coding agents (claude-code, antigravity) so students can use their existing agents and tools, and smoothly falls back to remote cloud APIs (Gemini, OpenAI).


Installation

Install via pip:

pip install afsar

Quickstart

1. Basic Text Query

import afsar

# Setup is explicit and required once before asking
afsar.setup()

answer = afsar.ask("How do you define a function in Python?")
print(answer)

2. Structured Output with @dataclass

You don't need Pydantic to get typed objects. Standard Python dataclasses work out of the box:

from dataclasses import dataclass
import afsar


@dataclass
class QuizQuestion:
    question: str
    points: int
    topic: str


afsar.setup()
quiz = afsar.ask("Create a beginner Python question.", schema=QuizQuestion)

print(quiz.question)
print(f"Points: {quiz.points}")
print(f"Topic: {quiz.topic}")

3. Raw JSON Output

If you just want a JSON-formatted string:

import afsar

afsar.setup()
raw_json = afsar.ask("List 3 Python data structures.", format="json")
print(raw_json)  # Returns clean JSON string

4. Student Coding Exercises (Local Tools)

When using local agents (claude or agy), afsar defaults to tools=["python", "files"]. This allows asking the agent to write and test code locally:

import afsar

afsar.setup()
response = afsar.ask(
    "Write a solution to check if a word is a palindrome, save it to palindrome.py, run it with python, and show the result."
)
print(response)

Remote APIs (Gemini & OpenAI)

Remote models do not support local file or execution tools. When connecting to remote APIs, explicitly pass tools=[]:

import afsar

# Using Gemini
afsar.setup(provider="gemini", api_key="YOUR_GEMINI_KEY", tools=[])

# Or using OpenAI
afsar.setup(provider="openai", api_key="YOUR_OPENAI_KEY", tools=[])

answer = afsar.ask("Explain recursion in simple terms.")
print(answer)

Auto-Detection & Priority

Calling afsar.setup() without arguments automatically searches for available providers in this order:

  1. Claude Code CLI (claude) in PATH
  2. Antigravity CLI (agy) in PATH
  3. Google Gemini (GEMINI_API_KEY in environment)
  4. OpenAI (OPENAI_API_KEY in environment)

You can override the provider explicitly in code:

afsar.setup(provider="agy")

Or via the AFSAR_PROVIDER environment variable:

export AFSAR_PROVIDER=agy

Exceptions

  • NotSetupError: Raised if afsar.ask() is called before afsar.setup().
  • UnsupportedFeatureError: Raised if requesting local tools (tools=["python", "files"]) on remote API providers.
  • ModelNotFoundError: Raised if no supported local CLI or remote API key is discovered.
  • ExecutionError: Raised if a CLI command or remote API request fails.

Metadata

Release files for afsar 1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for afsar 1.0
File Size Uploaded Provenance
afsar-1.0.tar.gz 46.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for afsar 1.0
File Interpreter ABI Platform
afsar-1.0-py3-none-any.whl Python 3 none any Details

Total release size: 83.2 kB

Release files / afsar-1.0.tar.gz

Download URL afsar-1.0.tar.gz
Size 46.5 kB
Tags Source
SHA-256 checksum
How to use checksums
0dc53e2dcca656983965fa4a58cb0e24f4d14c10b602ef10656706d157370248
BLAKE2b-256 checksum
How to use checksums
25cf1e8c0bdac66d9479886e4562878f9a26d03a18b5356dffa11cba017f0332
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.0

Release files / afsar-1.0-py3-none-any.whl

Download URL afsar-1.0-py3-none-any.whl
Size 36.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
04c7c1676fadc7cb7f162337d2eef8d5959b04b193dd915a400325180611d786
BLAKE2b-256 checksum
How to use checksums
eb3fd473754aa833e1cfbc2ab71858417c83426f9deb85a3f2964ade0a63bfa2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.0

Security for afsar 1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page