remove_background
Remove the background from an image using AI. Upload an image and receive a version with the background removed, ideal for product photos and portraits.
Instructions
Remove the background from an image using AI.
Upload an image and get back a version with the background removed. Works great for product photos, portraits, and any image where you need a clean cutout. Cost: ~2 credits.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| image_url | Yes | URL of the image to process | |
| output_format | No | Background replacement — "transparent" (PNG with alpha) or "white" (white background) | transparent |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/yaparai/tools/edit.py:58-94 (handler)The main handler function that implements the 'remove_background' tool logic. It calls YaparAIClient.generate() with mode 'editor_bg_remove', passes image_url and output_format, then waits for the result and returns the processed image URL.
async def remove_background( image_url: str, output_format: Literal["transparent", "white"] = "transparent", ) -> dict: """ Remove the background from an image using AI. Upload an image and get back a version with the background removed. Works great for product photos, portraits, and any image where you need a clean cutout. Cost: ~2 credits. Args: image_url: URL of the image to process output_format: Background replacement — "transparent" (PNG with alpha) or "white" (white background) Returns: Dict with image_url (processed image), job_id, credits_used, and balance_remaining. """ client = YaparAIClient() job = await client.generate({ "type": "image", "mode": "editor_bg_remove", "image_url": image_url, "output_format": output_format, }) result = await client.wait_for_result(job["job_id"], timeout=60) return { "status": "success", "image_url": result.get("result_url"), "job_id": result.get("job_id"), "credits_used": job.get("credits_used"), "balance_remaining": job.get("balance_remaining"), } - src/yaparai/tools/edit.py:58-60 (schema)Input schema for remove_background: expects image_url (str) and optional output_format limited to 'transparent' or 'white' literal types.
async def remove_background( image_url: str, output_format: Literal["transparent", "white"] = "transparent", - src/yaparai/server.py:130-130 (registration)Registration of remove_background as an MCP tool via mcp.tool(remove_background) on line 130 of the server.
mcp.tool(remove_background) - src/yaparai/server.py:32-36 (registration)Import statement for remove_background from yaparai.tools.edit in the server module.
from yaparai.tools.edit import ( transform_image, remove_background, swap_face, ) - src/yaparai/client.py:126-163 (helper)The YaparAIClient.generate() helper used by remove_background to start the background removal job, plus wait_for_result() to poll for completion.
async def generate(self, request: dict) -> dict: """Start a generation job.""" return await self._request("POST", "/v1/public/generate", json=request) async def get_job(self, job_id: str) -> dict: """Get job status and result.""" return await self._request("GET", f"/v1/public/jobs/{job_id}") async def get_balance(self) -> dict: """Get credit balance.""" return await self._request("GET", "/v1/public/balance") async def get_models(self) -> dict: """List available models and their credit costs.""" return await self._request("GET", "/v1/public/models") async def wait_for_result( self, job_id: str, timeout: int = 120, poll_interval: int = 3, ) -> dict: """Poll job status until completed or timeout.""" elapsed = 0 while elapsed < timeout: job = await self.get_job(job_id) status = job.get("status", "") if status == "succeeded": return job if status == "failed": error = job.get("error_message") or job.get("error") or "Unknown error" raise RuntimeError(f"Generation failed: {error}") await asyncio.sleep(poll_interval) elapsed += poll_interval raise TimeoutError( f"Job {job_id} is still processing after {timeout}s. " f"Use get_job_status('{job_id}') to check later." )