Skip to content

Add image upload support (/image API) for Google Lens - #38

Open
adarshdigievo wants to merge 1 commit into
masterfrom
feat/image-upload
Open

Add image upload support (/image API) for Google Lens#38
adarshdigievo wants to merge 1 commit into
masterfrom
feat/image-upload

Conversation

@adarshdigievo

Copy link
Copy Markdown
Member

Summary

This PR adds image-upload support for Google Lens, using the new /image POST endpoint.

Changes

  • Added serpapi.upload_image(image, **kwargs).
  • Uploads images with multipart POST /image using:
    • image as the file field.
    • api_key and any additional supplied arguments as form data.
  • The image argument supports:
    • String filesystem paths
    • os.PathLike/pathlib.Path
    • Open binary files
    • BytesIO
      This means users can pass an image path or an open image binary file to upload_image()
  • Added README examples for both Lens workflows:
    • client.search(engine="google_lens", url=...)
    • upload_image(...) followed by client.search(..., image_id=...)

Live Tests

Image uploads were verified using the live tests below:

Usage Code Search ID Result
Public URL results = client.search(engine="google_lens", url="https://raw.githubusercontent.com/serpapi/serpapi-python/master/docs/_static/serpapi-python.png") 6a756f57ad9ecfd6f1ff2edc JSON result
String path upload = client.upload_image(IMAGE_PATH); results = client.search(engine="google_lens", image_id=upload["image_id"]) 6a756f5a55cfe1c665a6b616 JSON result
pathlib.Path image = Path(IMAGE_PATH); upload = client.upload_image(image); results = client.search(engine="google_lens", image_id=upload["image_id"]) 6a756f5d4c437f4d4996fb65 JSON result
Open binary file with open(IMAGE_PATH, "rb") as image: upload = client.upload_image(image); results = client.search(engine="google_lens", image_id=upload["image_id"]) 6a756f60f0114a9bff17f8d0 JSON result
BytesIO image = BytesIO(Path(IMAGE_PATH).read_bytes()); upload = client.upload_image(image); results = client.search(engine="google_lens", image_id=upload["image_id"]) 6a756f63bb48ad0e678bc1b7 JSON result

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds first-class image upload support to serpapi-python for Google Lens workflows via SerpApi’s /image POST endpoint, enabling users to upload local images and then perform Lens searches using the returned image_id.

Changes:

  • Added Client.upload_image(image, **kwargs) (and module-level serpapi.upload_image) to upload images as multipart form data.
  • Updated HTTP API-key injection logic to avoid injecting into query params when api_key is already provided in form data.
  • Added unit tests and documentation/examples covering URL-based Lens search and upload-then-search workflows.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/test_image_upload.py Adds unit coverage for multipart upload behavior, input types, and Google Lens URL vs image_id usage.
serpapi/http.py Adjusts API-key injection to support endpoints that authenticate via form data.
serpapi/core.py Implements Client.upload_image(...) and exposes module-level upload_image.
README.md Documents Google Lens usage via URL and via uploaded image_id.
docs/index.rst Exposes upload_image in Sphinx API reference for both module function and Client method.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@adarshdigievo
adarshdigievo requested review from ilyazub and jvmvik August 7, 2026 06:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants