instagrapi icon indicating copy to clipboard operation
instagrapi copied to clipboard

[BUG] validation errors for MediaXma video_url and header_icon_url: URL input should be a string or URL

Open supreme-gg-gg opened this issue 1 year ago • 3 comments

Try Instagrapi SaaS with a free trial https://hikerapi.com/p/5GBWznd3

Describe the bug

Got this validation error when fetching direct threads, the video_url and header_icon_url fields are not valid URL but declared in the MedaiXma type to be.

For suspected cause and suggested solution see below. I am willing to submit a PR to fix this if you agree with my suggestion.

To Reproduce Anything that uses the function direct_threads_chunk (e.g. getting threads) depending on the content of the thread

Traceback

Traceback (most recent call last):
  File "/Users/supremegg/miniconda3/lib/python3.12/site-packages/instagram/chat.py", line 57, in start_chat
    curses.wrapper(init_chat)
  File "/Users/supremegg/miniconda3/lib/python3.12/curses/__init__.py", line 94, in wrapper
    return func(stdscr, *args, **kwds)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/supremegg/miniconda3/lib/python3.12/site-packages/instagram/chat.py", line 54, in init_chat
    return main_loop(screen, client, username, search_filter)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/supremegg/miniconda3/lib/python3.12/site-packages/instagram/chat.py", line 96, in main_loop
    with_loading_screen(
  File "/Users/supremegg/miniconda3/lib/python3.12/site-packages/instagram/utils/loading.py", line 36, in with_loading_screen
    result = func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/supremegg/miniconda3/lib/python3.12/site-packages/instagram/api/direct_messages.py", line 75, in fetch_chat_data
    res, self.chats_cursor = direct_threads_chunk(
                             ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/supremegg/miniconda3/lib/python3.12/site-packages/instagram/api/utils.py", line 214, in direct_threads_chunk
    threads_chunk, cursor = self.direct_threads_chunk(
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/supremegg/miniconda3/lib/python3.12/site-packages/instagrapi/mixins/direct.py", line 153, in direct_threads_chunk
    threads.append(extract_direct_thread(thread))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/supremegg/miniconda3/lib/python3.12/site-packages/instagrapi/extractors.py", line 285, in extract_direct_thread
    data["messages"].append(extract_direct_message(item))
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/supremegg/miniconda3/lib/python3.12/site-packages/instagrapi/extractors.py", line 353, in extract_direct_message
    data["xma_share"] = extract_media_v1_xma(xma_media_share[0])
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/supremegg/miniconda3/lib/python3.12/site-packages/instagrapi/extractors.py", line 101, in extract_media_v1_xma
    return MediaXma(
           ^^^^^^^^^
  File "/Users/supremegg/miniconda3/lib/python3.12/site-packages/pydantic/main.py", line 253, in __init__
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.ValidationError: 2 validation errors for MediaXma
video_url
  URL input should be a string or URL [type=url_type, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.11/v/url_type
header_icon_url
  Input should be a valid URL, input is empty [type=url_parsing, input_value='', input_type=str]
    For further information visit https://errors.pydantic.dev/2.11/v/url_parsing

Expected behavior This should be avoided if there is no valid video_url or header_icon_url.

Suggested solution:

  1. Check that target_url indeed exists before extracting xma media share:
# Previously: if xma_media_share:
if xma_media_share and xma_media_share[0].get("target_url"):
        data["xma_share"] = extract_media_v1_xma(xma_media_share[0])
  1. Default header_icon_url to be None instead of empty string in extract_media_v1_xma
# Previously: media["header_icon_url"] = media.get("header_icon_url", "")
media["header_icon_url"] = media.get("header_icon_url", None)

This is because of the type declared here, video_url is required and header_icon_url is either None or a valid HttpUrl type which empty string is not

class MediaXma(TypesBaseModel):
    # media_type: int
    video_url: HttpUrl  # for Video and IGTV
    title: Optional[str] = ""
    preview_url: Optional[HttpUrl] = None
    preview_url_mime_type: Optional[str] = None
    header_icon_url: Optional[HttpUrl] = None
    header_icon_width: Optional[int] = None
    header_icon_height: Optional[int] = None
    header_title_text: Optional[str] = None
    preview_media_fbid: Optional[str] = None

This seems to be able to fix it when I reran it after applying the modification locally.

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: MacOS
  • Python version 3.12
  • instagrapi version both 2.1.5 and 2.2.1
  • moveipy version if used N/A
  • imagemagick version if used N/A

Additional context Add any other context about the problem here.

supreme-gg-gg avatar Aug 07 '25 05:08 supreme-gg-gg

Fixed on current master in https://github.com/subzeroid/instagrapi/commit/923e72c.

Root cause: extract_direct_message() was attempting to build xma_share objects even when Instagram sent incomplete XMA payloads without target_url, and extract_media_v1_xma() also normalized header_icon_url to an empty string instead of None.

Current master now:

  • ignores incomplete xma_clip / xma_media_share payloads that do not include target_url
  • normalizes empty header_icon_url to None
  • includes regression coverage for both cases

Local checks:

  • python3 -m py_compile instagrapi/extractors.py tests.py
  • docker compose run --rm --entrypoint python devbox -m unittest tests.DirectExtractorRegressionTestCase

subzeroid avatar Apr 16 '26 10:04 subzeroid

error1 validation error for MediaXma video_url URL scheme should be 'http' or 'https' [type=url_scheme, input_value='instagram://direct_media...ext=7472472767777447583', input_type=str] For further information visit https://errors.pydantic.dev/2.13/v/url_scheme

still getting the error from: threads = self._cl.direct_threads(selected_filter="unread")

alexferra avatar Jun 16 '26 14:06 alexferra

Thanks for the follow-up. This is a different XMA URL shape from the original report: Instagram is returning an internal deep link such as instagram://direct_media... in target_url, not an http(s) URL.

This is fixed in instagrapi 2.16.11 by https://github.com/subzeroid/instagrapi/commit/08c2bbe. MediaXma.video_url now accepts Instagram deep links, and regression coverage was added for Direct XMA payloads with instagram://... targets.

Please upgrade and retry:

pip install -U instagrapi

If it still fails on 2.16.11, please share:

  • instagrapi.__version__
  • the full sanitized Direct item/XMA payload for the failing message, with cookies, tokens, usernames, user ids, and message text redacted

The stack trace alone is enough to identify the URL-scheme issue, but the sanitized payload is needed if Instagram is returning another XMA shape that is not covered yet.

subzeroid avatar Jun 19 '26 19:06 subzeroid